| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 | 2 |
| 3 """ | 3 """ |
| 4 requests.auth | 4 requests.auth |
| 5 ~~~~~~~~~~~~~ | 5 ~~~~~~~~~~~~~ |
| 6 | 6 |
| 7 This module contains the authentication handlers for Requests. | 7 This module contains the authentication handlers for Requests. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import os | 10 import os |
| 11 import re | 11 import re |
| 12 import time | 12 import time |
| 13 import hashlib | 13 import hashlib |
| 14 import logging | 14 import logging |
| 15 | 15 |
| 16 from base64 import b64encode | 16 from base64 import b64encode |
| 17 | 17 |
| 18 from .compat import urlparse, str | 18 from .compat import urlparse, str |
| 19 from .utils import parse_dict_header | 19 from .utils import parse_dict_header |
| 20 | 20 |
| 21 | |
| 22 log = logging.getLogger(__name__) | 21 log = logging.getLogger(__name__) |
| 23 | 22 |
| 24 CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded' | 23 CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded' |
| 25 CONTENT_TYPE_MULTI_PART = 'multipart/form-data' | 24 CONTENT_TYPE_MULTI_PART = 'multipart/form-data' |
| 26 | 25 |
| 27 | 26 |
| 28 def _basic_auth_str(username, password): | 27 def _basic_auth_str(username, password): |
| 29 """Returns a Basic Auth string.""" | 28 """Returns a Basic Auth string.""" |
| 30 | 29 |
| 31 return 'Basic ' + b64encode(('%s:%s' % (username, password)).encode('latin1'
)).strip().decode('latin1') | 30 return 'Basic ' + b64encode(('%s:%s' % (username, password)).encode('latin1'
)).strip().decode('latin1') |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 # XXX not implemented yet | 98 # XXX not implemented yet |
| 100 entdig = None | 99 entdig = None |
| 101 p_parsed = urlparse(url) | 100 p_parsed = urlparse(url) |
| 102 path = p_parsed.path | 101 path = p_parsed.path |
| 103 if p_parsed.query: | 102 if p_parsed.query: |
| 104 path += '?' + p_parsed.query | 103 path += '?' + p_parsed.query |
| 105 | 104 |
| 106 A1 = '%s:%s:%s' % (self.username, realm, self.password) | 105 A1 = '%s:%s:%s' % (self.username, realm, self.password) |
| 107 A2 = '%s:%s' % (method, path) | 106 A2 = '%s:%s' % (method, path) |
| 108 | 107 |
| 109 if qop == 'auth': | 108 if qop is None: |
| 109 respdig = KD(hash_utf8(A1), "%s:%s" % (nonce, hash_utf8(A2))) |
| 110 elif qop == 'auth' or 'auth' in qop.split(','): |
| 110 if nonce == self.last_nonce: | 111 if nonce == self.last_nonce: |
| 111 self.nonce_count += 1 | 112 self.nonce_count += 1 |
| 112 else: | 113 else: |
| 113 self.nonce_count = 1 | 114 self.nonce_count = 1 |
| 114 | 115 |
| 115 ncvalue = '%08x' % self.nonce_count | 116 ncvalue = '%08x' % self.nonce_count |
| 116 s = str(self.nonce_count).encode('utf-8') | 117 s = str(self.nonce_count).encode('utf-8') |
| 117 s += nonce.encode('utf-8') | 118 s += nonce.encode('utf-8') |
| 118 s += time.ctime().encode('utf-8') | 119 s += time.ctime().encode('utf-8') |
| 119 s += os.urandom(8) | 120 s += os.urandom(8) |
| 120 | 121 |
| 121 cnonce = (hashlib.sha1(s).hexdigest()[:16]) | 122 cnonce = (hashlib.sha1(s).hexdigest()[:16]) |
| 122 noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, hash_utf
8(A2)) | 123 noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, hash_utf
8(A2)) |
| 123 respdig = KD(hash_utf8(A1), noncebit) | 124 respdig = KD(hash_utf8(A1), noncebit) |
| 124 elif qop is None: | |
| 125 respdig = KD(hash_utf8(A1), "%s:%s" % (nonce, hash_utf8(A2))) | |
| 126 else: | 125 else: |
| 127 # XXX handle auth-int. | 126 # XXX handle auth-int. |
| 128 return None | 127 return None |
| 129 | 128 |
| 130 self.last_nonce = nonce | 129 self.last_nonce = nonce |
| 131 | 130 |
| 132 # XXX should the partial digests be encoded too? | 131 # XXX should the partial digests be encoded too? |
| 133 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \ | 132 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \ |
| 134 'response="%s"' % (self.username, realm, nonce, path, respdig) | 133 'response="%s"' % (self.username, realm, nonce, path, respdig) |
| 135 if opaque: | 134 if opaque: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 152 if 'digest' in s_auth.lower() and num_401_calls < 2: | 151 if 'digest' in s_auth.lower() and num_401_calls < 2: |
| 153 | 152 |
| 154 setattr(self, 'num_401_calls', num_401_calls + 1) | 153 setattr(self, 'num_401_calls', num_401_calls + 1) |
| 155 pat = re.compile(r'digest ', flags=re.IGNORECASE) | 154 pat = re.compile(r'digest ', flags=re.IGNORECASE) |
| 156 self.chal = parse_dict_header(pat.sub('', s_auth, count=1)) | 155 self.chal = parse_dict_header(pat.sub('', s_auth, count=1)) |
| 157 | 156 |
| 158 # Consume content and release the original connection | 157 # Consume content and release the original connection |
| 159 # to allow our new request to reuse the same one. | 158 # to allow our new request to reuse the same one. |
| 160 r.content | 159 r.content |
| 161 r.raw.release_conn() | 160 r.raw.release_conn() |
| 161 prep = r.request.copy() |
| 162 prep.prepare_cookies(r.cookies) |
| 162 | 163 |
| 163 r.request.headers['Authorization'] = self.build_digest_header(r.requ
est.method, r.request.url) | 164 prep.headers['Authorization'] = self.build_digest_header( |
| 164 _r = r.connection.send(r.request, **kwargs) | 165 prep.method, prep.url) |
| 166 _r = r.connection.send(prep, **kwargs) |
| 165 _r.history.append(r) | 167 _r.history.append(r) |
| 168 _r.request = prep |
| 166 | 169 |
| 167 return _r | 170 return _r |
| 168 | 171 |
| 169 setattr(self, 'num_401_calls', 1) | 172 setattr(self, 'num_401_calls', 1) |
| 170 return r | 173 return r |
| 171 | 174 |
| 172 def __call__(self, r): | 175 def __call__(self, r): |
| 173 # If we have a saved nonce, skip the 401 | 176 # If we have a saved nonce, skip the 401 |
| 174 if self.last_nonce: | 177 if self.last_nonce: |
| 175 r.headers['Authorization'] = self.build_digest_header(r.method, r.ur
l) | 178 r.headers['Authorization'] = self.build_digest_header(r.method, r.ur
l) |
| 176 r.register_hook('response', self.handle_401) | 179 r.register_hook('response', self.handle_401) |
| 177 return r | 180 return r |
| OLD | NEW |