|
14 | 14 |
|
15 | 15 | """Test cases for the firebase_admin.fpnv module.""" |
16 | 16 |
|
| 17 | +import base64 |
| 18 | +import time |
17 | 19 | from unittest import mock |
| 20 | +from unittest.mock import patch |
18 | 21 |
|
19 | 22 | import jwt |
20 | 23 | import pytest |
| 24 | +from cryptography.hazmat.primitives.asymmetric import ec |
21 | 25 |
|
22 | 26 | import firebase_admin |
23 | 27 | from firebase_admin import fpnv |
|
29 | 33 | _ISSUER = f'https://fpnv.googleapis.com/projects/{_PROJECT_ID}' |
30 | 34 | _PHONE_NUMBER = '+1234567890' |
31 | 35 | _PUBLIC_KEY = 'test-public-key' # In real tests, use the corresponding public key |
| 36 | +_ALGORITHM = 'ES256' |
| 37 | +_KEY_ID = 'test-key-id' |
| 38 | +_TYPE = 'JWT' |
32 | 39 |
|
33 | 40 | _MOCK_PAYLOAD = { |
34 | 41 | 'iss': _ISSUER, |
35 | | - 'sub': '+1234567890', |
| 42 | + 'sub': _PHONE_NUMBER, |
36 | 43 | 'aud': [_ISSUER], |
37 | 44 | 'exp': _EXP_TIMESTAMP, |
38 | 45 | 'iat': _EXP_TIMESTAMP - 3600, |
@@ -102,6 +109,61 @@ def test_client_explicit_app(self): |
102 | 109 |
|
103 | 110 | class TestVerifyToken(TestCommon): |
104 | 111 |
|
| 112 | + def test_verify_token_with_real_crypto(self): |
| 113 | + """Verifies a token signed with a real ES256 key pair. |
| 114 | +
|
| 115 | + Mocking only the JWKS endpoint. |
| 116 | + This ensures the cryptographic verification logic is functioning correctly. |
| 117 | + """ |
| 118 | + # Generate a real ES256 key pair |
| 119 | + private_key = ec.generate_private_key(ec.SECP256R1()) |
| 120 | + public_key = private_key.public_key() |
| 121 | + |
| 122 | + # Create the JWK representation of the public key (for the mock endpoint) |
| 123 | + # Note: Retrieving numbers from the key involves cryptography primitives |
| 124 | + public_numbers = public_key.public_numbers() |
| 125 | + |
| 126 | + def to_b64url(b_data): |
| 127 | + return base64.urlsafe_b64encode(b_data).rstrip(b'=').decode('utf-8') |
| 128 | + |
| 129 | + jwk = { |
| 130 | + "kty": "EC", |
| 131 | + "use": "sig", |
| 132 | + "alg": _ALGORITHM, |
| 133 | + "kid": _KEY_ID, |
| 134 | + "crv": "P-256", |
| 135 | + "x": to_b64url(public_numbers.x.to_bytes(32, 'big')), |
| 136 | + "y": to_b64url(public_numbers.y.to_bytes(32, 'big')), |
| 137 | + } |
| 138 | + now = int(time.time()) |
| 139 | + payload = { |
| 140 | + 'iss': _ISSUER, |
| 141 | + 'aud': [_ISSUER], |
| 142 | + 'iat': now, |
| 143 | + 'exp': now + 3600, |
| 144 | + 'sub': _PHONE_NUMBER |
| 145 | + } |
| 146 | + |
| 147 | + # Sign using the private key object directly (PyJWT supports this) |
| 148 | + token = jwt.encode( |
| 149 | + payload, |
| 150 | + private_key, |
| 151 | + algorithm=_ALGORITHM, |
| 152 | + headers={'alg': _ALGORITHM, 'typ': _TYPE, 'kid': _KEY_ID}, |
| 153 | + ) |
| 154 | + |
| 155 | + # Mock PyJWKClient fetch_data |
| 156 | + with patch('jwt.PyJWKClient.fetch_data') as mock_fetch: |
| 157 | + mock_fetch.return_value = {'keys': [jwk]} |
| 158 | + |
| 159 | + app = firebase_admin.get_app() |
| 160 | + client = fpnv.client(app) |
| 161 | + decoded_token = client.verify_token(token) |
| 162 | + |
| 163 | + assert decoded_token['sub'] == _PHONE_NUMBER |
| 164 | + assert _ISSUER in decoded_token['aud'] |
| 165 | + assert decoded_token.phone_number == decoded_token['sub'] |
| 166 | + |
105 | 167 | @mock.patch('jwt.PyJWKClient') |
106 | 168 | @mock.patch('jwt.decode') |
107 | 169 | @mock.patch('jwt.get_unverified_header') |
|
0 commit comments