1"""
2oauthlib.openid.connect.core.endpoints.pre_configured
3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5This module is an implementation of various endpoints needed
6for providing OpenID Connect servers.
7"""
8
9from oauthlib.oauth2.rfc6749.endpoints import (
10 AuthorizationEndpoint,
11 IntrospectEndpoint,
12 ResourceEndpoint,
13 RevocationEndpoint,
14 TokenEndpoint,
15)
16from oauthlib.oauth2.rfc6749.grant_types import (
17 AuthorizationCodeGrant as OAuth2AuthorizationCodeGrant,
18 ClientCredentialsGrant,
19 ImplicitGrant as OAuth2ImplicitGrant,
20 ResourceOwnerPasswordCredentialsGrant,
21)
22from oauthlib.oauth2.rfc8628.grant_types import DeviceCodeGrant
23from oauthlib.oauth2.rfc6749.tokens import BearerToken
24
25from ..grant_types import (
26 AuthorizationCodeGrant,
27 HybridGrant,
28 ImplicitGrant,
29 RefreshTokenGrant,
30)
31from ..grant_types.dispatchers import (
32 AuthorizationCodeGrantDispatcher,
33 AuthorizationTokenGrantDispatcher,
34 ImplicitTokenGrantDispatcher,
35)
36from ..tokens import JWTToken
37from .userinfo import UserInfoEndpoint
38
39
40class Server(
41 AuthorizationEndpoint,
42 IntrospectEndpoint,
43 TokenEndpoint,
44 ResourceEndpoint,
45 RevocationEndpoint,
46 UserInfoEndpoint,
47):
48 """
49 An all-in-one endpoint featuring all four major grant types
50 and extension grants.
51 """
52
53 def __init__(
54 self,
55 request_validator,
56 token_expires_in=None,
57 token_generator=None,
58 refresh_token_generator=None,
59 *args,
60 **kwargs,
61 ):
62 """Construct a new all-grants-in-one server.
63
64 :param request_validator: An implementation of
65 oauthlib.oauth2.RequestValidator.
66 :param token_expires_in: An int or a function to generate a token
67 expiration offset (in seconds) given a
68 oauthlib.common.Request object.
69 :param token_generator: A function to generate a token from a request.
70 :param refresh_token_generator: A function to generate a token from a
71 request for the refresh token.
72 :param kwargs: Extra parameters to pass to authorization-,
73 token-, resource-, and revocation-endpoint constructors.
74 """
75 self.auth_grant = OAuth2AuthorizationCodeGrant(request_validator)
76 self.implicit_grant = OAuth2ImplicitGrant(request_validator)
77 self.password_grant = ResourceOwnerPasswordCredentialsGrant(request_validator)
78 self.credentials_grant = ClientCredentialsGrant(request_validator)
79 self.refresh_grant = RefreshTokenGrant(request_validator)
80 self.openid_connect_auth = AuthorizationCodeGrant(request_validator)
81 self.openid_connect_implicit = ImplicitGrant(request_validator)
82 self.openid_connect_hybrid = HybridGrant(request_validator)
83 self.device_code_grant = DeviceCodeGrant(request_validator, **kwargs)
84
85 self.bearer = BearerToken(
86 request_validator, token_generator, token_expires_in, refresh_token_generator
87 )
88
89 self.jwt = JWTToken(
90 request_validator, token_generator, token_expires_in, refresh_token_generator
91 )
92
93 self.auth_grant_choice = AuthorizationCodeGrantDispatcher(
94 default_grant=self.auth_grant, oidc_grant=self.openid_connect_auth
95 )
96 self.implicit_grant_choice = ImplicitTokenGrantDispatcher(
97 default_grant=self.implicit_grant, oidc_grant=self.openid_connect_implicit
98 )
99
100 # See http://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#Combinations for valid combinations
101 # internally our AuthorizationEndpoint will ensure they can appear in any order for any valid combination
102 AuthorizationEndpoint.__init__(
103 self,
104 default_response_type="code",
105 response_types={
106 "code": self.auth_grant_choice,
107 "token": self.implicit_grant_choice,
108 "id_token": self.openid_connect_implicit,
109 "id_token token": self.openid_connect_implicit,
110 "code token": self.openid_connect_hybrid,
111 "code id_token": self.openid_connect_hybrid,
112 "code id_token token": self.openid_connect_hybrid,
113 "none": self.auth_grant,
114 },
115 default_token_type=self.bearer,
116 )
117
118 self.token_grant_choice = AuthorizationTokenGrantDispatcher(
119 request_validator, default_grant=self.auth_grant, oidc_grant=self.openid_connect_auth
120 )
121
122 TokenEndpoint.__init__(
123 self,
124 default_grant_type="authorization_code",
125 grant_types={
126 "authorization_code": self.token_grant_choice,
127 "password": self.password_grant,
128 "client_credentials": self.credentials_grant,
129 "refresh_token": self.refresh_grant,
130 "urn:ietf:params:oauth:grant-type:device_code": self.device_code_grant,
131 },
132 default_token_type=self.bearer,
133 )
134 ResourceEndpoint.__init__(
135 self, default_token="Bearer", token_types={"Bearer": self.bearer, "JWT": self.jwt}
136 )
137 RevocationEndpoint.__init__(self, request_validator)
138 IntrospectEndpoint.__init__(self, request_validator)
139 UserInfoEndpoint.__init__(self, request_validator)