|
3 | 3 | import jwt |
4 | 4 | import pytest |
5 | 5 | from fastapi.testclient import TestClient |
| 6 | +from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer |
| 7 | +from fastapi_azure_auth.auth import AzureAuthorizationCodeBearerBase |
| 8 | +from fastapi_azure_auth.user import User |
| 9 | +from pydantic import ValidationError |
| 10 | +from pytest import MonkeyPatch |
| 11 | + |
| 12 | +from isar.apis.security.authentication import build_azure_scheme |
| 13 | +from isar.config.settings import settings |
| 14 | + |
| 15 | + |
| 16 | +def advertised_scopes(scheme: AzureAuthorizationCodeBearerBase) -> dict[str, str]: |
| 17 | + """Scopes offered by Swagger's Authorize button, for the given scheme.""" |
| 18 | + return scheme.oauth.model.flows.authorizationCode.scopes |
6 | 19 |
|
7 | 20 |
|
8 | 21 | def stub_access_token() -> str: |
@@ -30,3 +43,112 @@ def test_authentication( |
30 | 43 | ) |
31 | 44 |
|
32 | 45 | assert response.status_code == expected_status_code |
| 46 | + |
| 47 | + |
| 48 | +class TestBuildAzureScheme: |
| 49 | + def test_defaults_to_single_tenant_azure_scheme( |
| 50 | + self, monkeypatch: MonkeyPatch |
| 51 | + ) -> None: |
| 52 | + monkeypatch.setattr(settings, "OPENID_CONFIG_URL", None) |
| 53 | + |
| 54 | + scheme = build_azure_scheme() |
| 55 | + |
| 56 | + assert isinstance(scheme, SingleTenantAzureAuthorizationCodeBearer) |
| 57 | + assert scheme.openid_config.config_url is None |
| 58 | + assert scheme.openid_config.tenant_id == settings.AZURE_TENANT_ID |
| 59 | + assert scheme.app_client_id == settings.AZURE_CLIENT_ID |
| 60 | + |
| 61 | + def test_openid_config_url_is_honoured(self, monkeypatch: MonkeyPatch) -> None: |
| 62 | + config_url = ( |
| 63 | + "http://keycloak:8080/realms/robotics/.well-known/openid-configuration" |
| 64 | + ) |
| 65 | + authorization_url = ( |
| 66 | + "http://keycloak:8080/realms/robotics/protocol/openid-connect/auth" |
| 67 | + ) |
| 68 | + token_url = "http://keycloak:8080/realms/robotics/protocol/openid-connect/token" |
| 69 | + |
| 70 | + monkeypatch.setattr(settings, "OPENID_CONFIG_URL", config_url) |
| 71 | + monkeypatch.setattr(settings, "OPENAPI_AUTHORIZATION_URL", authorization_url) |
| 72 | + monkeypatch.setattr(settings, "OPENAPI_TOKEN_URL", token_url) |
| 73 | + |
| 74 | + scheme = build_azure_scheme() |
| 75 | + |
| 76 | + assert not isinstance(scheme, SingleTenantAzureAuthorizationCodeBearer) |
| 77 | + assert isinstance(scheme, AzureAuthorizationCodeBearerBase) |
| 78 | + assert scheme.openid_config.config_url == config_url |
| 79 | + assert scheme.authorization_url == authorization_url |
| 80 | + assert scheme.token_url == token_url |
| 81 | + assert scheme.app_client_id == settings.AZURE_CLIENT_ID |
| 82 | + assert scheme.validate_iss is True |
| 83 | + |
| 84 | + def test_openapi_urls_fall_back_to_azure_when_unset( |
| 85 | + self, monkeypatch: MonkeyPatch |
| 86 | + ) -> None: |
| 87 | + monkeypatch.setattr( |
| 88 | + settings, |
| 89 | + "OPENID_CONFIG_URL", |
| 90 | + "http://keycloak:8080/realms/robotics/.well-known/openid-configuration", |
| 91 | + ) |
| 92 | + monkeypatch.setattr(settings, "OPENAPI_AUTHORIZATION_URL", None) |
| 93 | + monkeypatch.setattr(settings, "OPENAPI_TOKEN_URL", None) |
| 94 | + |
| 95 | + scheme = build_azure_scheme() |
| 96 | + |
| 97 | + assert scheme.authorization_url is not None |
| 98 | + assert settings.AZURE_TENANT_ID in scheme.authorization_url |
| 99 | + |
| 100 | + def test_scope_defaults_to_the_entra_shaped_scope( |
| 101 | + self, monkeypatch: MonkeyPatch |
| 102 | + ) -> None: |
| 103 | + monkeypatch.setattr(settings, "OPENID_SCOPE", None) |
| 104 | + |
| 105 | + scheme = build_azure_scheme() |
| 106 | + |
| 107 | + expected = f"api://{settings.AZURE_CLIENT_ID}/user_impersonation" |
| 108 | + assert advertised_scopes(scheme) == {expected: "user_impersonation"} |
| 109 | + |
| 110 | + def test_openid_scope_is_honoured(self, monkeypatch: MonkeyPatch) -> None: |
| 111 | + monkeypatch.setattr(settings, "OPENID_SCOPE", "isar-api") |
| 112 | + |
| 113 | + scheme = build_azure_scheme() |
| 114 | + |
| 115 | + assert advertised_scopes(scheme) == {"isar-api": "isar-api"} |
| 116 | + assert scheme.app_client_id == settings.AZURE_CLIENT_ID |
| 117 | + |
| 118 | + |
| 119 | +class TestAudienceClaimShape: |
| 120 | + """Pin the audience shapes ISAR accepts. |
| 121 | +
|
| 122 | + ``fastapi_azure_auth`` declares ``aud`` as a plain ``str``, so the array form |
| 123 | + RFC 7519 also permits is rejected with an opaque 401. Asserted here so that a |
| 124 | + dependency upgrade lifting the restriction is noticed. |
| 125 | + """ |
| 126 | + |
| 127 | + def test_string_audience_is_accepted(self) -> None: |
| 128 | + user = User( |
| 129 | + aud=settings.AZURE_CLIENT_ID, |
| 130 | + claims={}, |
| 131 | + access_token="", |
| 132 | + iss="", |
| 133 | + sub="", |
| 134 | + exp=0, |
| 135 | + iat=0, |
| 136 | + nbf=0, |
| 137 | + ver="2.0", |
| 138 | + ) |
| 139 | + |
| 140 | + assert user.aud == settings.AZURE_CLIENT_ID |
| 141 | + |
| 142 | + def test_array_audience_is_rejected(self) -> None: |
| 143 | + with pytest.raises(ValidationError): |
| 144 | + User( |
| 145 | + aud=[settings.AZURE_CLIENT_ID, "another-audience"], |
| 146 | + claims={}, |
| 147 | + access_token="", |
| 148 | + iss="", |
| 149 | + sub="", |
| 150 | + exp=0, |
| 151 | + iat=0, |
| 152 | + nbf=0, |
| 153 | + ver="2.0", |
| 154 | + ) |
0 commit comments