You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before using the token exchange grant, you need to enable it for your application:
3
+
4
+
1. Go to <CloudLinkto="/applications">Console > Applications</CloudLink> and select your application.
5
+
2. In the application settings, find the "Token exchange" section.
6
+
3. Enable the "Allow token exchange" toggle.
7
+
8
+
Token exchange is disabled by default for security reasons. If you don't enable it, you will receive a "token exchange is not allowed for this application" error.
Imagine Sarah, a support engineer at TechCorp, receives an urgent ticket from Alex, a customer who can't access a critical resource. To efficiently diagnose and resolve the issue, Sarah needs to see exactly what Alex sees in the system. This is where Logto's user impersonation feature comes in handy.
@@ -109,16 +111,37 @@ TechCorp's server should then return this subject token to Sarah's application.
109
111
110
112
### Step 3: Exchanging the subject token for an access token \{#step-3-exchanging-the-subject-token-for-an-access-token}
111
113
114
+
<TokenExchangePrerequisites />
115
+
112
116
Now, Sarah's application exchanges this subject token for an access token representing Alex, specifying the resource where the token will be used.
113
117
114
118
**Request (Sarah's application to Logto's token endpoint)**
115
119
120
+
For traditional web applications or machine-to-machine applications with app secret, include the credentials in the `Authorization` header:
The `access_token` returned will be bound to the specified resource, ensuring it can only be used with TechCorp's customer data API.
142
165
143
-
**Note**: For traditional web applications, include `client_id` and `client_secret`in the header of the token request to prevent a 401 invalid_client error.
@@ -227,20 +260,24 @@ async function impersonateUser(
227
260
// Sarah uses this function to impersonate Alex
228
261
async function performImpersonation(): Promise<void> {
229
262
try {
263
+
// highlight-start
264
+
// For traditional web or M2M apps, pass the client secret
230
265
const accessToken = await impersonateUser(
231
266
'alex123',
232
267
'techcorp_support_app',
233
268
'TECH-1234',
234
-
'https://api.techcorp.com/customer-data'
269
+
'https://api.techcorp.com/customer-data',
270
+
'your-client-secret' // Omit this for SPA or native apps
235
271
);
272
+
// highlight-end
236
273
console.log('Impersonation access token for Alex:', accessToken);
237
274
} catch (error) {
238
275
console.error('Failed to perform impersonation:', error);
239
276
}
240
277
}
241
278
242
279
// Execute the impersonation
243
-
void performImpersonation()
280
+
void performImpersonation();
244
281
```
245
282
246
283
:::note
@@ -257,12 +294,33 @@ When using the token exchange flow for impersonation, the issued access token ca
257
294
258
295
To include the `act` claim, Sarah's application needs to provide an `actor_token`in the token exchange request. This token should be a valid access token forSarah with the `openid` scope. Here's how to include itin the token exchange request:
259
296
297
+
For traditional web applications or machine-to-machine applications:
Personal access tokens (PATs) provide a secure way for users to grant [access token](https://auth.wiki/access-token) without using their credentials and interactive sign-in. This is useful for CI/CD, scripts, or applications that need to access resources programmatically.
@@ -34,6 +36,8 @@ If you're working with organizations, the access patterns and permissions are th
34
36
35
37
### Request \{#request}
36
38
39
+
<TokenExchangePrerequisites />
40
+
37
41
The application makes a [token exchange request](https://auth.wiki/authorization-code-flow#token-exchange-request) to the tenant's [token endpoint](/integrate-logto/application-data-structure#token-endpoint) with a special grant type using the HTTP POST method. The following parameters are included in the HTTP request entity-body using the `application/x-www-form-urlencoded` format.
38
42
39
43
1.`client_id`: REQUIRED. The client ID of the application.
@@ -55,36 +59,40 @@ If the token exchange request is successful, the tenant's token endpoint returns
55
59
56
60
### Example token exchange \{#example-token-exchange}
57
61
58
-
For traditional web applications with app secret:
62
+
For traditional web applications or machine-to-machine applications with app secret, include the credentials in the `Authorization` header using HTTP Basic authentication:
0 commit comments