1"""
2oauthlib.openid.connect.core.grant_types
3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4"""
5import logging
6
7from oauthlib.oauth2.rfc6749.grant_types.authorization_code import (
8 AuthorizationCodeGrant as OAuth2AuthorizationCodeGrant,
9)
10
11from .base import GrantTypeBase
12
13log = logging.getLogger(__name__)
14
15
16class AuthorizationCodeGrant(GrantTypeBase):
17
18 def __init__(self, request_validator=None, **kwargs):
19 self.proxy_target = OAuth2AuthorizationCodeGrant(
20 request_validator=request_validator, **kwargs)
21 self.custom_validators.post_auth.append(
22 self.openid_authorization_validator)
23 self.register_token_modifier(self.add_id_token)
24
25 def add_id_token(self, token, token_handler, request):
26 """
27 Construct an initial version of id_token, and let the
28 request_validator sign or encrypt it.
29
30 The authorization_code version of this method is used to
31 retrieve the nonce accordingly to the code storage.
32 """
33 # Treat it as normal OAuth 2 auth code request if openid is not present
34 if not request.scopes or 'openid' not in request.scopes:
35 return token
36
37 nonce = self.request_validator.get_authorization_code_nonce(
38 request.client_id,
39 request.code,
40 request.redirect_uri,
41 request
42 )
43 return super().add_id_token(token, token_handler, request, nonce=nonce)