|
| 1 | +from rest_framework import status |
| 2 | +from rest_framework.test import APIClient |
| 3 | + |
| 4 | +from tests.test_helpers import OIDCIssuerStub |
| 5 | + |
| 6 | + |
| 7 | +def test_token_exchange__matching_token__returns_usable_access_token( |
| 8 | + machine_client: APIClient, |
| 9 | + organisation: int, |
| 10 | + trust_relationship: int, |
| 11 | + oidc_issuer: OIDCIssuerStub, |
| 12 | +) -> None: |
| 13 | + # Given |
| 14 | + token = oidc_issuer.sign_token( |
| 15 | + aud="https://github.com/Flagsmith", |
| 16 | + sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", |
| 17 | + repository="Flagsmith/flagsmith", |
| 18 | + ) |
| 19 | + |
| 20 | + # When |
| 21 | + response = machine_client.post( |
| 22 | + "/api/v1/auth/oidc/token/", data={"token": token}, format="json" |
| 23 | + ) |
| 24 | + |
| 25 | + # Then |
| 26 | + assert response.status_code == status.HTTP_200_OK |
| 27 | + response_json = response.json() |
| 28 | + assert response_json["token_type"] == "Bearer" |
| 29 | + assert response_json["expires_in"] == 3600 |
| 30 | + |
| 31 | + # And the minted token authenticates against the admin API |
| 32 | + machine_client.credentials( |
| 33 | + HTTP_AUTHORIZATION=f"Bearer {response_json['access_token']}" |
| 34 | + ) |
| 35 | + organisations_response = machine_client.get("/api/v1/organisations/") |
| 36 | + assert organisations_response.status_code == status.HTTP_200_OK |
| 37 | + assert organisations_response.json()["results"][0]["id"] == organisation |
| 38 | + |
| 39 | + |
| 40 | +def test_token_exchange__deleted_trust_relationship__access_token_rejected( |
| 41 | + machine_client: APIClient, |
| 42 | + admin_client: APIClient, |
| 43 | + organisation: int, |
| 44 | + trust_relationship: int, |
| 45 | + oidc_issuer: OIDCIssuerStub, |
| 46 | +) -> None: |
| 47 | + # Given |
| 48 | + token = oidc_issuer.sign_token( |
| 49 | + aud="https://github.com/Flagsmith", |
| 50 | + sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", |
| 51 | + repository="Flagsmith/flagsmith", |
| 52 | + ) |
| 53 | + access_token = machine_client.post( |
| 54 | + "/api/v1/auth/oidc/token/", data={"token": token}, format="json" |
| 55 | + ).json()["access_token"] |
| 56 | + admin_client.delete( |
| 57 | + f"/api/v1/organisations/{organisation}" |
| 58 | + f"/trust-relationships/{trust_relationship}/" |
| 59 | + ) |
| 60 | + |
| 61 | + # When |
| 62 | + machine_client.credentials(HTTP_AUTHORIZATION=f"Bearer {access_token}") |
| 63 | + response = machine_client.get("/api/v1/organisations/") |
| 64 | + |
| 65 | + # Then |
| 66 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 67 | + |
| 68 | + |
| 69 | +def test_token_exchange__minted_token__cannot_manage_machine_credentials( |
| 70 | + machine_client: APIClient, |
| 71 | + organisation: int, |
| 72 | + trust_relationship: int, |
| 73 | + oidc_issuer: OIDCIssuerStub, |
| 74 | +) -> None: |
| 75 | + # Given |
| 76 | + token = oidc_issuer.sign_token( |
| 77 | + aud="https://github.com/Flagsmith", |
| 78 | + sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", |
| 79 | + repository="Flagsmith/flagsmith", |
| 80 | + ) |
| 81 | + access_token = machine_client.post( |
| 82 | + "/api/v1/auth/oidc/token/", data={"token": token}, format="json" |
| 83 | + ).json()["access_token"] |
| 84 | + machine_client.credentials(HTTP_AUTHORIZATION=f"Bearer {access_token}") |
| 85 | + |
| 86 | + # When |
| 87 | + master_api_keys_response = machine_client.post( |
| 88 | + f"/api/v1/organisations/{organisation}/master-api-keys/", |
| 89 | + data={"name": "sneaky", "organisation": organisation}, |
| 90 | + ) |
| 91 | + trust_relationships_response = machine_client.get( |
| 92 | + f"/api/v1/organisations/{organisation}/trust-relationships/" |
| 93 | + ) |
| 94 | + |
| 95 | + # Then |
| 96 | + assert master_api_keys_response.status_code == status.HTTP_401_UNAUTHORIZED |
| 97 | + assert trust_relationships_response.status_code == status.HTTP_401_UNAUTHORIZED |
| 98 | + |
| 99 | + |
| 100 | +def test_token_exchange__no_matching_trust_relationship__returns_401( |
| 101 | + machine_client: APIClient, |
| 102 | + trust_relationship: int, |
| 103 | + oidc_issuer: OIDCIssuerStub, |
| 104 | +) -> None: |
| 105 | + # Given |
| 106 | + token = oidc_issuer.sign_token( |
| 107 | + aud="https://github.com/Flagsmith", |
| 108 | + sub="repo:SomeoneElse/repo:ref:refs/heads/main", |
| 109 | + repository="SomeoneElse/repo", |
| 110 | + ) |
| 111 | + |
| 112 | + # When |
| 113 | + response = machine_client.post( |
| 114 | + "/api/v1/auth/oidc/token/", data={"token": token}, format="json" |
| 115 | + ) |
| 116 | + |
| 117 | + # Then |
| 118 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 119 | + |
| 120 | + |
| 121 | +def test_token_exchange__multi_audience_token_matching_multiple__returns_401( |
| 122 | + machine_client: APIClient, |
| 123 | + admin_client: APIClient, |
| 124 | + organisation: int, |
| 125 | + trust_relationship: int, |
| 126 | + oidc_issuer: OIDCIssuerStub, |
| 127 | +) -> None: |
| 128 | + # Given: a second trust relationship under a different audience, and a |
| 129 | + # token listing both audiences |
| 130 | + create_response = admin_client.post( |
| 131 | + f"/api/v1/organisations/{organisation}/trust-relationships/", |
| 132 | + data={ |
| 133 | + "name": "GitHub Actions (CI)", |
| 134 | + "issuer": "https://token.actions.githubusercontent.com", |
| 135 | + "audience": "flagsmith-ci", |
| 136 | + "is_admin": True, |
| 137 | + }, |
| 138 | + format="json", |
| 139 | + ) |
| 140 | + assert create_response.status_code == status.HTTP_201_CREATED |
| 141 | + token = oidc_issuer.sign_token( |
| 142 | + aud=["https://github.com/Flagsmith", "flagsmith-ci"], |
| 143 | + sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", |
| 144 | + repository="Flagsmith/flagsmith", |
| 145 | + ) |
| 146 | + |
| 147 | + # When |
| 148 | + response = machine_client.post( |
| 149 | + "/api/v1/auth/oidc/token/", data={"token": token}, format="json" |
| 150 | + ) |
| 151 | + |
| 152 | + # Then |
| 153 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 154 | + assert response.json()["detail"] == ( |
| 155 | + "Token audience matches multiple trust relationships." |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def test_token_exchange__missing_token__returns_400( |
| 160 | + machine_client: APIClient, |
| 161 | +) -> None: |
| 162 | + # Given / When |
| 163 | + response = machine_client.post("/api/v1/auth/oidc/token/", data={}, format="json") |
| 164 | + |
| 165 | + # Then |
| 166 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
0 commit comments