-
Notifications
You must be signed in to change notification settings - Fork 557
feat(OIDC): Add OIDC token exchange endpoint #8043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa7dc3f
feat(trust-relationships): Add OIDC token exchange endpoint
khvn26 4efb50a
fix(OIDC): Token exchange rejects issuers with a trailing slash
khvn26 72bbaab
chore: Update documentation artefacts
flagsmith-engineering[bot] 3820909
chore(OIDC): Explain token exchange verification steps
khvn26 80045ca
fix(OIDC): Trailing-slash issuer registered via API never matches
khvn26 3c07744
fix(OIDC): JWKS retrieval waits 30s regardless of discovery timeout
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
api/tests/integration/trust_relationships/test_token_exchange.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| from rest_framework import status | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from tests.test_helpers import OIDCIssuerStub | ||
|
|
||
|
|
||
| def test_token_exchange__matching_token__returns_usable_access_token( | ||
| machine_client: APIClient, | ||
| organisation: int, | ||
| trust_relationship: int, | ||
| oidc_issuer: OIDCIssuerStub, | ||
| ) -> None: | ||
| # Given | ||
| token = oidc_issuer.sign_token( | ||
| aud="https://github.com/Flagsmith", | ||
| sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", | ||
| repository="Flagsmith/flagsmith", | ||
| ) | ||
|
|
||
| # When | ||
| response = machine_client.post( | ||
| "/api/v1/auth/oidc/token/", data={"token": token}, format="json" | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
| response_json = response.json() | ||
| assert response_json["token_type"] == "Bearer" | ||
| assert response_json["expires_in"] == 3600 | ||
|
|
||
| # And the minted token authenticates against the admin API | ||
| machine_client.credentials( | ||
| HTTP_AUTHORIZATION=f"Bearer {response_json['access_token']}" | ||
| ) | ||
| organisations_response = machine_client.get("/api/v1/organisations/") | ||
| assert organisations_response.status_code == status.HTTP_200_OK | ||
| assert organisations_response.json()["results"][0]["id"] == organisation | ||
|
|
||
|
|
||
| def test_token_exchange__deleted_trust_relationship__access_token_rejected( | ||
| machine_client: APIClient, | ||
| admin_client: APIClient, | ||
| organisation: int, | ||
| trust_relationship: int, | ||
| oidc_issuer: OIDCIssuerStub, | ||
| ) -> None: | ||
| # Given | ||
| token = oidc_issuer.sign_token( | ||
| aud="https://github.com/Flagsmith", | ||
| sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", | ||
| repository="Flagsmith/flagsmith", | ||
| ) | ||
| access_token = machine_client.post( | ||
| "/api/v1/auth/oidc/token/", data={"token": token}, format="json" | ||
| ).json()["access_token"] | ||
| admin_client.delete( | ||
| f"/api/v1/organisations/{organisation}" | ||
| f"/trust-relationships/{trust_relationship}/" | ||
| ) | ||
|
|
||
| # When | ||
| machine_client.credentials(HTTP_AUTHORIZATION=f"Bearer {access_token}") | ||
| response = machine_client.get("/api/v1/organisations/") | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | ||
|
|
||
|
|
||
| def test_token_exchange__minted_token__cannot_manage_machine_credentials( | ||
| machine_client: APIClient, | ||
| organisation: int, | ||
| trust_relationship: int, | ||
| oidc_issuer: OIDCIssuerStub, | ||
| ) -> None: | ||
| # Given | ||
| token = oidc_issuer.sign_token( | ||
| aud="https://github.com/Flagsmith", | ||
| sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", | ||
| repository="Flagsmith/flagsmith", | ||
| ) | ||
| access_token = machine_client.post( | ||
| "/api/v1/auth/oidc/token/", data={"token": token}, format="json" | ||
| ).json()["access_token"] | ||
| machine_client.credentials(HTTP_AUTHORIZATION=f"Bearer {access_token}") | ||
|
|
||
| # When | ||
| master_api_keys_response = machine_client.post( | ||
| f"/api/v1/organisations/{organisation}/master-api-keys/", | ||
| data={"name": "sneaky", "organisation": organisation}, | ||
| ) | ||
| trust_relationships_response = machine_client.get( | ||
| f"/api/v1/organisations/{organisation}/trust-relationships/" | ||
| ) | ||
|
|
||
| # Then | ||
| assert master_api_keys_response.status_code == status.HTTP_401_UNAUTHORIZED | ||
| assert trust_relationships_response.status_code == status.HTTP_401_UNAUTHORIZED | ||
|
|
||
|
|
||
| def test_token_exchange__no_matching_trust_relationship__returns_401( | ||
| machine_client: APIClient, | ||
| trust_relationship: int, | ||
| oidc_issuer: OIDCIssuerStub, | ||
| ) -> None: | ||
| # Given | ||
| token = oidc_issuer.sign_token( | ||
| aud="https://github.com/Flagsmith", | ||
| sub="repo:SomeoneElse/repo:ref:refs/heads/main", | ||
| repository="SomeoneElse/repo", | ||
| ) | ||
|
|
||
| # When | ||
| response = machine_client.post( | ||
| "/api/v1/auth/oidc/token/", data={"token": token}, format="json" | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | ||
|
|
||
|
|
||
| def test_token_exchange__multi_audience_token_matching_multiple__returns_401( | ||
| machine_client: APIClient, | ||
| admin_client: APIClient, | ||
| organisation: int, | ||
| trust_relationship: int, | ||
| oidc_issuer: OIDCIssuerStub, | ||
| ) -> None: | ||
| # Given: a second trust relationship under a different audience, and a | ||
| # token listing both audiences | ||
| create_response = admin_client.post( | ||
| f"/api/v1/organisations/{organisation}/trust-relationships/", | ||
| data={ | ||
| "name": "GitHub Actions (CI)", | ||
| "issuer": "https://token.actions.githubusercontent.com", | ||
| "audience": "flagsmith-ci", | ||
| "is_admin": True, | ||
| }, | ||
| format="json", | ||
| ) | ||
| assert create_response.status_code == status.HTTP_201_CREATED | ||
| token = oidc_issuer.sign_token( | ||
| aud=["https://github.com/Flagsmith", "flagsmith-ci"], | ||
| sub="repo:Flagsmith/flagsmith:ref:refs/heads/main", | ||
| repository="Flagsmith/flagsmith", | ||
| ) | ||
|
|
||
| # When | ||
| response = machine_client.post( | ||
| "/api/v1/auth/oidc/token/", data={"token": token}, format="json" | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | ||
| assert response.json()["detail"] == ( | ||
| "Token audience matches multiple trust relationships." | ||
| ) | ||
|
|
||
|
|
||
| def test_token_exchange__missing_token__returns_400( | ||
| machine_client: APIClient, | ||
| ) -> None: | ||
| # Given / When | ||
| response = machine_client.post("/api/v1/auth/oidc/token/", data={}, format="json") | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_400_BAD_REQUEST |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.