def parse_authorization_header(value, charset='utf-8'): '''Parse an HTTP basic/digest authorisation header. :param value: the authorisation header to parse. :return: either `None` if the header was invalid or not given, otherwise an :class:`Auth` object. ''' if not value: return try: auth_type, auth_info = value.split(None, 1) auth_type = auth_type.lower() except ValueError: return if auth_type == 'basic': try: up = b64decode(auth_info.encode(DEFAULT_CHARSET)).decode(charset) username, password = up.split(':', 1) except Exception: return return BasicAuth(username, password) elif auth_type == 'digest': auth_map = parse_dict_header(auth_info) if not digest_parameters.difference(auth_map): return DigestAuth(auth_map.pop('username'), options=auth_map)
def parse_authorization_header(value, charset='utf-8'): '''Parse an HTTP basic/digest authorisation header. :param value: the authorisation header to parse. :return: either `None` if the header was invalid or not given, otherwise an :class:`Auth` object. ''' if not value: return try: auth_type, auth_info = value.split(None, 1) auth_type = auth_type.lower() except ValueError: return if auth_type == 'basic': try: up = b64decode(auth_info.encode(CHARSET)).decode(charset) username, password = up.split(':', 1) except Exception: return return BasicAuth(username, password) elif auth_type == 'digest': auth_map = parse_dict_header(auth_info) if not digest_parameters.difference(auth_map): return DigestAuth(auth_map.pop('username'), options=auth_map)
def handle_401(self, response, exc=None): """Takes the given response and tries digest-auth, if needed.""" if response.status_code == 401: s_auth = response.headers.get('www-authenticate', '') if 'digest' not in s_auth.lower(): return request = response.request if not request.headers.get('authorization'): self.options = parse_dict_header(s_auth.replace('Digest ', '')) authorization = self.encode(request.method, request.url) params = request.inp_params.copy() headers = params.pop('headers', []) headers.append(('authorization', authorization)) params['headers'] = headers response.request_again = request_again(request.method, request.url, params)
def handle_401(self, response, exc=None): """Takes the given response and tries digest-auth, if needed.""" if not exc and response.status_code == 401: request = response.request response._handle_401 = getattr(response, '_handle_401', 0) + 1 s_auth = response.headers.get('www-authenticate', '') if 'digest' in s_auth.lower() and response._handle_401 < 2: self.options = parse_dict_header(s_auth.replace('Digest ', '')) params = request.inp_params.copy() headers = params.pop('headers', []) headers.append(('authorization', self.encode( request.method, request.full_url))) params['headers'] = headers response.request_again = request_again(request.method, request.full_url, params)
def parse_cache_control_header(value, on_update=None, cls=None): """Parse a cache control header. The RFC differs between response and request cache control, this method does not. It's your responsibility to not use the wrong control statements. :param value: a cache control header to be parsed. :param on_update: an optional callable that is called every time a value on the :class:`~.CacheControl` object is changed. :param cls: the class for the returned object. By default :class:`~.RequestCacheControl` is used. :return: a `cls` object. """ if cls is None: cls = RequestCacheControl if not value: return cls(None, on_update) return cls(parse_dict_header(value), on_update)