1 # -*- coding: utf-8 -*-
3 oauthlib.oauth2.rfc6749
4 ~~~~~~~~~~~~~~~~~~~~~~~
6 This module is an implementation of various logic needed
7 for consuming and providing OAuth 2.0 RFC6749.
9 from __future__ import absolute_import, unicode_literals
11 from ..tokens import BearerToken
12 from ..grant_types import AuthorizationCodeGrant
13 from ..grant_types import ImplicitGrant
14 from ..grant_types import ResourceOwnerPasswordCredentialsGrant
15 from ..grant_types import ClientCredentialsGrant
16 from ..grant_types import RefreshTokenGrant
18 from .authorization import AuthorizationEndpoint
19 from .token import TokenEndpoint
20 from .resource import ResourceEndpoint
21 from .revocation import RevocationEndpoint
24 class Server(AuthorizationEndpoint, TokenEndpoint, ResourceEndpoint,
27 """An all-in-one endpoint featuring all four major grant types."""
29 def __init__(self, request_validator, token_expires_in=None,
30 token_generator=None, refresh_token_generator=None,
32 """Construct a new all-grants-in-one server.
34 :param request_validator: An implementation of
35 oauthlib.oauth2.RequestValidator.
36 :param token_expires_in: An int or a function to generate a token
37 expiration offset (in seconds) given a
38 oauthlib.common.Request object.
39 :param token_generator: A function to generate a token from a request.
40 :param refresh_token_generator: A function to generate a token from a
41 request for the refresh token.
42 :param kwargs: Extra parameters to pass to authorization-,
43 token-, resource-, and revocation-endpoint constructors.
45 auth_grant = AuthorizationCodeGrant(request_validator)
46 implicit_grant = ImplicitGrant(request_validator)
47 password_grant = ResourceOwnerPasswordCredentialsGrant(
49 credentials_grant = ClientCredentialsGrant(request_validator)
50 refresh_grant = RefreshTokenGrant(request_validator)
51 bearer = BearerToken(request_validator, token_generator,
52 token_expires_in, refresh_token_generator)
53 AuthorizationEndpoint.__init__(self, default_response_type='code',
56 'token': implicit_grant,
58 default_token_type=bearer)
59 TokenEndpoint.__init__(self, default_grant_type='authorization_code',
61 'authorization_code': auth_grant,
62 'password': password_grant,
63 'client_credentials': credentials_grant,
64 'refresh_token': refresh_grant,
66 default_token_type=bearer)
67 ResourceEndpoint.__init__(self, default_token='Bearer',
68 token_types={'Bearer': bearer})
69 RevocationEndpoint.__init__(self, request_validator)
72 class WebApplicationServer(AuthorizationEndpoint, TokenEndpoint, ResourceEndpoint,
75 """An all-in-one endpoint featuring Authorization code grant and Bearer tokens."""
77 def __init__(self, request_validator, token_generator=None,
78 token_expires_in=None, refresh_token_generator=None, **kwargs):
79 """Construct a new web application server.
81 :param request_validator: An implementation of
82 oauthlib.oauth2.RequestValidator.
83 :param token_expires_in: An int or a function to generate a token
84 expiration offset (in seconds) given a
85 oauthlib.common.Request object.
86 :param token_generator: A function to generate a token from a request.
87 :param refresh_token_generator: A function to generate a token from a
88 request for the refresh token.
89 :param kwargs: Extra parameters to pass to authorization-,
90 token-, resource-, and revocation-endpoint constructors.
92 auth_grant = AuthorizationCodeGrant(request_validator)
93 refresh_grant = RefreshTokenGrant(request_validator)
94 bearer = BearerToken(request_validator, token_generator,
95 token_expires_in, refresh_token_generator)
96 AuthorizationEndpoint.__init__(self, default_response_type='code',
97 response_types={'code': auth_grant},
98 default_token_type=bearer)
99 TokenEndpoint.__init__(self, default_grant_type='authorization_code',
101 'authorization_code': auth_grant,
102 'refresh_token': refresh_grant,
104 default_token_type=bearer)
105 ResourceEndpoint.__init__(self, default_token='Bearer',
106 token_types={'Bearer': bearer})
107 RevocationEndpoint.__init__(self, request_validator)
110 class MobileApplicationServer(AuthorizationEndpoint, ResourceEndpoint,
113 """An all-in-one endpoint featuring Implicit code grant and Bearer tokens."""
115 def __init__(self, request_validator, token_generator=None,
116 token_expires_in=None, refresh_token_generator=None, **kwargs):
117 """Construct a new implicit grant server.
119 :param request_validator: An implementation of
120 oauthlib.oauth2.RequestValidator.
121 :param token_expires_in: An int or a function to generate a token
122 expiration offset (in seconds) given a
123 oauthlib.common.Request object.
124 :param token_generator: A function to generate a token from a request.
125 :param refresh_token_generator: A function to generate a token from a
126 request for the refresh token.
127 :param kwargs: Extra parameters to pass to authorization-,
128 token-, resource-, and revocation-endpoint constructors.
130 implicit_grant = ImplicitGrant(request_validator)
131 bearer = BearerToken(request_validator, token_generator,
132 token_expires_in, refresh_token_generator)
133 AuthorizationEndpoint.__init__(self, default_response_type='token',
135 'token': implicit_grant},
136 default_token_type=bearer)
137 ResourceEndpoint.__init__(self, default_token='Bearer',
138 token_types={'Bearer': bearer})
139 RevocationEndpoint.__init__(self, request_validator,
140 supported_token_types=['access_token'])
143 class LegacyApplicationServer(TokenEndpoint, ResourceEndpoint,
146 """An all-in-one endpoint featuring Resource Owner Password Credentials grant and Bearer tokens."""
148 def __init__(self, request_validator, token_generator=None,
149 token_expires_in=None, refresh_token_generator=None, **kwargs):
150 """Construct a resource owner password credentials grant server.
152 :param request_validator: An implementation of
153 oauthlib.oauth2.RequestValidator.
154 :param token_expires_in: An int or a function to generate a token
155 expiration offset (in seconds) given a
156 oauthlib.common.Request object.
157 :param token_generator: A function to generate a token from a request.
158 :param refresh_token_generator: A function to generate a token from a
159 request for the refresh token.
160 :param kwargs: Extra parameters to pass to authorization-,
161 token-, resource-, and revocation-endpoint constructors.
163 password_grant = ResourceOwnerPasswordCredentialsGrant(
165 refresh_grant = RefreshTokenGrant(request_validator)
166 bearer = BearerToken(request_validator, token_generator,
167 token_expires_in, refresh_token_generator)
168 TokenEndpoint.__init__(self, default_grant_type='password',
170 'password': password_grant,
171 'refresh_token': refresh_grant,
173 default_token_type=bearer)
174 ResourceEndpoint.__init__(self, default_token='Bearer',
175 token_types={'Bearer': bearer})
176 RevocationEndpoint.__init__(self, request_validator)
179 class BackendApplicationServer(TokenEndpoint, ResourceEndpoint,
182 """An all-in-one endpoint featuring Client Credentials grant and Bearer tokens."""
184 def __init__(self, request_validator, token_generator=None,
185 token_expires_in=None, refresh_token_generator=None, **kwargs):
186 """Construct a client credentials grant server.
188 :param request_validator: An implementation of
189 oauthlib.oauth2.RequestValidator.
190 :param token_expires_in: An int or a function to generate a token
191 expiration offset (in seconds) given a
192 oauthlib.common.Request object.
193 :param token_generator: A function to generate a token from a request.
194 :param refresh_token_generator: A function to generate a token from a
195 request for the refresh token.
196 :param kwargs: Extra parameters to pass to authorization-,
197 token-, resource-, and revocation-endpoint constructors.
199 credentials_grant = ClientCredentialsGrant(request_validator)
200 bearer = BearerToken(request_validator, token_generator,
201 token_expires_in, refresh_token_generator)
202 TokenEndpoint.__init__(self, default_grant_type='client_credentials',
204 'client_credentials': credentials_grant},
205 default_token_type=bearer)
206 ResourceEndpoint.__init__(self, default_token='Bearer',
207 token_types={'Bearer': bearer})
208 RevocationEndpoint.__init__(self, request_validator,
209 supported_token_types=['access_token'])