pre_configured.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.oauth2.rfc6749
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. This module is an implementation of various logic needed
  6. for consuming and providing OAuth 2.0 RFC6749.
  7. """
  8. from __future__ import absolute_import, unicode_literals
  9. from ..grant_types import OpenIDConnectAuthCode
  10. from ..tokens import BearerToken
  11. from ..grant_types import AuthorizationCodeGrant
  12. from ..grant_types import ImplicitGrant
  13. from ..grant_types import ResourceOwnerPasswordCredentialsGrant
  14. from ..grant_types import ClientCredentialsGrant
  15. from ..grant_types import RefreshTokenGrant
  16. from ..grant_types import OpenIDConnectImplicit
  17. from ..grant_types import AuthCodeGrantDispatcher
  18. from .authorization import AuthorizationEndpoint
  19. from .token import TokenEndpoint
  20. from .resource import ResourceEndpoint
  21. from .revocation import RevocationEndpoint
  22. class Server(AuthorizationEndpoint, TokenEndpoint, ResourceEndpoint,
  23. RevocationEndpoint):
  24. """An all-in-one endpoint featuring all four major grant types."""
  25. def __init__(self, request_validator, token_expires_in=None,
  26. token_generator=None, refresh_token_generator=None,
  27. *args, **kwargs):
  28. """Construct a new all-grants-in-one server.
  29. :param request_validator: An implementation of
  30. oauthlib.oauth2.RequestValidator.
  31. :param token_expires_in: An int or a function to generate a token
  32. expiration offset (in seconds) given a
  33. oauthlib.common.Request object.
  34. :param token_generator: A function to generate a token from a request.
  35. :param refresh_token_generator: A function to generate a token from a
  36. request for the refresh token.
  37. :param kwargs: Extra parameters to pass to authorization-,
  38. token-, resource-, and revocation-endpoint constructors.
  39. """
  40. auth_grant = AuthorizationCodeGrant(request_validator)
  41. implicit_grant = ImplicitGrant(request_validator)
  42. password_grant = ResourceOwnerPasswordCredentialsGrant(
  43. request_validator)
  44. credentials_grant = ClientCredentialsGrant(request_validator)
  45. refresh_grant = RefreshTokenGrant(request_validator)
  46. openid_connect_auth = OpenIDConnectAuthCode(request_validator)
  47. openid_connect_implicit = OpenIDConnectImplicit(request_validator)
  48. bearer = BearerToken(request_validator, token_generator,
  49. token_expires_in, refresh_token_generator)
  50. auth_grant_choice = AuthCodeGrantDispatcher( default_auth_grant=auth_grant, oidc_auth_grant=openid_connect_auth)
  51. # See http://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#Combinations for valid combinations
  52. # internally our AuthorizationEndpoint will ensure they can appear in any order for any valid combination
  53. AuthorizationEndpoint.__init__(self, default_response_type='code',
  54. response_types={
  55. 'code': auth_grant_choice,
  56. 'token': implicit_grant,
  57. 'id_token': openid_connect_implicit,
  58. 'id_token token': openid_connect_implicit,
  59. 'code token': openid_connect_auth,
  60. 'code id_token': openid_connect_auth,
  61. 'code token id_token': openid_connect_auth,
  62. 'none': auth_grant
  63. },
  64. default_token_type=bearer)
  65. TokenEndpoint.__init__(self, default_grant_type='authorization_code',
  66. grant_types={
  67. 'authorization_code': auth_grant,
  68. 'password': password_grant,
  69. 'client_credentials': credentials_grant,
  70. 'refresh_token': refresh_grant,
  71. 'openid' : openid_connect_auth
  72. },
  73. default_token_type=bearer)
  74. ResourceEndpoint.__init__(self, default_token='Bearer',
  75. token_types={'Bearer': bearer})
  76. RevocationEndpoint.__init__(self, request_validator)
  77. class WebApplicationServer(AuthorizationEndpoint, TokenEndpoint, ResourceEndpoint,
  78. RevocationEndpoint):
  79. """An all-in-one endpoint featuring Authorization code grant and Bearer tokens."""
  80. def __init__(self, request_validator, token_generator=None,
  81. token_expires_in=None, refresh_token_generator=None, **kwargs):
  82. """Construct a new web application server.
  83. :param request_validator: An implementation of
  84. oauthlib.oauth2.RequestValidator.
  85. :param token_expires_in: An int or a function to generate a token
  86. expiration offset (in seconds) given a
  87. oauthlib.common.Request object.
  88. :param token_generator: A function to generate a token from a request.
  89. :param refresh_token_generator: A function to generate a token from a
  90. request for the refresh token.
  91. :param kwargs: Extra parameters to pass to authorization-,
  92. token-, resource-, and revocation-endpoint constructors.
  93. """
  94. auth_grant = AuthorizationCodeGrant(request_validator)
  95. refresh_grant = RefreshTokenGrant(request_validator)
  96. bearer = BearerToken(request_validator, token_generator,
  97. token_expires_in, refresh_token_generator)
  98. AuthorizationEndpoint.__init__(self, default_response_type='code',
  99. response_types={'code': auth_grant},
  100. default_token_type=bearer)
  101. TokenEndpoint.__init__(self, default_grant_type='authorization_code',
  102. grant_types={
  103. 'authorization_code': auth_grant,
  104. 'refresh_token': refresh_grant,
  105. },
  106. default_token_type=bearer)
  107. ResourceEndpoint.__init__(self, default_token='Bearer',
  108. token_types={'Bearer': bearer})
  109. RevocationEndpoint.__init__(self, request_validator)
  110. class MobileApplicationServer(AuthorizationEndpoint, ResourceEndpoint,
  111. RevocationEndpoint):
  112. """An all-in-one endpoint featuring Implicit code grant and Bearer tokens."""
  113. def __init__(self, request_validator, token_generator=None,
  114. token_expires_in=None, refresh_token_generator=None, **kwargs):
  115. """Construct a new implicit grant server.
  116. :param request_validator: An implementation of
  117. oauthlib.oauth2.RequestValidator.
  118. :param token_expires_in: An int or a function to generate a token
  119. expiration offset (in seconds) given a
  120. oauthlib.common.Request object.
  121. :param token_generator: A function to generate a token from a request.
  122. :param refresh_token_generator: A function to generate a token from a
  123. request for the refresh token.
  124. :param kwargs: Extra parameters to pass to authorization-,
  125. token-, resource-, and revocation-endpoint constructors.
  126. """
  127. implicit_grant = ImplicitGrant(request_validator)
  128. bearer = BearerToken(request_validator, token_generator,
  129. token_expires_in, refresh_token_generator)
  130. AuthorizationEndpoint.__init__(self, default_response_type='token',
  131. response_types={
  132. 'token': implicit_grant},
  133. default_token_type=bearer)
  134. ResourceEndpoint.__init__(self, default_token='Bearer',
  135. token_types={'Bearer': bearer})
  136. RevocationEndpoint.__init__(self, request_validator,
  137. supported_token_types=['access_token'])
  138. class LegacyApplicationServer(TokenEndpoint, ResourceEndpoint,
  139. RevocationEndpoint):
  140. """An all-in-one endpoint featuring Resource Owner Password Credentials grant and Bearer tokens."""
  141. def __init__(self, request_validator, token_generator=None,
  142. token_expires_in=None, refresh_token_generator=None, **kwargs):
  143. """Construct a resource owner password credentials grant server.
  144. :param request_validator: An implementation of
  145. oauthlib.oauth2.RequestValidator.
  146. :param token_expires_in: An int or a function to generate a token
  147. expiration offset (in seconds) given a
  148. oauthlib.common.Request object.
  149. :param token_generator: A function to generate a token from a request.
  150. :param refresh_token_generator: A function to generate a token from a
  151. request for the refresh token.
  152. :param kwargs: Extra parameters to pass to authorization-,
  153. token-, resource-, and revocation-endpoint constructors.
  154. """
  155. password_grant = ResourceOwnerPasswordCredentialsGrant(
  156. request_validator)
  157. refresh_grant = RefreshTokenGrant(request_validator)
  158. bearer = BearerToken(request_validator, token_generator,
  159. token_expires_in, refresh_token_generator)
  160. TokenEndpoint.__init__(self, default_grant_type='password',
  161. grant_types={
  162. 'password': password_grant,
  163. 'refresh_token': refresh_grant,
  164. },
  165. default_token_type=bearer)
  166. ResourceEndpoint.__init__(self, default_token='Bearer',
  167. token_types={'Bearer': bearer})
  168. RevocationEndpoint.__init__(self, request_validator)
  169. class BackendApplicationServer(TokenEndpoint, ResourceEndpoint,
  170. RevocationEndpoint):
  171. """An all-in-one endpoint featuring Client Credentials grant and Bearer tokens."""
  172. def __init__(self, request_validator, token_generator=None,
  173. token_expires_in=None, refresh_token_generator=None, **kwargs):
  174. """Construct a client credentials grant server.
  175. :param request_validator: An implementation of
  176. oauthlib.oauth2.RequestValidator.
  177. :param token_expires_in: An int or a function to generate a token
  178. expiration offset (in seconds) given a
  179. oauthlib.common.Request object.
  180. :param token_generator: A function to generate a token from a request.
  181. :param refresh_token_generator: A function to generate a token from a
  182. request for the refresh token.
  183. :param kwargs: Extra parameters to pass to authorization-,
  184. token-, resource-, and revocation-endpoint constructors.
  185. """
  186. credentials_grant = ClientCredentialsGrant(request_validator)
  187. bearer = BearerToken(request_validator, token_generator,
  188. token_expires_in, refresh_token_generator)
  189. TokenEndpoint.__init__(self, default_grant_type='client_credentials',
  190. grant_types={
  191. 'client_credentials': credentials_grant},
  192. default_token_type=bearer)
  193. ResourceEndpoint.__init__(self, default_token='Bearer',
  194. token_types={'Bearer': bearer})
  195. RevocationEndpoint.__init__(self, request_validator,
  196. supported_token_types=['access_token'])