| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime |
| 4 |
import sys |
| 5 |
import typing as t |
| 6 |
|
| 7 |
import jwt |
| 8 |
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey |
| 9 |
|
| 10 |
from globus_sdk.response import GlobusHTTPResponse |
| 11 |
|
| 12 |
from ._common import SupportsJWKMethods |
| 13 |
|
| 14 |
if t.TYPE_CHECKING: |
| 15 |
from globus_sdk import AuthLoginClient, GlobusAppConfig |
| 16 |
|
| 17 |
if sys.version_info >= (3, 11): |
| 18 |
from typing import Self |
| 19 |
else: |
| 20 |
from typing_extensions import Self |
| 21 |
|
| 22 |
|
| 23 |
class IDTokenDecoder: |
| 24 |
""" |
| 25 |
JWT decoder for OIDC ID tokens issued by Globus Auth. |
| 26 |
Decoding uses a client object to fetch necessary data from Globus Auth. |
| 27 |
|
| 28 |
By default, the OIDC configuration data and JWKs will be cached in an internal dict. |
| 29 |
An alternative cache can be provided on init to use an alternative storage |
| 30 |
mechanism. |
| 31 |
|
| 32 |
The ``get_jwt_audience`` and ``get_jwt_leeway`` methods supply parameters to |
| 33 |
decoding. Subclasses can override these methods to customize the decoder. |
| 34 |
|
| 35 |
:param auth_client: The client which should be used to callout to Globus Auth as |
| 36 |
needed. Any AuthClient or AuthLoginClient will work for this purpose. |
| 37 |
:param jwt_leeway: The JWT leeway to use during decoding, as a number of seconds |
| 38 |
or a timedelta. The default is 5 minutes. |
| 39 |
:param jwt_options: The ``options`` passed to the underlying JWT decode function. |
| 40 |
Defaults to an empty dict. |
| 41 |
""" |
| 42 |
|
| 43 |
def __init__( |
| 44 |
self, |
| 45 |
auth_client: SupportsJWKMethods, |
| 46 |
*, |
| 47 |
# default to 300 seconds |
| 48 |
# |
| 49 |
# valuable inputs to this number: |
| 50 |
# - expected clock drift per day (6s for a bad clock) |
| 51 |
# - Windows time sync interval (64s) |
| 52 |
# - Windows' stated goal of meeting the Kerberos 5 clock skew requirement (5m) |
| 53 |
# - ntp panic threshold (1000s of drift) |
| 54 |
# - the knowledge that VM clocks typically run slower and may skew significantly |
| 55 |
# |
| 56 |
# NTP panic should be understood as a critical error; 1000s of drift is |
| 57 |
# therefore too high for us to allow. |
| 58 |
# |
| 59 |
# 300s (5m) is therefore chosen to match the Windows desired maximum for |
| 60 |
# clock drift, and the underlying Kerberos requirement. |
| 61 |
jwt_leeway: float | datetime.timedelta = 300.0, |
| 62 |
jwt_options: dict[str, t.Any] | None = None, |
| 63 |
) -> None: |
| 64 |
self._auth_client = auth_client |
| 65 |
self._openid_configuration: dict[str, t.Any] | None = None |
| 66 |
self._jwk: RSAPublicKey | None = None |
| 67 |
|
| 68 |
self.jwt_leeway: float | datetime.timedelta = jwt_leeway |
| 69 |
self.jwt_options: dict[str, t.Any] = ( |
| 70 |
jwt_options if jwt_options is not None else {} |
| 71 |
) |
| 72 |
|
| 73 |
@classmethod |
| 74 |
def for_globus_app( |
| 75 |
cls, |
| 76 |
*, |
| 77 |
app_name: str, # pylint: disable=unused-argument |
| 78 |
config: GlobusAppConfig, # pylint: disable=unused-argument |
| 79 |
login_client: AuthLoginClient, |
| 80 |
) -> Self: |
| 81 |
""" |
| 82 |
Create an ``IDTokenDecoder`` for use in a GlobusApp. |
| 83 |
|
| 84 |
:param app_name: The name supplied to the GlobusApp. |
| 85 |
:param config: The configuration supplied to the GlobusApp. |
| 86 |
:param login_client: A login client to use for instantiating an |
| 87 |
``IDTokenDecoder``. |
| 88 |
""" |
| 89 |
return cls(login_client) |
| 90 |
|
| 91 |
def decode(self, id_token: str, /) -> dict[str, t.Any]: |
| 92 |
""" |
| 93 |
The ``decode()`` method takes an ``id_token`` as a string, and decodes it to |
| 94 |
a dictionary. |
| 95 |
|
| 96 |
This method should implicitly retrieve the OpenID configuration and JWK |
| 97 |
for Globus Auth. |
| 98 |
|
| 99 |
:param id_token: The token to decode |
| 100 |
""" |
| 101 |
audience = self.get_jwt_audience() |
| 102 |
openid_configuration = self.get_openid_configuration() |
| 103 |
jwk = self.get_jwk() |
| 104 |
|
| 105 |
signing_algos = openid_configuration["id_token_signing_alg_values_supported"] |
| 106 |
|
| 107 |
return jwt.decode( |
| 108 |
id_token, |
| 109 |
key=jwk, |
| 110 |
algorithms=signing_algos, |
| 111 |
audience=audience, |
| 112 |
options=self.jwt_options, |
| 113 |
leeway=self.jwt_leeway, |
| 114 |
) |
| 115 |
|
| 116 |
def get_jwt_audience(self) -> str | None: |
| 117 |
""" |
| 118 |
The audience for JWT verification defaults to the client's client ID. |
| 119 |
""" |
| 120 |
return self._auth_client.client_id |
| 121 |
|
| 122 |
def store_openid_configuration( |
| 123 |
self, openid_configuration: dict[str, t.Any] | GlobusHTTPResponse |
| 124 |
) -> None: |
| 125 |
""" |
| 126 |
Store openid_configuration data for future use in ``decode()``. |
| 127 |
|
| 128 |
:param openid_configuration: The configuration data, as fetched via |
| 129 |
:meth:`AuthClient.get_openid_configuration` |
| 130 |
""" |
| 131 |
if isinstance(openid_configuration, GlobusHTTPResponse): |
| 132 |
self._openid_configuration = openid_configuration.data |
| 133 |
else: |
| 134 |
self._openid_configuration = openid_configuration |
| 135 |
|
| 136 |
def store_jwk(self, jwk: RSAPublicKey) -> None: |
| 137 |
""" |
| 138 |
Store a JWK for future use in ``decode()``. |
| 139 |
|
| 140 |
:param jwk: The JWK, as fetched via :meth:`AuthClient.get_jwk` |
| 141 |
with ``as_pem=True``. |
| 142 |
""" |
| 143 |
self._jwk = jwk |
| 144 |
|
| 145 |
def get_openid_configuration(self) -> dict[str, t.Any]: |
| 146 |
""" |
| 147 |
Fetch the OpenID Configuration for Globus Auth, and cache the result before |
| 148 |
returning it. |
| 149 |
|
| 150 |
If a config was previously stored, return that instead. |
| 151 |
""" |
| 152 |
if self._openid_configuration is None: |
| 153 |
self._openid_configuration = ( |
| 154 |
self._auth_client.get_openid_configuration().data |
| 155 |
) |
| 156 |
return self._openid_configuration |
| 157 |
|
| 158 |
def get_jwk(self) -> RSAPublicKey: |
| 159 |
""" |
| 160 |
Fetch the JWK for Globus Auth, and cache the result before returning it. |
| 161 |
|
| 162 |
If a key was previously stored, return that instead. |
| 163 |
""" |
| 164 |
if not self._jwk: |
| 165 |
self._jwk = self._auth_client.get_jwk( |
| 166 |
openid_configuration=self.get_openid_configuration(), as_pem=True |
| 167 |
) |
| 168 |
return self._jwk |