|
1 |
| -import json |
2 |
| -import os |
3 |
| -import time |
4 |
| -import uuid |
5 |
| - |
6 |
| -import jwt |
7 |
| -from cryptography.hazmat.backends import default_backend |
8 |
| -from cryptography.hazmat.primitives import serialization |
9 |
| -from cryptography.hazmat.primitives.asymmetric import rsa |
10 |
| - |
11 | 1 | from fastapi_oidc import auth
|
| 2 | +from fastapi_oidc.types import IDToken |
12 | 3 |
|
13 | 4 |
|
14 |
| -class Fixtures: |
15 |
| - _fixtures_directory = os.path.join(os.path.dirname(__file__), "fixtures") |
16 |
| - |
17 |
| - with open(_fixtures_directory + "/AuthServerDiscovery.json") as f: |
18 |
| - OIDC_DISCOVERY_RESPONSE = json.load(f) |
19 |
| - |
20 |
| - _key = rsa.generate_private_key( |
21 |
| - backend=default_backend(), public_exponent=65537, key_size=2048 |
22 |
| - ) |
23 |
| - |
24 |
| - TESTING_PRIVATE_KEY = _key.private_bytes( |
25 |
| - serialization.Encoding.PEM, |
26 |
| - serialization.PrivateFormat.PKCS8, |
27 |
| - serialization.NoEncryption(), |
28 |
| - ).decode("UTF-8") |
29 |
| - |
30 |
| - TESTING_PUBLIC_KEY = ( |
31 |
| - _key.public_key() |
32 |
| - .public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.PKCS1) |
33 |
| - .decode("UTF-8") |
34 |
| - ) |
35 |
| - |
| 5 | +def test__authenticate_user( |
| 6 | + monkeypatch, |
| 7 | + mock_discovery, |
| 8 | + token_with_audience, |
| 9 | + config_w_aud, |
| 10 | + test_email, |
| 11 | +): |
36 | 12 |
|
37 |
| -TEST_CONFIG = { |
38 |
| - "client_id": "CongenitalOptimist", |
39 |
| - "audience": "NeverAgain", |
40 |
| - "base_authorization_server_uri": "WhatAreTheCivilianApplications?", |
41 |
| - "issuer": "PokeItWithAStick", |
42 |
| - "signature_cache_ttl": 6e3, |
43 |
| -} |
44 |
| - |
45 |
| -# Test configuration without audience |
46 |
| -TEST_CONFIG_NO_AUD = { |
47 |
| - "client_id": "CongenitalOptimist", |
48 |
| - "base_authorization_server_uri": "WhatAreTheCivilianApplications?", |
49 |
| - "issuer": "PokeItWithAStick", |
50 |
| - "signature_cache_ttl": 6e3, |
51 |
| -} |
52 |
| - |
53 |
| - |
54 |
| -def _make_token( |
55 |
| - email: str, |
56 |
| - private_key: str = Fixtures.TESTING_PRIVATE_KEY, |
57 |
| - client_id: str = str(TEST_CONFIG["client_id"]), |
58 |
| - audience: str = str(TEST_CONFIG["audience"]), |
59 |
| - issuer: str = str(TEST_CONFIG["issuer"]), |
60 |
| -) -> str: |
61 |
| - now = int(time.time()) |
62 |
| - return jwt.encode( |
63 |
| - { |
64 |
| - "aud": audience, |
65 |
| - "iss": issuer, |
66 |
| - "email": email, |
67 |
| - "name": "SweetAndFullOfGrace", |
68 |
| - "preferred_username": "Sweet", |
69 |
| - "exp": now + 30, |
70 |
| - "auth_time": now, |
71 |
| - "sub": "foo", |
72 |
| - "ver": "1", |
73 |
| - "iat": now, |
74 |
| - "jti": str(uuid.uuid4()), |
75 |
| - "amr": [], |
76 |
| - "idp": "", |
77 |
| - "nonce": "", |
78 |
| - "at_hash": "", |
79 |
| - }, |
80 |
| - private_key, |
81 |
| - algorithm="RS256", |
82 |
| - ).decode("UTF-8") |
| 13 | + monkeypatch.setattr(auth.discovery, "configure", mock_discovery) |
83 | 14 |
|
| 15 | + token = token_with_audience |
84 | 16 |
|
85 |
| -# Make a token where audience is client_id |
86 |
| -def _make_token_no_aud( |
87 |
| - email: str, |
88 |
| - private_key: str = Fixtures.TESTING_PRIVATE_KEY, |
89 |
| - client_id: str = str(TEST_CONFIG_NO_AUD["client_id"]), |
90 |
| - issuer: str = str(TEST_CONFIG_NO_AUD["issuer"]), |
91 |
| -) -> str: |
92 |
| - now = int(time.time()) |
93 |
| - return jwt.encode( |
94 |
| - { |
95 |
| - "aud": client_id, |
96 |
| - "iss": issuer, |
97 |
| - "email": email, |
98 |
| - "name": "SweetAndFullOfGrace", |
99 |
| - "preferred_username": "Sweet", |
100 |
| - "exp": now + 30, |
101 |
| - "auth_time": now, |
102 |
| - "sub": "foo", |
103 |
| - "ver": "1", |
104 |
| - "iat": now, |
105 |
| - "jti": str(uuid.uuid4()), |
106 |
| - "amr": [], |
107 |
| - "idp": "", |
108 |
| - "nonce": "", |
109 |
| - "at_hash": "", |
110 |
| - }, |
111 |
| - private_key, |
112 |
| - algorithm="RS256", |
113 |
| - ).decode("UTF-8") |
| 17 | + authenticate_user = auth.get_auth(**config_w_aud) |
| 18 | + id_token: IDToken = authenticate_user(auth_header=f"Bearer {token}") |
114 | 19 |
|
| 20 | + assert id_token.email == test_email # nosec |
| 21 | + assert id_token.aud == config_w_aud["audience"] |
115 | 22 |
|
116 |
| -def test__authenticate_user(monkeypatch): |
117 |
| - def mock_discovery(*args, **kwargs): |
118 |
| - class functions: |
119 |
| - auth_server = lambda **_: Fixtures.OIDC_DISCOVERY_RESPONSE |
120 |
| - public_keys = lambda _: Fixtures.TESTING_PUBLIC_KEY |
121 |
| - signing_algos = lambda x: x["id_token_signing_alg_values_supported"] |
122 | 23 |
|
123 |
| - return functions |
| 24 | +# Ensure that when no audience is supplied, that the audience defaults to client ID |
| 25 | +def test__authenticate_user_no_aud( |
| 26 | + monkeypatch, |
| 27 | + mock_discovery, |
| 28 | + token_without_audience, |
| 29 | + no_audience_config, |
| 30 | + test_email, |
| 31 | +): |
124 | 32 |
|
125 | 33 | monkeypatch.setattr(auth.discovery, "configure", mock_discovery)
|
126 |
| - email = "AnticipationOfANewLoversArrivalThe@VeryLittleGravitasIndeed" |
127 |
| - token = _make_token(email=email) |
128 |
| - authenticate_user = auth.get_auth(**TEST_CONFIG) |
129 |
| - IDToken = authenticate_user(auth_header=f"Bearer {token}") |
130 |
| - assert IDToken.email == email # nosec |
131 | 34 |
|
| 35 | + token = token_without_audience |
132 | 36 |
|
133 |
| -# Ensure that when no audience is supplied, that the audience defaults to client ID |
134 |
| -def test__authenticate_user_no_aud(monkeypatch): |
135 |
| - def mock_discovery(*args, **kwargs): |
136 |
| - class functions: |
137 |
| - auth_server = lambda **_: Fixtures.OIDC_DISCOVERY_RESPONSE |
138 |
| - public_keys = lambda _: Fixtures.TESTING_PUBLIC_KEY |
139 |
| - signing_algos = lambda x: x["id_token_signing_alg_values_supported"] |
| 37 | + authenticate_user = auth.get_auth(**no_audience_config) |
140 | 38 |
|
141 |
| - return functions |
| 39 | + id_token: IDToken = authenticate_user(auth_header=f"Bearer {token}") |
142 | 40 |
|
143 |
| - monkeypatch.setattr(auth.discovery, "configure", mock_discovery) |
144 |
| - email = "AnticipationOfANewLoversArrivalThe@VeryLittleGravitasIndeed" |
145 |
| - token = _make_token_no_aud(email=email) |
146 |
| - authenticate_user = auth.get_auth(**TEST_CONFIG_NO_AUD) |
147 |
| - IDToken = authenticate_user(auth_header=f"Bearer {token}") |
148 |
| - assert IDToken.email == email # nosec |
149 |
| - assert IDToken.aud == TEST_CONFIG_NO_AUD["client_id"] |
| 41 | + assert id_token.email == test_email # nosec |
| 42 | + assert id_token.aud == no_audience_config["client_id"] |
0 commit comments