| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from allauth.core.internal import jwkkit |
| 6 |
from allauth.core.internal.deferred import jwt |
| 7 |
from allauth.idp.oidc.adapter import get_adapter |
| 8 |
from allauth.idp.oidc.models import Token |
| 9 |
|
| 10 |
|
| 11 |
def decode_jwt_token( |
| 12 |
value: str, *, client_id: str | None = None, verify_exp: bool, verify_iss: bool |
| 13 |
) -> dict[str, Any] | None: |
| 14 |
if not value: |
| 15 |
return None |
| 16 |
try: |
| 17 |
headers = jwt.get_unverified_header(value) |
| 18 |
|
| 19 |
if "kid" not in headers: |
| 20 |
return None |
| 21 |
|
| 22 |
adapter = get_adapter() |
| 23 |
for key in adapter.list_private_keys(is_active=True): |
| 24 |
jwk_dict, private_key = jwkkit.load_jwk_from_pem(key.pem) |
| 25 |
if jwk_dict["kid"] == headers["kid"]: |
| 26 |
break |
| 27 |
else: |
| 28 |
return None |
| 29 |
|
| 30 |
issuer: str | None = None |
| 31 |
audience: str | None = None |
| 32 |
if client_id: |
| 33 |
audience = client_id |
| 34 |
if verify_iss: |
| 35 |
issuer = adapter.get_issuer() |
| 36 |
return jwt.decode( |
| 37 |
value, |
| 38 |
key=private_key.public_key(), |
| 39 |
algorithms=["RS256"], |
| 40 |
options={ |
| 41 |
"verify_signature": True, |
| 42 |
"verify_iss": verify_iss, |
| 43 |
"verify_aud": client_id is not None, |
| 44 |
"verify_exp": verify_exp, |
| 45 |
}, |
| 46 |
audience=audience, |
| 47 |
issuer=issuer, |
| 48 |
) |
| 49 |
except jwt.PyJWTError: |
| 50 |
return None |
| 51 |
|
| 52 |
|
| 53 |
def is_jwt_token(token: str) -> bool: |
| 54 |
# Same check as done by the `JWTToken` class of `oauthlib` |
| 55 |
# count == "2" should be sufficient as currently only JWS as AccessTokens is supported, |
| 56 |
# but count == "4" allows also for JWE, if that is added in the future. |
| 57 |
# This is more of a "loose" check, but as those JWT that aren't detected using this check |
| 58 |
# will be rejected by the verify_request anyways, we can use the same logic here. |
| 59 |
return ( |
| 60 |
isinstance(token, str) and token.startswith("ey") and token.count(".") in (2, 4) |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
def determine_token_type( |
| 65 |
token: str, token_type_hint: str | None |
| 66 |
) -> tuple[list[Token.Type] | None, list[Token.Type] | None]: |
| 67 |
""" |
| 68 |
https://datatracker.ietf.org/doc/html/rfc7009#section-2.1 |
| 69 |
> token_type_hint OPTIONAL. A hint about the type of the token |
| 70 |
> submitted for revocation. Clients MAY pass this parameter in |
| 71 |
> order to help the authorization server to optimize the token |
| 72 |
> lookup. If the server is unable to locate the token using |
| 73 |
> the given hint, it MUST extend its search across all of its |
| 74 |
> supported token types. An authorization server MAY ignore |
| 75 |
> this parameter, particularly if it is able to detect the |
| 76 |
> token type automatically. This specification defines two |
| 77 |
> such values: |
| 78 |
|
| 79 |
> * access_token: An access token as defined in [RFC6749], |
| 80 |
> Section 1.4 |
| 81 |
|
| 82 |
> * refresh_token: A refresh token as defined in [RFC6749], |
| 83 |
> Section 1.5 |
| 84 |
|
| 85 |
> Specific implementations, profiles, and extensions of this |
| 86 |
> specification MAY define other values for this parameter |
| 87 |
> using the registry defined in Section 4.1.2. |
| 88 |
|
| 89 |
Returns a tuple of (token_types_to_try, token_type_for_error_response) |
| 90 |
""" |
| 91 |
|
| 92 |
# This allows us to still handle JWT tokens when the configured access token format has been changed |
| 93 |
if is_jwt_token(token): |
| 94 |
payload = decode_jwt_token(token, verify_exp=True, verify_iss=True) |
| 95 |
if payload: |
| 96 |
return ( |
| 97 |
( |
| 98 |
[Token.Type.ACCESS_TOKEN] |
| 99 |
if payload.get("token_use") == "access" |
| 100 |
else None |
| 101 |
), |
| 102 |
None, |
| 103 |
) |
| 104 |
# In theory, an opaque token could start with "ey...", so continue. |
| 105 |
default = ([Token.Type.ACCESS_TOKEN, Token.Type.REFRESH_TOKEN], None) |
| 106 |
if not isinstance(token_type_hint, str): |
| 107 |
return default |
| 108 |
return { |
| 109 |
"access_token": ([Token.Type.ACCESS_TOKEN], [Token.Type.REFRESH_TOKEN]), |
| 110 |
"refresh_token": ([Token.Type.REFRESH_TOKEN], [Token.Type.ACCESS_TOKEN]), |
| 111 |
}.get(token_type_hint, default) |