pre_configured.py 12 KB

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