def get_authenticated_user(self, callback): """ Fetches the authenticated user data upon redirect. This method should be called by the handler that handles the callback URL to which the service redirects when the authenticate_redirect() or authorize_redirect() methods are called. :param callback: A function that is called after the authentication attempt. It is called passing a dictionary with the requested user attributes or None if the authentication failed. """ request_arguments = self.adapter_request_params http = self.adapter_http_client # Verify the OpenID response via direct request to the OP args = map_dict(lambda k, v: (k, v[-1]), request_arguments) args["openid.mode"] = OPENID_MODE_CHECK_AUTHENTICATION url = self._OPENID_ENDPOINT response = http.fetch(RequestAdapter( HTTP_POST, url, urlencode(args), { HEADER_CONTENT_TYPE: CONTENT_TYPE_FORM_URLENCODED, } )) self._on_authentication_verified(callback, response)
def _parse_credentials_response(cls, response, strict=True): """ Parses the entity-body of the OAuth server response to an OAuth credential request. :param response: An instance of :class:`pyoauth.http.ResponseAdapter`. :param strict: ``True`` (default) for string response parsing; ``False`` to be a bit lenient. Some non-compliant OAuth servers return credentials without setting the content-type. Setting this to ``False`` will not raise an error, but will still warn you that the response content-type is not valid. :returns: A tuple of the form:: (pyoauth.oauth1.Credentials instance, other parameters) """ if not response.status: raise InvalidHttpResponseError( "Invalid status code: `%r`" % response.status) if not response.reason: raise InvalidHttpResponseError( "Invalid status message: `%r`" % response.reason) if not response.body: raise InvalidHttpResponseError( "Body is invalid or empty: `%r`" % response.body) if not response.headers: raise InvalidHttpResponseError( "Headers are invalid or not specified: `%r`" % \ response.headers) if response.error: raise HttpError("Could not fetch credentials: HTTP %d - %s" \ % (response.status, response.reason,)) # The response body must be form URL-encoded. if not response.is_body_form_urlencoded(): if strict: raise InvalidContentTypeError( "OAuth credentials server response must " \ "have Content-Type: `%s`; got %r" % (CONTENT_TYPE_FORM_URLENCODED, response.content_type)) else: logging.warning( "Response parsing strict-mode disabled -- " \ "OAuth server credentials response specifies invalid " \ "Content-Type: expected %r; got %r", CONTENT_TYPE_FORM_URLENCODED, response.content_type) params = parse_qs(response.body) # Ensure the keys to this dictionary are unicode strings in Python 3.x. params = map_dict(lambda k, v: (utf8_decode_if_bytes(k), v), params) credentials = Credentials(identifier=params[OAUTH_PARAM_TOKEN][0], shared_secret=params[OAUTH_PARAM_TOKEN_SECRET][0]) return credentials, params
def test_map(self): d = { "a": "aye", "b": 5, "C": 0, 8: 5, } def _uppercase_key(key, value): if isinstance(key, str): return key.upper(), value else: return key, value self.assertDictEqual(map_dict(None, d), d) self.assertDictEqual(map_dict(lambda k, v: (k, v), d), d) self.assertDictEqual(map_dict(_uppercase_key, d), { "A": "aye", "B": 5, "C": 0, 8: 5, })
def test_map(self): d = { "a": "aye", "b": 5, "C": 0, 8: 5, } def _uppercase_key(key, value): if isinstance(key, str): return key.upper(), value else: return key, value self.assertDictEqual(functional.map_dict(None, d), d) self.assertDictEqual(functional.map_dict(lambda k, v: (k, v), d), d) self.assertDictEqual(functional.map_dict(_uppercase_key, d), { "A": "aye", "B": 5, "C": 0, 8: 5, })
def query_unflatten(query): """ Given a query string parses it into an un-flattened query parameter dictionary or given a parameter dictionary, un-flattens it. Example:: dict(a=1, b=[1, 2], c="") -> dict(a[1], b=[1, 2], c=[""]) a=1&b=1&b=2&c= -> dict(a[1], b=[1, 2], c=[""]) :param query: A query parameter dictionary or a query string. If this argument is ``None`` an empty dictionary will be returned. Any other value will raise a :class:`pyoauth.errors.InvalidQueryParametersError` exception. :returns: An un-flattened query parameter dictionary. """ if is_bytes_or_unicode(query): return parse_qs(query) elif isinstance(query, dict): # Un-flatten the dictionary. def _choose(key, value): if not isinstance(value, list) and not isinstance(value, tuple): return key, [value] else: return key, list(value) return map_dict(_choose, query) # Alternative, but slower: #return parse_qs(urlencode_s(query)) elif query is None: return {} else: raise InvalidQueryParametersError( "Dictionary or query string required: got `%r` instead" \ % (query, ))