Debian Codegraph @

tests/functional/services/auth/test_id_token.py

raw
1 import json
2 import time
3
4 import jwt
5 import pytest
6
7 import globus_sdk
8 from tests.common import register_api_route
9
10 OIDC_CONFIG = {
11 "issuer": "https://auth.globus.org",
12 "authorization_endpoint": "https://auth.globus.org/v2/oauth2/authorize",
13 "userinfo_endpoint": "https://auth.globus.org/v2/oauth2/userinfo",
14 "token_endpoint": "https://auth.globus.org/v2/oauth2/token",
15 "revocation_endpoint": "https://auth.globus.org/v2/oauth2/token/revoke",
16 "jwks_uri": "https://auth.globus.org/jwk.json",
17 "response_types_supported": ["code", "token", "token id_token", "id_token"],
18 "id_token_signing_alg_values_supported": ["RS512"],
19 "scopes_supported": ["openid", "email", "profile"],
20 "token_endpoint_auth_methods_supported": ["client_secret_basic"],
21 "claims_supported": [
22 "at_hash",
23 "aud",
24 "email",
25 "exp",
26 "name",
27 "nonce",
28 "preferred_username",
29 "iat",
30 "iss",
31 "sub",
32 ],
33 "subject_types_supported": ["public"],
34 }
35
36 JWK = {
37 "keys": [
38 {
39 "alg": "RS512",
40 "e": "AQAB",
41 "kty": "RSA",
42 "n": "73l27Yp7WT2c0Ve7EoGJ13AuKzg-GHU7Mpgx0JKa_hO04gAXSVXRadQy7gmdLLtAK8uBVcV0fHGgsBl4J92t-I7hayiJSLbgbX-sZhI_OfegeOLcSNB9poPS9w60XGqR9buYOW2x-KXXitsmyHXNmg_-1u0uqfKHu9pmST8dcjUYXTM5F3oJpQKeJlSH8daMlDks4xb9Y83EEFRv-ppY965-WTm2NW4pwLlbgGTWFvZ6YS6GTb-mfGwGuzStI0lKZ7dOFx9ryYQ4wSoUVHtIrypT-gbuaT90Z2SkwOH-GaEZJkudctBeGpieOsyC7P40UXpwgGNFy3xoWL4vHpnHmQ", # noqa: E501
43 "use": "sig",
44 }
45 ]
46 }
47 JWK_PEM = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(JWK["keys"][0]))
48
49 TOKEN_PAYLOAD = {
50 "access_token": "auth_access_token",
51 "scope": "profile email openid",
52 "expires_in": 172800,
53 "token_type": "Bearer",
54 "resource_server": "auth.globus.org",
55 "state": "_default",
56 "refresh_token": "auth_refresh_token",
57 "other_tokens": [
58 {
59 "access_token": "transfer_access_token",
60 "scope": "urn:globus:auth:scope:transfer.api.globus.org:all",
61 "expires_in": 172800,
62 "token_type": "Bearer",
63 "resource_server": "transfer.api.globus.org",
64 "state": "_default",
65 "refresh_token": "transfer_refresh",
66 }
67 ],
68 # this is a real ID token
69 # and since the JWK is real as well, it should decode correctly
70 "id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJzdWIiOiJjOGFhZDQzZS1kMjc0LTExZTUtYmY5OC04YjAyODk2Y2Y3ODIiLCJvcmdhbml6YXRpb24iOiJHbG9idXMiLCJuYW1lIjoiU3RlcGhlbiBSb3NlbiIsInByZWZlcnJlZF91c2VybmFtZSI6InNpcm9zZW4yQGdsb2J1c2lkLm9yZyIsImlkZW50aXR5X3Byb3ZpZGVyIjoiNDExNDM3NDMtZjNjOC00ZDYwLWJiZGItZWVlY2FiYTg1YmQ5IiwiaWRlbnRpdHlfcHJvdmlkZXJfZGlzcGxheV9uYW1lIjoiR2xvYnVzIElEIiwiZW1haWwiOiJzaXJvc2VuQHVjaGljYWdvLmVkdSIsImxhc3RfYXV0aGVudGljYXRpb24iOjE2MjE0ODEwMDYsImlkZW50aXR5X3NldCI6W3sic3ViIjoiYzhhYWQ0M2UtZDI3NC0xMWU1LWJmOTgtOGIwMjg5NmNmNzgyIiwib3JnYW5pemF0aW9uIjoiR2xvYnVzIiwibmFtZSI6IlN0ZXBoZW4gUm9zZW4iLCJ1c2VybmFtZSI6InNpcm9zZW4yQGdsb2J1c2lkLm9yZyIsImlkZW50aXR5X3Byb3ZpZGVyIjoiNDExNDM3NDMtZjNjOC00ZDYwLWJiZGItZWVlY2FiYTg1YmQ5IiwiaWRlbnRpdHlfcHJvdmlkZXJfZGlzcGxheV9uYW1lIjoiR2xvYnV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ICo0euHv9GnpVHFL1jWz0NriPTXAv6w08Ylk9JBJtmB3oMKNSO-1TGoWUPFDp9TFFk6N32VyF0hsVDtT5DT3t5oq0qfqbPrZA3R04HARW0xtcK_ejNDHBmj6wysey3EzjT764XTvcGOe63CKQ_RJm97ulVaseIT0Aet7AYo5tQuOiSOQ70xzL7Oax3W6TrWi3FIAA-PIMSrAJKbsG7imGOVkaIObG9a-X5yTOcrB4IG4Wat-pN_QiCiiOw_LDCF-r455PwalmnSGUugMYfsdL2k3UxqwOMLIppHnx5-UVAzj3mygj8eZTp6imjqxNMdakS3vhG8dtxbw", # noqa: E501
71 }
72
73 # this is the 'exp' value encoded above
74 FIXED_JWT_EXPIRATION_TIME = 1621653811
75
76
77 @pytest.fixture
78 def client():
79 # this client ID is the audience for the above id_token
80 # the client_id must match the audience in order for the decode to work (we pass it
81 # as the audience during decoding)
82 return globus_sdk.AuthLoginClient(client_id="7fb58e00-839d-44e3-8047-10a502612dca")
83
84
85 @pytest.fixture(autouse=True)
86 def register_token_response():
87 register_api_route(
88 "auth", "/v2/oauth2/token", method="POST", body=json.dumps(TOKEN_PAYLOAD)
89 )
90
91
92 @pytest.fixture
93 def token_response(register_token_response, client):
94 return client.oauth2_token(
95 {
96 "grant_type": "authorization_code",
97 "code": "foo",
98 "redirect_uri": "https://bar.example.org/",
99 }
100 )
101
102
103 def test_decode_id_token(token_response):
104 register_api_route(
105 "auth",
106 "/.well-known/openid-configuration",
107 method="GET",
108 body=json.dumps(OIDC_CONFIG),
109 )
110 register_api_route("auth", "/jwk.json", method="GET", body=json.dumps(JWK))
111
112 decoded = token_response.decode_id_token(jwt_params={"verify_exp": False})
113 assert decoded["preferred_username"] == "[email protected]"
114
115
116 def test_decode_id_token_with_saved_oidc_config(token_response):
117 register_api_route("auth", "/jwk.json", method="GET", body=json.dumps(JWK))
118
119 decoded = token_response.decode_id_token(
120 openid_configuration=OIDC_CONFIG, jwt_params={"verify_exp": False}
121 )
122 assert decoded["preferred_username"] == "[email protected]"
123
124
125 def test_decode_id_token_with_saved_oidc_config_and_jwk(token_response):
126 decoded = token_response.decode_id_token(
127 openid_configuration=OIDC_CONFIG,
128 jwk=JWK_PEM,
129 jwt_params={"verify_exp": False},
130 )
131 assert decoded["preferred_username"] == "[email protected]"
132
133
134 def test_invalid_decode_id_token_usage(token_response):
135 with pytest.raises(globus_sdk.exc.GlobusSDKUsageError):
136 token_response.decode_id_token(jwk=JWK_PEM, jwt_params={"verify_exp": False})
137
138
139 def test_decode_id_token_with_leeway(token_response):
140 register_api_route(
141 "auth",
142 "/.well-known/openid-configuration",
143 method="GET",
144 body=json.dumps(OIDC_CONFIG),
145 )
146 register_api_route("auth", "/jwk.json", method="GET", body=json.dumps(JWK))
147
148 # do a decode with a leeway parameter set high enough that the ancient
149 # expiration time will be tolerated
150 expiration_delta = time.time() - FIXED_JWT_EXPIRATION_TIME
151 decoded = token_response.decode_id_token(
152 jwt_params={"leeway": expiration_delta + 1}
153 )
154 assert decoded["preferred_username"] == "[email protected]"