1 # -*- coding: utf-8 -*-
3 oauthlib.oauth1.rfc5849.endpoints.authorization
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 This module is an implementation of various logic needed
7 for signing and checking OAuth 1.0 RFC 5849 requests.
9 from __future__ import absolute_import, unicode_literals
11 from oauthlib.common import Request, add_params_to_uri
13 from .base import BaseEndpoint
16 from urllib import urlencode
18 from urllib.parse import urlencode
21 class AuthorizationEndpoint(BaseEndpoint):
23 """An endpoint responsible for letting authenticated users authorize access
24 to their protected resources to a client.
26 Typical use would be to have two views, one for displaying the authorization
27 form and one to process said form on submission.
29 The first view will want to utilize ``get_realms_and_credentials`` to fetch
30 requested realms and useful client credentials, such as name and
31 description, to be used when creating the authorization form.
33 During form processing you can use ``create_authorization_response`` to
34 validate the request, create a verifier as well as prepare the final
35 redirection URI used to send the user back to the client.
37 See :doc:`/oauth1/validator` for details on which validator methods to implement
41 def create_verifier(self, request, credentials):
42 """Create and save a new request token.
44 :param request: An oauthlib.common.Request object.
45 :param credentials: A dict of extra token credentials.
46 :returns: The verifier as a dict.
49 'oauth_token': request.resource_owner_key,
50 'oauth_verifier': self.token_generator(),
52 verifier.update(credentials)
53 self.request_validator.save_verifier(
54 request.resource_owner_key, verifier, request)
57 def create_authorization_response(self, uri, http_method='GET', body=None,
58 headers=None, realms=None, credentials=None):
59 """Create an authorization response, with a new request token if valid.
61 :param uri: The full URI of the token request.
62 :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
63 :param body: The request body as a string.
64 :param headers: The request headers as a dict.
65 :param credentials: A list of credentials to include in the verifier.
66 :returns: A tuple of 3 elements.
67 1. A dict of headers to set on the response.
68 2. The response body as a string.
69 3. The response status code as an integer.
71 If the callback URI tied to the current token is "oob", a response with
72 a 200 status code will be returned. In this case, it may be desirable to
73 modify the response to better display the verifier to the client.
75 An example of an authorization request::
77 >>> from your_validator import your_validator
78 >>> from oauthlib.oauth1 import AuthorizationEndpoint
79 >>> endpoint = AuthorizationEndpoint(your_validator)
80 >>> h, b, s = endpoint.create_authorization_response(
81 ... 'https://your.provider/authorize?oauth_token=...',
83 ... 'extra': 'argument',
86 {'Location': 'https://the.client/callback?oauth_verifier=...&extra=argument'}
92 An example of a request with an "oob" callback::
94 >>> from your_validator import your_validator
95 >>> from oauthlib.oauth1 import AuthorizationEndpoint
96 >>> endpoint = AuthorizationEndpoint(your_validator)
97 >>> h, b, s = endpoint.create_authorization_response(
98 ... 'https://your.provider/authorize?foo=bar',
100 ... 'extra': 'argument',
103 {'Content-Type': 'application/x-www-form-urlencoded'}
105 'oauth_verifier=...&extra=argument'
109 request = self._create_request(uri, http_method=http_method, body=body,
112 if not request.resource_owner_key:
113 raise errors.InvalidRequestError(
114 'Missing mandatory parameter oauth_token.')
115 if not self.request_validator.verify_request_token(
116 request.resource_owner_key, request):
117 raise errors.InvalidClientError()
119 request.realms = realms
120 if (request.realms and not self.request_validator.verify_realms(
121 request.resource_owner_key, request.realms, request)):
122 raise errors.InvalidRequestError(
123 description=('User granted access to realms outside of '
124 'what the client may request.'))
126 verifier = self.create_verifier(request, credentials or {})
127 redirect_uri = self.request_validator.get_redirect_uri(
128 request.resource_owner_key, request)
129 if redirect_uri == 'oob':
131 'Content-Type': 'application/x-www-form-urlencoded'}
132 response_body = urlencode(verifier)
133 return response_headers, response_body, 200
135 populated_redirect = add_params_to_uri(
136 redirect_uri, verifier.items())
137 return {'Location': populated_redirect}, None, 302
139 def get_realms_and_credentials(self, uri, http_method='GET', body=None,
141 """Fetch realms and credentials for the presented request token.
143 :param uri: The full URI of the token request.
144 :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
145 :param body: The request body as a string.
146 :param headers: The request headers as a dict.
147 :returns: A tuple of 2 elements.
148 1. A list of request realms.
149 2. A dict of credentials which may be useful in creating the
152 request = self._create_request(uri, http_method=http_method, body=body,
155 if not self.request_validator.verify_request_token(
156 request.resource_owner_key, request):
157 raise errors.InvalidClientError()
159 realms = self.request_validator.get_realms(
160 request.resource_owner_key, request)
161 return realms, {'resource_owner_key': request.resource_owner_key}