|
2 | 2 |
|
3 | 3 | This document provides examples for using the `auth0-api-python` package to validate Auth0 tokens in your API. |
4 | 4 |
|
| 5 | +## On Behalf Of Token Exchange |
| 6 | + |
| 7 | +Use `get_token_on_behalf_of()` when your API receives an `Auth0` access token for itself and needs |
| 8 | +to exchange it for another `Auth0` access token targeting a downstream API while preserving the same |
| 9 | +user identity. This is especially useful for `MCP` servers and other intermediary APIs that need to |
| 10 | +call downstream APIs on behalf of the user. |
| 11 | + |
| 12 | +The following example verifies the incoming access token for your API, exchanges it for a token for the downstream API, and then calls the downstream API with the exchanged token. |
| 13 | + |
| 14 | +```python |
| 15 | +import asyncio |
| 16 | +import httpx |
| 17 | + |
| 18 | +from auth0_api_python import ApiClient, ApiClientOptions |
| 19 | + |
| 20 | +async def exchange_on_behalf_of(): |
| 21 | + api_client = ApiClient(ApiClientOptions( |
| 22 | + domain="your-tenant.auth0.com", |
| 23 | + audience="https://mcp-server.example.com", |
| 24 | + client_id="<AUTH0_CLIENT_ID>", |
| 25 | + client_secret="<AUTH0_CLIENT_SECRET>" |
| 26 | + )) |
| 27 | + |
| 28 | + incoming_access_token = "incoming-auth0-access-token" |
| 29 | + |
| 30 | + claims = await api_client.verify_access_token(access_token=incoming_access_token) |
| 31 | + |
| 32 | + result = await api_client.get_token_on_behalf_of( |
| 33 | + access_token=incoming_access_token, |
| 34 | + audience="https://calendar-api.example.com", |
| 35 | + scope="calendar:read calendar:write" |
| 36 | + ) |
| 37 | + |
| 38 | + async with httpx.AsyncClient() as client: |
| 39 | + downstream_response = await client.get( |
| 40 | + "https://calendar-api.example.com/events", |
| 41 | + headers={"Authorization": f"Bearer {result['access_token']}"} |
| 42 | + ) |
| 43 | + |
| 44 | + downstream_response.raise_for_status() |
| 45 | + |
| 46 | + return { |
| 47 | + "user": claims["sub"], |
| 48 | + "data": downstream_response.json(), |
| 49 | + } |
| 50 | + |
| 51 | +asyncio.run(exchange_on_behalf_of()) |
| 52 | +``` |
| 53 | + |
| 54 | +> [!TIP] Production notes: |
| 55 | +> - Pass the raw access token to `get_token_on_behalf_of()`. Do not pass the full `Authorization` header or include the `Bearer ` prefix. |
| 56 | +> - Verify the incoming token for your API before exchanging it so your application rejects invalid or mis-targeted tokens early. |
| 57 | +> - The downstream `audience` must match an API identifier configured in your Auth0 tenant. |
| 58 | +> - `get_token_on_behalf_of()` only returns access-token-oriented fields. It does not expose `id_token` or `refresh_token`. |
| 59 | +
|
| 60 | +In the current implementation, `get_token_on_behalf_of()` forwards the incoming access token as |
| 61 | +the [RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693#section-2.1) `subject_token` and relies on Auth0 to handle any DPoP-specific behavior for that token. |
| 62 | + |
| 63 | +## Inspecting Delegation After Token Verification |
| 64 | + |
| 65 | +When a downstream API or `MCP` server receives an access token that may have been issued through |
| 66 | +delegation, it can verify the token first and then inspect the `act` claim to identify the current |
| 67 | +actor for authorization and the full delegation chain for audit or attribution. |
| 68 | + |
| 69 | +```python |
| 70 | +import asyncio |
| 71 | +import logging |
| 72 | + |
| 73 | +from auth0_api_python import ( |
| 74 | + ApiClient, |
| 75 | + ApiClientOptions, |
| 76 | + get_current_actor, |
| 77 | + get_delegation_chain, |
| 78 | +) |
| 79 | + |
| 80 | +logger = logging.getLogger(__name__) |
| 81 | + |
| 82 | +async def inspect_delegated_token(): |
| 83 | + api_client = ApiClient(ApiClientOptions( |
| 84 | + domain="your-tenant.auth0.com", |
| 85 | + audience="https://calendar-api.example.com" |
| 86 | + )) |
| 87 | + |
| 88 | + access_token = "delegated-auth0-access-token" |
| 89 | + |
| 90 | + claims = await api_client.verify_access_token(access_token=access_token) |
| 91 | + |
| 92 | + current_actor = get_current_actor(claims) |
| 93 | + delegation_chain = get_delegation_chain(claims) |
| 94 | + |
| 95 | + if current_actor != "mcp_server_client_id": |
| 96 | + raise PermissionError("unexpected actor") |
| 97 | + |
| 98 | + logger.info( |
| 99 | + "delegated request", |
| 100 | + extra={ |
| 101 | + "user_sub": claims["sub"], |
| 102 | + "current_actor": current_actor, |
| 103 | + "delegation_chain": delegation_chain, |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + return { |
| 108 | + "user_sub": claims["sub"], |
| 109 | + "current_actor": current_actor, |
| 110 | + "delegation_chain": delegation_chain, |
| 111 | + } |
| 112 | + |
| 113 | +asyncio.run(inspect_delegated_token()) |
| 114 | +``` |
| 115 | + |
| 116 | +Only the outermost `act.sub` represents the current actor and should be used for authorization |
| 117 | +decisions. Nested `act` values represent prior actors and are better suited for logging, audit, or |
| 118 | +attribution. |
| 119 | + |
5 | 120 | ## Bearer Authentication |
6 | 121 |
|
7 | 122 | Bearer authentication is the standard OAuth 2.0 token authentication method. |
@@ -157,4 +272,4 @@ async def verify_dpop_token(access_token, dpop_proof, http_method, http_url): |
157 | 272 | "token_claims": token_claims, |
158 | 273 | "proof_claims": proof_claims |
159 | 274 | } |
160 | | -``` |
| 275 | +``` |
0 commit comments