-
-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement rfc9068 JWT Access Tokens
- Loading branch information
Showing
18 changed files
with
1,602 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from .introspection import JWTIntrospectionEndpoint | ||
from .revocation import JWTRevocationEndpoint | ||
from .token import JWTBearerTokenGenerator | ||
from .token_validator import JWTBearerTokenValidator | ||
|
||
__all__ = [ | ||
'JWTBearerTokenGenerator', | ||
'JWTBearerTokenValidator', | ||
'JWTIntrospectionEndpoint', | ||
'JWTRevocationEndpoint', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from authlib.jose.errors import InvalidClaimError | ||
from authlib.jose.rfc7519 import JWTClaims | ||
|
||
|
||
class JWTAccessTokenClaims(JWTClaims): | ||
REGISTERED_CLAIMS = JWTClaims.REGISTERED_CLAIMS + [ | ||
'client_id', | ||
'auth_time', | ||
'acr', | ||
'amr', | ||
'scope', | ||
'groups', | ||
'roles', | ||
'entitlements', | ||
] | ||
|
||
def validate(self, **kwargs): | ||
self.validate_typ() | ||
|
||
super().validate(**kwargs) | ||
self.validate_client_id() | ||
self.validate_auth_time() | ||
self.validate_acr() | ||
self.validate_amr() | ||
self.validate_scope() | ||
self.validate_groups() | ||
self.validate_roles() | ||
self.validate_entitlements() | ||
|
||
def validate_typ(self): | ||
# The resource server MUST verify that the 'typ' header value is 'at+jwt' | ||
# or 'application/at+jwt' and reject tokens carrying any other value. | ||
if self.header['typ'].lower() not in ('at+jwt', 'application/at+jwt'): | ||
raise InvalidClaimError('typ') | ||
|
||
def validate_client_id(self): | ||
return self._validate_claim_value('client_id') | ||
|
||
def validate_auth_time(self): | ||
auth_time = self.get('auth_time') | ||
if auth_time and not isinstance(auth_time, (int, float)): | ||
raise InvalidClaimError('auth_time') | ||
|
||
def validate_acr(self): | ||
return self._validate_claim_value('acr') | ||
|
||
def validate_amr(self): | ||
amr = self.get('amr') | ||
if amr and not isinstance(self['amr'], list): | ||
raise InvalidClaimError('amr') | ||
|
||
def validate_scope(self): | ||
return self._validate_claim_value('scope') | ||
|
||
def validate_groups(self): | ||
return self._validate_claim_value('groups') | ||
|
||
def validate_roles(self): | ||
return self._validate_claim_value('roles') | ||
|
||
def validate_entitlements(self): | ||
return self._validate_claim_value('entitlements') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
from ..rfc7662 import IntrospectionEndpoint | ||
from authlib.common.errors import ContinueIteration | ||
from authlib.consts import default_json_headers | ||
from authlib.jose.errors import ExpiredTokenError | ||
from authlib.jose.errors import InvalidClaimError | ||
from authlib.oauth2.rfc6750.errors import InvalidTokenError | ||
from authlib.oauth2.rfc9068.token_validator import JWTBearerTokenValidator | ||
|
||
|
||
class JWTIntrospectionEndpoint(IntrospectionEndpoint): | ||
''' | ||
JWTIntrospectionEndpoint inherits from :ref:`specs/rfc7662` | ||
:class:`~authlib.oauth2.rfc7662.IntrospectionEndpoint` and implements the machinery | ||
to automatically process the JWT access tokens. | ||
:param issuer: The issuer identifier for which tokens will be introspected. | ||
:param \\*\\*kwargs: Other parameters are inherited from | ||
:class:`~authlib.oauth2.rfc7662.introspection.IntrospectionEndpoint`. | ||
:: | ||
class MyJWTAccessTokenIntrospectionEndpoint(JWTRevocationEndpoint): | ||
def get_jwks(self): | ||
... | ||
def get_username(self, user_id): | ||
... | ||
authorization_server.register_endpoint( | ||
MyJWTAccessTokenIntrospectionEndpoint( | ||
issuer="https://authorization-server.example.org", | ||
) | ||
) | ||
authorization_server.register_endpoint(MyRefreshTokenIntrospectionEndpoint) | ||
''' | ||
|
||
#: Endpoint name to be registered | ||
ENDPOINT_NAME = 'introspection' | ||
|
||
def __init__(self, issuer, server=None, *args, **kwargs): | ||
super().__init__(*args, server=server, **kwargs) | ||
self.issuer = issuer | ||
|
||
def create_endpoint_response(self, request): | ||
'''''' | ||
# The authorization server first validates the client credentials | ||
client = self.authenticate_endpoint_client(request) | ||
|
||
# then verifies whether the token was issued to the client making | ||
# the revocation request | ||
token = self.authenticate_token(request, client) | ||
|
||
# the authorization server invalidates the token | ||
body = self.create_introspection_payload(token) | ||
return 200, body, default_json_headers | ||
|
||
def authenticate_token(self, request, client): | ||
'''''' | ||
self.check_params(request, client) | ||
|
||
# do not attempt to decode refresh_tokens | ||
if request.form.get('token_type_hint') not in ('access_token', None): | ||
raise ContinueIteration() | ||
|
||
validator = JWTBearerTokenValidator(issuer=self.issuer, resource_server=None) | ||
validator.get_jwks = self.get_jwks | ||
try: | ||
token = validator.authenticate_token(request.form['token']) | ||
|
||
# if the token is not a JWT, fall back to the regular flow | ||
except InvalidTokenError: | ||
raise ContinueIteration() | ||
|
||
if token and self.check_permission(token, client, request): | ||
return token | ||
|
||
def create_introspection_payload(self, token): | ||
if not token: | ||
return {'active': False} | ||
|
||
try: | ||
token.validate() | ||
except ExpiredTokenError: | ||
return {'active': False} | ||
except InvalidClaimError as exc: | ||
if exc.claim_name == 'iss': | ||
raise ContinueIteration() | ||
raise InvalidTokenError() | ||
|
||
|
||
payload = { | ||
'active': True, | ||
'token_type': 'Bearer', | ||
'client_id': token['client_id'], | ||
'scope': token['scope'], | ||
'sub': token['sub'], | ||
'aud': token['aud'], | ||
'iss': token['iss'], | ||
'exp': token['exp'], | ||
'iat': token['iat'], | ||
} | ||
|
||
if username := self.get_username(token['sub']): | ||
payload['username'] = username | ||
|
||
return payload | ||
|
||
def get_jwks(self): | ||
'''Return the JWKs that will be used to check the JWT access token signature. | ||
Developers MUST re-implement this method:: | ||
def get_jwks(self): | ||
return load_jwks("jwks.json") | ||
''' | ||
raise NotImplementedError() | ||
|
||
def get_username(self, user_id: str) -> str: | ||
'''Returns an username from a user ID. | ||
Developers MAY re-implement this method:: | ||
def get_username(self, user_id): | ||
return User.get(id=user_id).username | ||
''' | ||
return None |
Oops, something went wrong.