Skip to content

Commit 8adbbb1

Browse files
committed
Add some MRRT docs
1 parent 1c429ae commit 8adbbb1

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

examples/RetrievingData.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,107 @@ access_token = await server_client.get_access_token(store_options=store_options)
7070

7171
Read more above in [Configuring the Store](./ConfigureStore.md).
7272

73+
## Multi-Resource Refresh Tokens (MRRT)
74+
75+
Multi-Resource Refresh Tokens allow using a single refresh token to obtain access tokens for multiple audiences, simplifying token management in applications that interact with multiple backend services.
76+
77+
Read more about [Multi-Resource Refresh Tokens in the Auth0 documentation](https://auth0.com/docs/secure/tokens/refresh-tokens/multi-resource-refresh-token).
78+
79+
80+
> [!WARNING]
81+
> When using Multi-Resource Refresh Token Configuration (MRRT), **Refresh Token Policies** on your Application need to be configured with the audiences you want to support. See the [Auth0 MRRT documentation](https://auth0.com/docs/secure/tokens/refresh-tokens/multi-resource-refresh-token) for setup instructions.
82+
>
83+
> **Tokens requested for audiences outside your configured policies will be ignored by Auth0, which will return a token for the default audience instead!**
84+
85+
### Configuring Scopes Per Audience
86+
87+
When working with multiple APIs, you can define different default scopes for each audience by passing an object instead of a string. This is particularly useful when different APIs require different default scopes:
88+
89+
```python
90+
server_client = ServerClient(
91+
...
92+
authorization_params={
93+
audience: "https://api.example.com", # Default audience
94+
scope: {
95+
"https://api.example.com": "openid profile email offline_access read:products read:orders",
96+
"https://analytics.example.com": "openid profile email offline_access read:analytics write:analytics",
97+
"https://admin.example.com": "openid profile email offline_access read:admin write:admin delete:admin"
98+
}
99+
}
100+
)
101+
```
102+
103+
**How it works:**
104+
105+
- Each key in the `scope` object is an `audience` identifier
106+
- The corresponding value is the scope string for that audience
107+
- When calling `get_access_token(audience=audience)`, the SDK automatically uses the configured scopes for that audience. When scopes are also passed in the method call, they are be merged with the default scopes for that audience.
108+
109+
### Usage Example
110+
111+
To retrieve access tokens for different audiences, use the `get_access_token()` method with an `audience` (and optionally also the `scope`) parameter.
112+
113+
```python
114+
115+
server_client = ServerClient(
116+
...
117+
authorization_params={
118+
"audience": "https://api.example.com", # Default audience
119+
"scope": {
120+
"https://api.example.com": "openid email profile",
121+
"https://analytics.example.com": "read:analytics write:analytics"
122+
}
123+
}
124+
)
125+
126+
# Get token for default audience
127+
default_token = await server_client.get_access_token()
128+
# returns token for https://api.example.com with openid, email, and profile scopes
129+
130+
# Get token for different audience
131+
data_token = await server_client.get_access_token(audience="https://data-api.example.com")
132+
# returns token for https://analytics.example.com with read:analytics and write:analytics scopes
133+
134+
# Get token with additional scopes
135+
admin_token = await server_client.get_access_token(
136+
audience="https://api.example.com",
137+
scope="write:admin"
138+
)
139+
# returns token for https://api.example.com with openid, email, profile and write:admin scopes
140+
141+
```
142+
143+
### Token Management Best Practices
144+
145+
**Configure Broad Default Scopes**: Define comprehensive scopes in your `ServerClient` constructor for common use cases. This minimizes the need to request additional scopes dynamically, reducing the amount of tokens that need to be stored.
146+
147+
```python
148+
server_client = ServerClient(
149+
...
150+
authorization_params={
151+
"audience": "https://api.example.com", # Default audience
152+
# Configure broad default scopes for most common operations
153+
"scope": {
154+
"https://api.example.com": "openid profile email offline_access read:products read:orders read:users"
155+
}
156+
}
157+
)
158+
```
159+
160+
**Minimize Dynamic Scope Requests**: Avoid passing `scope` when calling `get_access_token()` unless absolutely necessary. Each `audience` + `scope` combination results in a token to store in the session, increasing session size.
161+
162+
```python
163+
# Preferred: Use default scopes
164+
token = await server_client.get_access_token(audience="https://api.example.com")
165+
166+
167+
# Avoid unless necessary: Dynamic scopes increase session size
168+
token = await server_client.get_access_token(
169+
audience="https://api.example.com"
170+
scope="openid profile email read:products write:products admin:all"
171+
)
172+
```
173+
73174
## Retrieving an Access Token for a Connections
74175

75176
The SDK's `get_access_token_for_connection()` can be used to retrieve an Access Token for a connection (e.g. `google-oauth2`) for the current logged-in user:

0 commit comments

Comments
 (0)