| 1 |
"""Helper functions for mocking CDSE authentication endpoints.""" |
| 2 |
|
| 3 |
import base64 |
| 4 |
import datetime |
| 5 |
import json |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
import jwt |
| 9 |
from cryptography.hazmat.primitives.asymmetric import rsa |
| 10 |
|
| 11 |
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) |
| 12 |
|
| 13 |
|
| 14 |
def mock_openid(requests_mock: Any) -> None: |
| 15 |
with open("tests/credentials/mock/openid-configuration.json") as f: |
| 16 |
requests_mock.get( |
| 17 |
"https://identity.dataspace.copernicus.eu/auth/realms/CDSE/.well-known/openid-configuration", |
| 18 |
text=f.read(), |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
def mock_token(requests_mock: Any) -> None: |
| 23 |
headers = {"alg": "RS256", "typ": "JWT", "kid": "key-9000"} |
| 24 |
now = datetime.datetime.now() |
| 25 |
payload = { |
| 26 |
"exp": now.timestamp() + 3600, |
| 27 |
"iat": now.timestamp(), |
| 28 |
"jti": "bb4d3fab-1a39-442c-9a6f-072a167543c0", |
| 29 |
"iss": "https://identity.dataspace.copernicus.eu/auth/realms/CDSE", |
| 30 |
"aud": ["CLOUDFERRO_PUBLIC", "account"], |
| 31 |
"sub": "bfb3df17-4506-4adf-86ab-9bb9d03f11f1", |
| 32 |
"typ": "Bearer", |
| 33 |
"azp": "cdse-public", |
| 34 |
"session_state": "4adfd9e9-27d2-496d-9c10-bd3a54c8f1a3", |
| 35 |
"allowed-origins": [ |
| 36 |
"https://localhost:4200", |
| 37 |
"*", |
| 38 |
"https://workspace.staging-cdse-data-explorer.apps.staging.intra.cloudferro.com", |
| 39 |
], |
| 40 |
"realm_access": { |
| 41 |
"roles": [ |
| 42 |
"offline_access", |
| 43 |
"uma_authorization", |
| 44 |
"default-roles-cdas", |
| 45 |
"copernicus-general", |
| 46 |
] |
| 47 |
}, |
| 48 |
"resource_access": { |
| 49 |
"account": { |
| 50 |
"roles": ["manage-account", "manage-account-links", "view-profile"] |
| 51 |
} |
| 52 |
}, |
| 53 |
"scope": "AUDIENCE_PUBLIC openid email profile user-context", |
| 54 |
"sid": "03a2986d-ccfa-45e7-8e3a-55982e7e2a6e", |
| 55 |
"group_membership": [ |
| 56 |
"/access_groups/user_typology/copernicus_general", |
| 57 |
"/organizations/default-4f0080be-2b79-4837-b6a2-7f10c2b9ee1d/regular_user", |
| 58 |
], |
| 59 |
"email_verified": True, |
| 60 |
"organizations": ["default-4f0080be-2b79-4837-b6a2-7f10c2b9ee1d"], |
| 61 |
"name": "User Full Name", |
| 62 |
"user_context_id": "b9ab6ae1-83b0-433d-828f-e9e06adcc4a2", |
| 63 |
"context_roles": {}, |
| 64 |
"context_groups": [ |
| 65 |
"/access_groups/user_typology/copernicus_general/", |
| 66 |
"/organizations/default-4f0080be-2b79-4837-b6a2-7f10c2b9ee1d/regular_user/", |
| 67 |
], |
| 68 |
"preferred_username": "[email protected]", |
| 69 |
"given_name": "User first name", |
| 70 |
"user_context": "default-4f0080be-2b79-4837-b6a2-7f10c2b9ee1d", |
| 71 |
"family_name": "User last name", |
| 72 |
"email": "[email protected]", |
| 73 |
} |
| 74 |
|
| 75 |
response = json.dumps( |
| 76 |
{ |
| 77 |
"access_token": jwt.encode( |
| 78 |
payload, private_key, algorithm="RS256", headers=headers |
| 79 |
), |
| 80 |
"expires_in": 600, |
| 81 |
"refresh_expires_in": 3600, |
| 82 |
"refresh_token": "check-for-refresh-token-not-implementation-yet", |
| 83 |
"token_type": "Bearer", |
| 84 |
"not-before-policy": 0, |
| 85 |
"session_state": "4adfd9e9-27d2-496d-9c10-bd3a54c8f1a3", |
| 86 |
"scope": "AUDIENCE_PUBLIC openid email profile user-context", |
| 87 |
} |
| 88 |
) |
| 89 |
|
| 90 |
requests_mock.post( |
| 91 |
"https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token", |
| 92 |
text=response, |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def mock_jwks(mocker: Any) -> None: |
| 97 |
n = private_key.public_key().public_numbers().n |
| 98 |
e = private_key.public_key().public_numbers().e |
| 99 |
n = base64.urlsafe_b64encode( |
| 100 |
n.to_bytes((n.bit_length() + 7) // 8, byteorder="big") |
| 101 |
).decode("utf-8") |
| 102 |
e = base64.urlsafe_b64encode( |
| 103 |
e.to_bytes((e.bit_length() + 7) // 8, byteorder="big") |
| 104 |
).decode("utf-8") |
| 105 |
|
| 106 |
jwks = { |
| 107 |
"keys": [ |
| 108 |
{ |
| 109 |
"kid": "key-9000", |
| 110 |
"kty": "RSA", |
| 111 |
"alg": "RS256", |
| 112 |
"use": "sig", |
| 113 |
"n": n, |
| 114 |
"e": e, |
| 115 |
} |
| 116 |
] |
| 117 |
} |
| 118 |
# Patch PyJWT's own fetch hook rather than urllib: PyJWT 2.14 stopped |
| 119 |
# calling urllib.request.urlopen when it began rejecting redirects. |
| 120 |
mocker.patch("jwt.PyJWKClient.fetch_data", return_value=jwks) |