-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add On-Behalf-Of token exchange support #122
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,38 @@ | ||
| from auth0_api_python import ( | ||
| ApiError, | ||
| CacheAdapter, | ||
| ConfigurationError, | ||
| DomainsResolver, | ||
| DomainsResolverContext, | ||
| DomainsResolverError, | ||
| GetTokenByExchangeProfileError, | ||
| InMemoryCache, | ||
| OnBehalfOfTokenResult, | ||
| get_current_actor, | ||
| get_delegation_chain, | ||
|
nandan-bhat marked this conversation as resolved.
|
||
| ) | ||
| from auth0_api_python.errors import ( | ||
| BaseAuthError, | ||
| MissingRequiredArgumentError, | ||
| VerifyAccessTokenError, | ||
| ) | ||
|
|
||
| from .fast_api_client import Auth0FastAPI | ||
|
|
||
| __all__ = [ | ||
| "ApiError", | ||
| "Auth0FastAPI", | ||
| "BaseAuthError", | ||
| "CacheAdapter", | ||
| "ConfigurationError", | ||
| "DomainsResolver", | ||
| "DomainsResolverContext", | ||
| "DomainsResolverError", | ||
| "GetTokenByExchangeProfileError", | ||
| "InMemoryCache", | ||
| "MissingRequiredArgumentError", | ||
| "OnBehalfOfTokenResult", | ||
| "VerifyAccessTokenError", | ||
| "get_current_actor", | ||
| "get_delegation_chain", | ||
| ] | ||
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,139 @@ | ||
| """ | ||
| Tests for the On-Behalf-Of (OBO) surface this plugin owns: the require_auth() -> | ||
| pull the verified token -> exchange flow through a real FastAPI route, and the | ||
| re-exports the plugin adds on top of auth0-api-python. | ||
|
|
||
| The exchange, the form well-formedness, the Basic auth encoding, and the act-claim | ||
| parsing are exercised in auth0-api-python's own suite (test_api_client.py, test_act.py), | ||
| so they are not repeated here. | ||
| """ | ||
| import base64 | ||
| import urllib.parse | ||
|
|
||
| import pytest | ||
| from fastapi import Depends, FastAPI, Request | ||
| from fastapi.testclient import TestClient | ||
| from pytest_httpx import HTTPXMock | ||
|
|
||
| from fastapi_plugin import ( | ||
| ApiError, | ||
| Auth0FastAPI, | ||
| BaseAuthError, | ||
| GetTokenByExchangeProfileError, | ||
| MissingRequiredArgumentError, | ||
| OnBehalfOfTokenResult, | ||
| VerifyAccessTokenError, | ||
| get_current_actor, | ||
| get_delegation_chain, | ||
| ) | ||
|
|
||
| from .test_utils import generate_token | ||
|
|
||
| TOKEN_ENDPOINT = "https://auth0.local/oauth/token" | ||
|
|
||
|
|
||
| def _setup_obo_mocks(httpx_mock: HTTPXMock): | ||
| """OIDC discovery (with a token_endpoint), JWKS, and the token-exchange response.""" | ||
| httpx_mock.add_response( | ||
| method="GET", | ||
| url="https://auth0.local/.well-known/openid-configuration", | ||
| json={ | ||
| "issuer": "https://auth0.local/", | ||
| "jwks_uri": "https://auth0.local/.well-known/jwks.json", | ||
| "token_endpoint": TOKEN_ENDPOINT, | ||
| }, | ||
| ) | ||
| from .conftest import PUBLIC_DPOP_JWK, RSA_PUBLIC_KEY | ||
| httpx_mock.add_response( | ||
| method="GET", | ||
| url="https://auth0.local/.well-known/jwks.json", | ||
| json={"keys": [RSA_PUBLIC_KEY, PUBLIC_DPOP_JWK]}, | ||
| ) | ||
| httpx_mock.add_response( | ||
| method="POST", | ||
| url=TOKEN_ENDPOINT, | ||
| json={ | ||
| "access_token": "obo-access-token", | ||
| "expires_in": 3600, | ||
| "scope": "calendar:read", | ||
| "token_type": "Bearer", | ||
| "issued_token_type": "urn:ietf:params:oauth:token-type:access_token", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # The owned flow: require_auth() -> pull verified token -> exchange, via a route | ||
| # ============================================================================= | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_documented_route_verifies_then_exchanges(httpx_mock: HTTPXMock): | ||
| """A protected route pulls the verified token and exchanges it on behalf of the user.""" | ||
| _setup_obo_mocks(httpx_mock) | ||
|
|
||
| access_token = await generate_token( | ||
| domain="auth0.local", | ||
| user_id="user_123", | ||
| audience="my-audience", | ||
| issuer="https://auth0.local/", | ||
| iat=True, | ||
| exp=True, | ||
| ) | ||
|
|
||
| app = FastAPI() | ||
| auth0 = Auth0FastAPI( | ||
| domain="auth0.local", | ||
| audience="my-audience", | ||
| client_id="cid", | ||
| client_secret="csecret", | ||
| ) | ||
|
|
||
| @app.post("/schedule-meeting") | ||
| async def schedule_meeting(request: Request, claims=Depends(auth0.require_auth())): | ||
| incoming = request.headers["authorization"].split(" ", 1)[1] | ||
| obo = await auth0.api_client.get_token_on_behalf_of( | ||
| access_token=incoming, | ||
| audience="https://calendar-api.example.com", | ||
| scope="calendar:read", | ||
| ) | ||
| return {"user": claims["sub"], "downstream_token": obo["access_token"]} | ||
|
|
||
| client = TestClient(app) | ||
| response = client.post( | ||
| "/schedule-meeting", | ||
| headers={"Authorization": f"Bearer {access_token}"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.json() == {"user": "user_123", "downstream_token": "obo-access-token"} | ||
|
|
||
| exchange_req = httpx_mock.get_requests(method="POST", url=TOKEN_ENDPOINT)[-1] | ||
| form = urllib.parse.parse_qs(exchange_req.content.decode()) | ||
| assert form["subject_token"] == [access_token] | ||
| assert form["audience"] == ["https://calendar-api.example.com"] | ||
| assert "client_secret" not in form | ||
| auth_header = exchange_req.headers.get("authorization") | ||
| assert auth_header.startswith("Basic ") | ||
| assert base64.b64decode(auth_header.split(" ")[1]).decode() == "cid:csecret" | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Re-exports the plugin adds on top of auth0-api-python | ||
| # ============================================================================= | ||
|
|
||
| def test_obo_surface_reexported(): | ||
| """The OBO method's helpers, result type, and error types are re-exported from the plugin.""" | ||
| import auth0_api_python as dep | ||
| import auth0_api_python.errors as errors | ||
|
|
||
| assert get_current_actor is dep.get_current_actor | ||
| assert get_delegation_chain is dep.get_delegation_chain | ||
| assert OnBehalfOfTokenResult is dep.OnBehalfOfTokenResult | ||
| assert GetTokenByExchangeProfileError is dep.GetTokenByExchangeProfileError | ||
| assert ApiError is dep.ApiError | ||
| # These three are only under auth0_api_python.errors upstream, not at its top level. | ||
| assert MissingRequiredArgumentError is errors.MissingRequiredArgumentError | ||
| assert VerifyAccessTokenError is errors.VerifyAccessTokenError | ||
| assert BaseAuthError is errors.BaseAuthError | ||
| for err in (GetTokenByExchangeProfileError, ApiError, MissingRequiredArgumentError, VerifyAccessTokenError): | ||
| assert issubclass(err, BaseAuthError) |
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.