Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /bluesky_tiled /tests /test_authenticators.py
| import asyncio | |
| import logging | |
| import os | |
| import time | |
| from typing import Any, Tuple | |
| import httpx | |
| import pytest | |
| from cryptography.hazmat.primitives.asymmetric import rsa | |
| from fastapi import HTTPException | |
| from fastapi.security import SecurityScopes | |
| from jose import ExpiredSignatureError, jwt | |
| from jose.backends import RSAKey | |
| from respx import MockRouter | |
| from starlette.datastructures import URL, QueryParams | |
| from starlette.requests import Request | |
| from starlette.status import HTTP_401_UNAUTHORIZED | |
| from tiled.authenticators import ( | |
| EntraAuthenticator, | |
| LDAPAuthenticator, | |
| OIDCAuthenticator, | |
| ProxiedOIDCAuthenticator, | |
| ) | |
| from tiled.server.authentication import _extract_scopes, check_scopes | |
| from tiled.server.settings import Settings | |
| # Set this if there is an LDAP container running for testing. | |
| # See continuous_integration/docker-configs/ldap-docker-compose.yml | |
| TILED_TEST_LDAP = os.getenv("TILED_TEST_LDAP") | |
| # fmt: off | |
| # fmt: on | |
| def test_LDAPAuthenticator_01(use_tls, use_ssl, ldap_server_address, ldap_server_port): | |
| """ | |
| Basic test for ``LDAPAuthenticator``. | |
| TODO: The test could be extended with enabled TLS or SSL, but it requires configuration | |
| of the LDAP server. | |
| """ | |
| if not TILED_TEST_LDAP: | |
| pytest.skip("Run an LDAP container and set TILED_TEST_LDAP to run") | |
| authenticator = LDAPAuthenticator( | |
| ldap_server_address, | |
| ldap_server_port, | |
| bind_dn_template="cn={username},ou=users,dc=example,dc=org", | |
| use_tls=use_tls, | |
| use_ssl=use_ssl, | |
| ) | |
| async def testing(): | |
| assert (await authenticator.authenticate("user01", "password1")).user_name == "user01" | |
| assert (await authenticator.authenticate("user02", "password2")).user_name == "user02" | |
| assert (await authenticator.authenticate("user02a", "password2")) is None | |
| assert (await authenticator.authenticate("user02", "password2a")) is None | |
| asyncio.run(testing()) | |
| def well_known_url(base_url: str) -> str: | |
| return f"{base_url}.well-known/openid-configuration" | |
| def mock_oidc_server( | |
| respx_mock: MockRouter, | |
| well_known_url: str, | |
| well_known_response: dict[str, Any], | |
| json_web_keyset: list[dict[str, Any]], | |
| ) -> MockRouter: | |
| respx_mock.get(well_known_url).mock( | |
| return_value=httpx.Response(httpx.codes.OK, json=well_known_response) | |
| ) | |
| respx_mock.get(well_known_response["jwks_uri"]).mock( | |
| return_value=httpx.Response(httpx.codes.OK, json={"keys": json_web_keyset}) | |
| ) | |
| return respx_mock | |
| def test_oidc_authenticator_caching( | |
| mock_oidc_server: MockRouter, | |
| well_known_url: str, | |
| well_known_response: dict[str, Any], | |
| json_web_keyset: list[dict[str, Any]] | |
| ): | |
| authenticator = OIDCAuthenticator("tiled", "tiled", "secret", well_known_uri=well_known_url) | |
| assert authenticator.client_id == "tiled" | |
| assert authenticator.authorization_endpoint == well_known_response["authorization_endpoint"] | |
| assert authenticator.id_token_signing_alg_values_supported == well_known_response[ | |
| "id_token_signing_alg_values_supported" | |
| ] | |
| assert authenticator.issuer == well_known_response["issuer"] | |
| assert authenticator.jwks_uri == well_known_response["jwks_uri"] | |
| assert authenticator.token_endpoint == well_known_response["token_endpoint"] | |
| assert authenticator.device_authorization_endpoint == well_known_response["device_authorization_endpoint"] | |
| assert authenticator.end_session_endpoint == well_known_response["end_session_endpoint"] | |
| # Cached the result of .well_known, only GET once | |
| assert len(mock_oidc_server.calls) == 1 | |
| call_request = mock_oidc_server.calls[0].request | |
| assert call_request.method == "GET" | |
| assert call_request.url == well_known_url | |
| assert authenticator.keys() == json_web_keyset | |
| assert len(mock_oidc_server.calls) == 2 # Called also to jwks | |
| keys_request = mock_oidc_server.calls[1].request | |
| assert keys_request.method == "GET" | |
| assert keys_request.url == well_known_response["jwks_uri"] | |
| for _ in range(10): | |
| assert authenticator.keys() == json_web_keyset | |
| assert len(mock_oidc_server.calls) == 2 # Getting keys is cached | |
| keys_request = mock_oidc_server.calls[1].request | |
| assert keys_request.method == "GET" | |
| assert keys_request.url == well_known_response["jwks_uri"] | |
| def test_oidc_decoding( | |
| mock_oidc_server: MockRouter, | |
| well_known_url: str, | |
| issued: bool, | |
| expired: bool, | |
| keys: Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey] | |
| ): | |
| private_key, _ = keys | |
| authenticator = OIDCAuthenticator("tiled", "tiled", "secret", well_known_uri=well_known_url) | |
| access_token = token(issued, expired) | |
| encrypted_access_token = encrypted_token(access_token, private_key) | |
| if not expired: | |
| # Decode does not currently care if issued_at_time > current time | |
| assert authenticator.decode_token(encrypted_access_token) == access_token | |
| else: | |
| with pytest.raises(ExpiredSignatureError): | |
| authenticator.decode_token(encrypted_access_token) | |
| def test_entra_decoding_ignores_unmapped_scopes(caplog): | |
| def mock_decode_token(self, id_token, access_token): | |
| return { | |
| "iss": "https://login.microsoftonline.com/example-tenant/v2.0", | |
| "sub": "opaque-sub", | |
| "preferred_username": "alice@example.org", | |
| "scp": "known.scope unknown.scope", | |
| } | |
| original_decode_token = OIDCAuthenticator.decode_token | |
| OIDCAuthenticator.decode_token = mock_decode_token | |
| try: | |
| caplog.set_level(logging.WARNING) | |
| authenticator = object.__new__(EntraAuthenticator) | |
| authenticator._scopes_map = {"known.scope": ["read:metadata"]} | |
| claims = authenticator.decode_token("id-token", "access-token") | |
| assert claims["entra_sub"] == "opaque-sub" | |
| assert claims["entra_username"] == "alice@example.org" | |
| assert claims["user"] == "alice" | |
| assert claims["scope"] == "read:metadata" | |
| assert any( | |
| "Unmapped scope in 'scp': unknown.scope" in record.message | |
| for record in caplog.records | |
| ) | |
| finally: | |
| OIDCAuthenticator.decode_token = original_decode_token | |
| def keys() -> Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]: | |
| # Key generated just for these tests | |
| private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) | |
| public_key = private_key.public_key() | |
| return (private_key, public_key) | |
| def json_web_keyset(keys: Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]) -> list[dict[str, Any]]: | |
| _, public_key = keys | |
| return [ | |
| RSAKey(key=public_key, algorithm="RS256").to_dict() | |
| ] | |
| def token(issued: bool, expired: bool) -> dict[str, str]: | |
| now = time.time() | |
| dummy_token = { | |
| "aud": "tiled", | |
| "exp": (now - 1500) if expired else (now + 1500), | |
| "iat": (now - 1500) if issued else (now + 1500), | |
| "iss": "https://example.com/realms/example", | |
| "sub": "Jane Doe", | |
| } | |
| return dummy_token | |
| def encrypted_token(token: dict[str, str], private_key: rsa.RSAPrivateKey) -> str: | |
| return jwt.encode( | |
| token, | |
| key=private_key, | |
| algorithm="RS256", | |
| headers={"kid": "secret"}, | |
| ) | |
| async def test_proxied_oidc_token_retrieval(well_known_url: str, mock_oidc_server: MockRouter): | |
| authenticator = ProxiedOIDCAuthenticator("tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli") | |
| test_request = httpx.Request("GET", "http://example.com", headers={ | |
| "Authorization": "bearer FOO" | |
| }) | |
| assert "FOO" == await authenticator.oauth2_schema(test_request) | |
| def create_mock_OIDC_request(query_params=None): | |
| """Helper function to create a realistic request object for testing.""" | |
| if query_params is None: | |
| query_params = {} | |
| class MockRequest: | |
| def __init__(self, query_params): | |
| self.query_params = QueryParams(query_params) | |
| self.scope = { | |
| "type": "http", | |
| "scheme": "http", | |
| "server": ("localhost", 8000), | |
| "path": "/api/v1/auth/provider/orcid/code", | |
| "headers": [] | |
| } | |
| self.headers = {"host": "localhost:8000"} | |
| self.url = URL("http://localhost:8000/api/v1/auth/provider/orcid/code") | |
| return MockRequest(query_params) | |
| async def test_OIDCAuthenticator_mock( | |
| mock_oidc_server: MockRouter, | |
| well_known_url: str, | |
| well_known_response: dict[str, Any], | |
| monkeypatch | |
| ): | |
| """ | |
| Test OIDCAuthenticator with mocked external dependencies using respx. | |
| """ | |
| # Mock JWT token payload | |
| mock_jwt_payload = { | |
| "sub": "0009-0008-8698-7745", | |
| "aud": "APP-TEST-CLIENT-ID", | |
| "iss": well_known_response["issuer"], | |
| "exp": 9999999999, # Far future | |
| "iat": 1000000000, | |
| "given_name": "Test User" | |
| } | |
| # Add token exchange endpoint to existing mock_oidc_server | |
| mock_oidc_server.post(well_known_response["token_endpoint"]).mock( | |
| return_value=httpx.Response(200, json={ | |
| "access_token": "mock-access-token", | |
| "id_token": "mock-id-token", | |
| "token_type": "bearer" | |
| }) | |
| ) | |
| authenticator = OIDCAuthenticator( | |
| audience="APP-TEST-CLIENT-ID", | |
| client_id="APP-TEST-CLIENT-ID", | |
| client_secret="test-secret", | |
| well_known_uri=well_known_url # Use the fixture | |
| ) | |
| mock_request = create_mock_OIDC_request({"code": "test-auth-code"}) | |
| def mock_jwt_decode(*args, **kwargs): | |
| return mock_jwt_payload | |
| def mock_jwk_construct(*args, **kwargs): | |
| class MockJWK: | |
| pass | |
| return MockJWK() | |
| monkeypatch.setattr("jose.jwt.decode", mock_jwt_decode) | |
| monkeypatch.setattr("jose.jwk.construct", mock_jwk_construct) | |
| # Test authentication | |
| user_session = await authenticator.authenticate(mock_request) | |
| assert user_session is not None | |
| assert user_session.user_name == "0009-0008-8698-7745" | |
| async def test_OIDCAuthenticator_missing_code_parameter(well_known_url: str): | |
| """ | |
| Test OIDCAuthenticator when the 'code' query parameter is missing. | |
| """ | |
| authenticator = OIDCAuthenticator( | |
| audience="APP-TEST-CLIENT-ID", | |
| client_id="APP-TEST-CLIENT-ID", | |
| client_secret="test-secret", | |
| well_known_uri=well_known_url # Use the fixture | |
| ) | |
| mock_request = create_mock_OIDC_request({}) # Empty, no code parameter | |
| result = await authenticator.authenticate(mock_request) | |
| assert result is None | |
| async def test_OIDCAuthenticator_token_exchange_failure( | |
| well_known_url: str, mock_oidc_server, well_known_response | |
| ): | |
| """ | |
| Test OIDCAuthenticator when token exchange fails. | |
| """ | |
| # Mock the token exchange endpoint to return an error | |
| mock_oidc_server.post(well_known_response["token_endpoint"]).mock( | |
| return_value=httpx.Response(400, json={ | |
| 'error': 'invalid_client', | |
| 'error_description': 'Client not found: APP-TEST-CLIENT-ID' | |
| }) | |
| ) | |
| authenticator = OIDCAuthenticator( | |
| audience="APP-TEST-CLIENT-ID", | |
| client_id="APP-TEST-CLIENT-ID", | |
| client_secret="test-secret", | |
| well_known_uri=well_known_url | |
| ) | |
| mock_request = create_mock_OIDC_request({"code": "invalid-code"}) | |
| # This should return None, not raise an exception | |
| result = await authenticator.authenticate(mock_request) | |
| assert result is None | |
| async def test_ProxiedOIDCAuthenticator_requires_scopes(mock_oidc_server, well_known_url): | |
| authenticator = ProxiedOIDCAuthenticator("tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes=["read:data"]) | |
| test_request = Request(scope={"type": "http", "scheme": "http", "headers": [(b"host", b"testserver")]}, ) | |
| settings = Settings(authenticator=authenticator) | |
| security_scopes = SecurityScopes(scopes=["create:apikeys"]) | |
| with pytest.raises(HTTPException) as exception: | |
| await check_scopes(request=test_request, settings=settings, scopes=["read:data"], | |
| security_scopes=security_scopes) | |
| assert exception.value.status_code == HTTP_401_UNAUTHORIZED | |
| async def test_ProxiedOIDCAuthenticator_scoped_api_key_passes(mock_oidc_server, well_known_url): | |
| # Regression test: a request (e.g. from a scope-restricted API key) that | |
| # carries only a *subset* of the authenticator's full scope universe must | |
| # succeed as long as it holds the scopes the endpoint actually requires. | |
| # Previously check_scopes required the request to carry *all* of the | |
| # authenticator's scopes (the union of every scope in scopes_map), which | |
| # broke scoped API keys under a ProxiedOIDC/Entra authenticator. | |
| authenticator = ProxiedOIDCAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes_map={"access_as_user": [ | |
| "read:metadata", "read:data", "create:node", "write:metadata", | |
| "write:data", "delete:revision", "delete:node", "create:apikeys", | |
| "revoke:apikeys", | |
| ]}, | |
| ) | |
| test_request = Request(scope={"type": "http", "scheme": "http", "headers": [(b"host", b"testserver")]}, ) | |
| settings = Settings(authenticator=authenticator) | |
| # The /metadata/ write endpoint requires these scopes; the restricted key | |
| # has exactly these (and not the full universe, e.g. no read:data). | |
| request_scopes = ["write:data", "create:node", "read:metadata", "write:metadata"] | |
| security_scopes = SecurityScopes(scopes=["write:metadata", "create:node"]) | |
| # Should NOT raise. | |
| await check_scopes(request=test_request, settings=settings, scopes=request_scopes, | |
| security_scopes=security_scopes) | |
| # Sanity check: missing a required endpoint scope still fails. | |
| security_scopes = SecurityScopes(scopes=["read:data"]) | |
| with pytest.raises(HTTPException) as exception: | |
| await check_scopes(request=test_request, settings=settings, scopes=request_scopes, | |
| security_scopes=security_scopes) | |
| assert exception.value.status_code == HTTP_401_UNAUTHORIZED | |
| def test_ProxiedOIDCAuthenticator_scopes_explicit(mock_oidc_server, well_known_url): | |
| # With no scopes_map, explicit scopes are returned as-is. | |
| authenticator = ProxiedOIDCAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes=["read:data"], | |
| ) | |
| assert authenticator.scopes == ["read:data"] | |
| def test_ProxiedOIDCAuthenticator_scopes_union(mock_oidc_server, well_known_url): | |
| # When both are provided, scopes is the (sorted) union of explicit scopes | |
| # and the Tiled scopes granted via scopes_map. | |
| authenticator = ProxiedOIDCAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes=["read:data"], | |
| scopes_map={"provider.write": ["write:data"]}, | |
| ) | |
| assert authenticator.scopes == ["read:data", "write:data"] | |
| def test_ProxiedOIDCAuthenticator_scopes_derived_from_scopes_map(mock_oidc_server, well_known_url): | |
| # With no explicit scopes, they are derived from the values of scopes_map. | |
| authenticator = ProxiedOIDCAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes_map={"provider.read": ["read:metadata", "read:data"]}, | |
| ) | |
| assert set(authenticator.scopes) == {"read:metadata", "read:data"} | |
| def test_ProxiedOIDCAuthenticator_scopes_default_empty(mock_oidc_server, well_known_url): | |
| # With neither scopes nor scopes_map, scopes is an empty list. | |
| authenticator = ProxiedOIDCAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| ) | |
| assert authenticator.scopes == [] | |
| def test_EntraAuthenticator_scopes_derived_from_scopes_map(mock_oidc_server, well_known_url): | |
| # EntraAuthenticator no longer defines its own scopes property; it inherits | |
| # the scopes_map-derived behavior from ProxiedOIDCAuthenticator. | |
| authenticator = EntraAuthenticator( | |
| "tiled", "tiled", well_known_url, device_flow_client_id="tiled-cli", | |
| scopes_map={"provider.read": ["read:metadata"], "provider.write": ["write:data"]}, | |
| ) | |
| assert set(authenticator.scopes) == {"read:metadata", "write:data"} | |
| def test_ProxiedOIDCAuthenticator_decode_token_maps_scopes(): | |
| # scopes_map translates provider scopes ("scp") into Tiled scopes ("scope"). | |
| def mock_decode_token(self, id_token, access_token=None): | |
| return {"sub": "abc", "scp": "provider.read provider.unknown"} | |
| original = OIDCAuthenticator.decode_token | |
| OIDCAuthenticator.decode_token = mock_decode_token | |
| try: | |
| authenticator = object.__new__(ProxiedOIDCAuthenticator) | |
| authenticator._scopes_map = {"provider.read": ["read:metadata"]} | |
| claims = authenticator.decode_token("id-token", "access-token") | |
| # Mapped scope is translated; unmapped provider scope is dropped. | |
| assert claims["scope"] == "read:metadata" | |
| finally: | |
| OIDCAuthenticator.decode_token = original | |
| def test_ProxiedOIDCAuthenticator_decode_token_no_map_leaves_scopes(): | |
| # Without scopes_map, the token's native scopes are left untouched. | |
| def mock_decode_token(self, id_token, access_token=None): | |
| return {"sub": "abc", "scp": ["read:data"]} | |
| original = OIDCAuthenticator.decode_token | |
| OIDCAuthenticator.decode_token = mock_decode_token | |
| try: | |
| authenticator = object.__new__(ProxiedOIDCAuthenticator) | |
| authenticator._scopes_map = {} | |
| claims = authenticator.decode_token("id-token", "access-token") | |
| assert "scope" not in claims | |
| assert claims["scp"] == ["read:data"] | |
| finally: | |
| OIDCAuthenticator.decode_token = original | |
| def test_ProxiedOIDCAuthenticator_decode_token_all_unmapped_empty_scope(): | |
| # When scopes_map is configured but nothing maps, "scope" is set to an empty | |
| # string (not left unset) so downstream extraction does not fall back to the | |
| # raw, unmapped provider "scp" claim. | |
| def mock_decode_token(self, id_token, access_token=None): | |
| return {"sub": "abc", "scp": "provider.unknown"} | |
| original = OIDCAuthenticator.decode_token | |
| OIDCAuthenticator.decode_token = mock_decode_token | |
| try: | |
| authenticator = object.__new__(ProxiedOIDCAuthenticator) | |
| authenticator._scopes_map = {"provider.read": ["read:metadata"]} | |
| claims = authenticator.decode_token("id-token", "access-token") | |
| assert claims["scope"] == "" | |
| finally: | |
| OIDCAuthenticator.decode_token = original | |
| def test_extract_scopes_empty_scope_is_empty_set(): | |
| # An empty "scope" string must yield an empty set, not {""}. | |
| assert _extract_scopes({"scope": ""}, None) == set() | |
| assert _extract_scopes( | |
| {"scope": "read:data write:data"}, None | |
| ) == {"read:data", "write:data"} | |
| # "scope" (even empty) takes precedence over "scp" and is not overridden. | |
| assert _extract_scopes({"scope": "", "scp": "read:data"}, None) == set() | |
Xet Storage Details
- Size:
- 19.8 kB
- Xet hash:
- 95b7828020ad576f7e24ab91ee52a89fc7d8a1b663f782ccabf2cc92eee72ec6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.