|
| 1 | +# Custom Token Exchange |
| 2 | + |
| 3 | +Custom Token Exchange allows you to exchange tokens from external identity providers or legacy authentication systems for Auth0 tokens without browser redirects. This implements **OAuth 2.0 Token Exchange** ([RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693)). |
| 4 | + |
| 5 | +> **NOTE**: For complete documentation on Custom Token Exchange, configuration requirements, and detailed use cases, see the [official Auth0 documentation](https://auth0.com/docs/authenticate/custom-token-exchange). |
| 6 | +
|
| 7 | +## 1. Basic Token Exchange |
| 8 | + |
| 9 | +Exchange a custom token for Auth0 tokens without creating a user session. |
| 10 | + |
| 11 | +```python |
| 12 | +from auth0_server_python.auth_server.server_client import ServerClient |
| 13 | +from auth0_server_python.auth_types import CustomTokenExchangeOptions |
| 14 | + |
| 15 | +# Initialize the client |
| 16 | +auth0 = ServerClient( |
| 17 | + domain="<AUTH0_DOMAIN>", |
| 18 | + client_id="<AUTH0_CLIENT_ID>", |
| 19 | + client_secret="<AUTH0_CLIENT_SECRET>", |
| 20 | + secret="<AUTH0_SECRET>" |
| 21 | +) |
| 22 | + |
| 23 | +# Exchange a custom token |
| 24 | +response = await auth0.custom_token_exchange( |
| 25 | + CustomTokenExchangeOptions( |
| 26 | + subject_token="custom-token-from-external-system", |
| 27 | + subject_token_type="urn:acme:mcp-token", |
| 28 | + audience="https://api.example.com", |
| 29 | + scope="read:data write:data" |
| 30 | + ) |
| 31 | +) |
| 32 | + |
| 33 | +# Access the exchanged tokens |
| 34 | +print(f"Access Token: {response.access_token}") |
| 35 | +print(f"Expires In: {response.expires_in} seconds") |
| 36 | +if response.id_token: |
| 37 | + print(f"ID Token: {response.id_token}") |
| 38 | +``` |
| 39 | + |
| 40 | +## 2. Login with Token Exchange |
| 41 | + |
| 42 | +Exchange a custom token AND establish a user session. |
| 43 | + |
| 44 | +```python |
| 45 | +from auth0_server_python.auth_types import LoginWithCustomTokenExchangeOptions |
| 46 | +from fastapi import Request, Response |
| 47 | + |
| 48 | +# Exchange token and create session |
| 49 | +result = await auth0.login_with_custom_token_exchange( |
| 50 | + LoginWithCustomTokenExchangeOptions( |
| 51 | + subject_token="custom-token-from-external-system", |
| 52 | + subject_token_type="urn:acme:mcp-token", |
| 53 | + audience="https://api.example.com" |
| 54 | + ), |
| 55 | + store_options={"request": request, "response": response} |
| 56 | +) |
| 57 | + |
| 58 | +# User is now logged in |
| 59 | +user = result.state_data["user"] |
| 60 | +print(f"User logged in: {user['sub']}") |
| 61 | +``` |
| 62 | + |
| 63 | +> **TIP**: Use `login_with_custom_token_exchange()` when you need both token exchange and session management (e.g., user migration flows). Use `custom_token_exchange()` for pure token exchange scenarios (e.g., service-to-service authentication). |
| 64 | +
|
| 65 | +## 3. Actor Tokens (Delegation) |
| 66 | + |
| 67 | +Enable delegation scenarios where one service acts on behalf of a user. |
| 68 | + |
| 69 | +```python |
| 70 | +# Service acting on behalf of a user |
| 71 | +response = await auth0.custom_token_exchange( |
| 72 | + CustomTokenExchangeOptions( |
| 73 | + subject_token="user-access-token", |
| 74 | + subject_token_type="urn:ietf:params:oauth:token-type:access_token", |
| 75 | + actor_token="service-access-token", |
| 76 | + actor_token_type="urn:ietf:params:oauth:token-type:access_token", |
| 77 | + audience="https://api.example.com" |
| 78 | + ) |
| 79 | +) |
| 80 | +``` |
| 81 | + |
| 82 | +## 4. Custom Authorization Parameters |
| 83 | + |
| 84 | +Pass additional parameters to the token endpoint. |
| 85 | + |
| 86 | +```python |
| 87 | +response = await auth0.custom_token_exchange( |
| 88 | + CustomTokenExchangeOptions( |
| 89 | + subject_token="custom-token", |
| 90 | + subject_token_type="urn:acme:mcp-token", |
| 91 | + audience="https://api.example.com", |
| 92 | + authorization_params={ |
| 93 | + "custom_field": "custom_value" |
| 94 | + } |
| 95 | + ) |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +> **NOTE**: Critical parameters (`grant_type`, `client_id`, `subject_token`, `subject_token_type`) cannot be overridden via `authorization_params` for security reasons. |
| 100 | +
|
| 101 | +## 5. Organization Support |
| 102 | + |
| 103 | +Specify an organization when exchanging tokens. |
| 104 | + |
| 105 | +```python |
| 106 | +response = await auth0.custom_token_exchange( |
| 107 | + CustomTokenExchangeOptions( |
| 108 | + subject_token="custom-token", |
| 109 | + subject_token_type="urn:acme:mcp-token", |
| 110 | + audience="https://api.example.com", |
| 111 | + organization="org_abc1234" |
| 112 | + ) |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +## 6. Error Handling |
| 117 | + |
| 118 | +```python |
| 119 | +from auth0_server_python.error import CustomTokenExchangeError |
| 120 | + |
| 121 | +try: |
| 122 | + response = await auth0.custom_token_exchange( |
| 123 | + CustomTokenExchangeOptions( |
| 124 | + subject_token="token", |
| 125 | + subject_token_type="urn:acme:mcp-token" |
| 126 | + ) |
| 127 | + ) |
| 128 | +except CustomTokenExchangeError as e: |
| 129 | + print(f"Exchange failed: {e.code} - {e.message}") |
| 130 | +``` |
| 131 | + |
| 132 | +### Common Error Codes |
| 133 | + |
| 134 | +- `INVALID_TOKEN_FORMAT`: Token is empty, whitespace-only, or has "Bearer " prefix |
| 135 | +- `MISSING_ACTOR_TOKEN_TYPE`: `actor_token` provided without `actor_token_type` |
| 136 | +- `TOKEN_EXCHANGE_FAILED`: General token exchange failure |
| 137 | +- `INVALID_RESPONSE`: Auth0 returned a non-JSON response |
| 138 | + |
| 139 | +## 7. Token Type URIs |
| 140 | + |
| 141 | +Use standard URNs when possible: |
| 142 | + |
| 143 | +```python |
| 144 | +# Standard token types |
| 145 | +"urn:ietf:params:oauth:token-type:jwt" # JWT tokens |
| 146 | +"urn:ietf:params:oauth:token-type:access_token" # OAuth access tokens |
| 147 | +"urn:ietf:params:oauth:token-type:id_token" # OpenID Connect ID tokens |
| 148 | +"urn:ietf:params:oauth:token-type:refresh_token" # OAuth refresh tokens |
| 149 | + |
| 150 | +# Custom token types (use your own namespace) |
| 151 | +"urn:acme:mcp-token" |
| 152 | +"urn:company:legacy-token" |
| 153 | +``` |
| 154 | + |
| 155 | +## Additional Resources |
| 156 | + |
| 157 | +- [Auth0 Custom Token Exchange Documentation](https://auth0.com/docs/authenticate/custom-token-exchange) |
| 158 | +- [RFC 8693 - OAuth 2.0 Token Exchange](https://datatracker.ietf.org/doc/html/rfc8693) |
0 commit comments