-
-
Notifications
You must be signed in to change notification settings - Fork 678
feat(core): support access token exchange for service-to-service delegation #8124
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
Open
wangsijie
wants to merge
7
commits into
master
Choose a base branch
from
feat/jwt-access-token-exchange
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
19cf6ba
feat(core): support JWT access token exchange for service-to-service …
wangsijie e62c5b0
fix: format import statement for prettier
wangsijie 02b0cfd
refactor(core): use object param for validateSubjectToken
wangsijie 2dc5082
chore: add changeset for JWT access token exchange feature
wangsijie 4d32d71
refactor(core): use standard access_token type for token exchange
wangsijie 9c3b6b1
refactor(test): use new impersonation token type for tests
wangsijie 6010a9c
refactor(core): rename impersonation token prefix constant
wangsijie 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@logto/core": minor | ||
| --- | ||
|
|
||
| support JWT access token exchange for service-to-service delegation | ||
|
|
||
| Added a new `subject_token_type` value `urn:ietf:params:oauth:token-type:jwt` to enable JWT access token exchange. This allows services to exchange JWT tokens issued by trusted issuers for Logto access tokens, enabling service-to-service delegation scenarios. The JWT tokens are verified using the issuer's JWK set and can be reused multiple times. |
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
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
148 changes: 148 additions & 0 deletions
148
packages/integration-tests/src/tests/api/oidc/token-exchange/actor-token.test.ts
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,148 @@ | ||
| import { ApplicationType, GrantType, InteractionEvent, type Resource } from '@logto/schemas'; | ||
| import { formUrlEncodedHeaders } from '@logto/shared'; | ||
|
|
||
| import { deleteUser } from '#src/api/admin-user.js'; | ||
| import { oidcApi } from '#src/api/api.js'; | ||
| import { createApplication, deleteApplication } from '#src/api/application.js'; | ||
| import { putInteraction } from '#src/api/interaction.js'; | ||
| import { createResource, deleteResource } from '#src/api/resource.js'; | ||
| import { createSubjectToken } from '#src/api/subject-token.js'; | ||
| import type MockClient from '#src/client/index.js'; | ||
| import { initClient, processSession } from '#src/helpers/client.js'; | ||
| import { createUserByAdmin } from '#src/helpers/index.js'; | ||
| import { enableAllPasswordSignInMethods } from '#src/helpers/sign-in-experience.js'; | ||
| import { generatePassword, generateUsername, getAccessTokenPayload } from '#src/utils.js'; | ||
|
|
||
| describe('Token Exchange (Actor Token)', () => { | ||
| const username = generateUsername(); | ||
| const password = generatePassword(); | ||
|
|
||
| const testApiResourceInfo: Pick<Resource, 'name' | 'indicator'> = { | ||
| name: 'test-actor-token-resource', | ||
| indicator: 'https://actor-token.logto.io/api', | ||
| }; | ||
|
|
||
| /* eslint-disable @silverhand/fp/no-let */ | ||
| let testApiResourceId: string; | ||
| let testApplicationId: string; | ||
| let testUserId: string; | ||
| let testAccessToken: string; | ||
| let client: MockClient; | ||
| /* eslint-enable @silverhand/fp/no-let */ | ||
|
|
||
| beforeAll(async () => { | ||
| await enableAllPasswordSignInMethods(); | ||
|
|
||
| /* eslint-disable @silverhand/fp/no-mutation */ | ||
| const resource = await createResource(testApiResourceInfo.name, testApiResourceInfo.indicator); | ||
| testApiResourceId = resource.id; | ||
|
|
||
| const applicationName = 'test-actor-token-app'; | ||
| const applicationType = ApplicationType.SPA; | ||
| const application = await createApplication(applicationName, applicationType, { | ||
| oidcClientMetadata: { redirectUris: ['http://localhost:3000'], postLogoutRedirectUris: [] }, | ||
| }); | ||
| testApplicationId = application.id; | ||
|
|
||
| const { id } = await createUserByAdmin({ username, password }); | ||
| testUserId = id; | ||
|
|
||
| client = await initClient({ | ||
| resources: [testApiResourceInfo.indicator], | ||
| }); | ||
| await client.successSend(putInteraction, { | ||
| event: InteractionEvent.SignIn, | ||
| identifier: { username, password }, | ||
| }); | ||
| const { redirectTo } = await client.submitInteraction(); | ||
| await processSession(client, redirectTo); | ||
| testAccessToken = await client.getAccessToken(); | ||
| /* eslint-enable @silverhand/fp/no-mutation */ | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await deleteUser(testUserId); | ||
| await deleteResource(testApiResourceId); | ||
| await deleteApplication(testApplicationId); | ||
| }); | ||
|
|
||
| it('should exchange an access token with `act` claim', async () => { | ||
| const { subjectToken } = await createSubjectToken(testUserId); | ||
|
|
||
| const { access_token } = await oidcApi | ||
| .post('token', { | ||
| headers: formUrlEncodedHeaders, | ||
| body: new URLSearchParams({ | ||
| client_id: testApplicationId, | ||
| grant_type: GrantType.TokenExchange, | ||
| subject_token: subjectToken, | ||
| subject_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| actor_token: testAccessToken, | ||
| actor_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| resource: testApiResourceInfo.indicator, | ||
| }), | ||
| }) | ||
| .json<{ access_token: string }>(); | ||
|
|
||
| expect(getAccessTokenPayload(access_token)).toHaveProperty('act', { sub: testUserId }); | ||
| }); | ||
|
|
||
| it('should fail with invalid actor_token_type', async () => { | ||
| const { subjectToken } = await createSubjectToken(testUserId); | ||
|
|
||
| await expect( | ||
| oidcApi.post('token', { | ||
| headers: formUrlEncodedHeaders, | ||
| body: new URLSearchParams({ | ||
| client_id: testApplicationId, | ||
| grant_type: GrantType.TokenExchange, | ||
| subject_token: subjectToken, | ||
| subject_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| actor_token: testAccessToken, | ||
| actor_token_type: 'invalid_actor_token_type', | ||
| resource: testApiResourceInfo.indicator, | ||
| }), | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should fail with invalid actor_token', async () => { | ||
| const { subjectToken } = await createSubjectToken(testUserId); | ||
|
|
||
| await expect( | ||
| oidcApi.post('token', { | ||
| headers: formUrlEncodedHeaders, | ||
| body: new URLSearchParams({ | ||
| client_id: testApplicationId, | ||
| grant_type: GrantType.TokenExchange, | ||
| subject_token: subjectToken, | ||
| subject_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| actor_token: 'invalid_actor_token', | ||
| actor_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| resource: testApiResourceInfo.indicator, | ||
| }), | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should fail when the actor token do not have `openid` scope', async () => { | ||
| const { subjectToken } = await createSubjectToken(testUserId); | ||
| // Set `resource` to ensure that the access token is JWT, and then it won't have `openid` scope. | ||
| const accessToken = await client.getAccessToken(testApiResourceInfo.indicator); | ||
|
|
||
| await expect( | ||
| oidcApi.post('token', { | ||
| headers: formUrlEncodedHeaders, | ||
| body: new URLSearchParams({ | ||
| client_id: testApplicationId, | ||
| grant_type: GrantType.TokenExchange, | ||
| subject_token: subjectToken, | ||
| subject_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| actor_token: accessToken, | ||
| actor_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| resource: testApiResourceInfo.indicator, | ||
| }), | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.