summary refs log tree commit diff
path: root/tests/test_utils/oidc.py
diff options
context:
space:
mode:
authorAndrew Morgan <1342360+anoadragon453@users.noreply.github.com>2025-05-02 12:16:14 +0100
committerGitHub <noreply@github.com>2025-05-02 12:16:14 +0100
commitfd5d3d852df9dbbac13b406144be7ec5a807078d (patch)
tree6331033096a408730c96f0f7c708792cc68f481a /tests/test_utils/oidc.py
parentFix typo in doc for Scheduled Tasks Admin API (#18384) (diff)
downloadsynapse-fd5d3d852df9dbbac13b406144be7ec5a807078d.tar.xz
Don't check the `at_hash` (access token hash) in OIDC ID Tokens if we don't use the access token (#18374)
Co-authored-by: Eric Eastwood <erice@element.io>
Diffstat (limited to 'tests/test_utils/oidc.py')
-rw-r--r--tests/test_utils/oidc.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/tests/test_utils/oidc.py b/tests/test_utils/oidc.py

index 6c4be1c1f8..5bf5e5cb0c 100644 --- a/tests/test_utils/oidc.py +++ b/tests/test_utils/oidc.py
@@ -20,7 +20,9 @@ # +import base64 import json +from hashlib import sha256 from typing import Any, ContextManager, Dict, List, Optional, Tuple from unittest.mock import Mock, patch from urllib.parse import parse_qs @@ -154,10 +156,23 @@ class FakeOidcServer: json_payload = json.dumps(payload) return jws.serialize_compact(protected, json_payload, self._key).decode("utf-8") - def generate_id_token(self, grant: FakeAuthorizationGrant) -> str: + def generate_id_token( + self, grant: FakeAuthorizationGrant, access_token: str + ) -> str: + # Generate a hash of the access token for the optional + # `at_hash` field in an ID Token. + # + # 3.1.3.6. ID Token, https://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken + at_hash = ( + base64.urlsafe_b64encode(sha256(access_token.encode("ascii")).digest()[:16]) + .rstrip(b"=") + .decode("ascii") + ) + now = int(self._clock.time()) id_token = { **grant.userinfo, + "at_hash": at_hash, "iss": self.issuer, "aud": grant.client_id, "iat": now, @@ -243,7 +258,7 @@ class FakeOidcServer: } if "openid" in grant.scope: - token["id_token"] = self.generate_id_token(grant) + token["id_token"] = self.generate_id_token(grant, access_token) return dict(token)