Skip to content

Commit

Permalink
feat(core): add subject token context to jwt customizer
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie committed Jul 5, 2024
1 parent 4163f70 commit 840aad5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 85 deletions.
19 changes: 17 additions & 2 deletions packages/core/src/oidc/extra-token-claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ export const getExtraTokenClaimsForJwtCustomization = async (
(await libraries.jwtCustomizers.getUserContext(token.accountId))
);

const subjectToken =
isTokenClientCredentials || token.gty !== GrantType.TokenExchange
? undefined
: await trySafe(async () => queries.subjectTokens.findSubjectToken(token.grantId));

const payload: CustomJwtFetcher = {
script,
environmentVariables,
Expand All @@ -151,8 +156,18 @@ export const getExtraTokenClaimsForJwtCustomization = async (
tokenType: LogtoJwtTokenKeyType.AccessToken,
// TODO (LOG-8555): the newly added `UserProfile` type includes undefined fields and can not be directly assigned to `Json` type. And the `undefined` fields should be removed by zod guard.
// `context` parameter is only eligible for user's access token for now.
// eslint-disable-next-line no-restricted-syntax
context: { user: logtoUserInfo as Record<string, Json> },
context: {
// eslint-disable-next-line no-restricted-syntax
user: logtoUserInfo as Record<string, Json>,
...conditional(
subjectToken && {
grant: {
type: GrantType.TokenExchange,
subjectTokenContext: subjectToken.context,
},
}
),
},
}),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/integration-tests/src/__mocks__/jwt-customizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AccessTokenPayload, ClientCredentialsPayload } from '@logto/schemas';
import { type AccessTokenPayload, type ClientCredentialsPayload } from '@logto/schemas';

const standardTokenPayloadData = {
jti: 'f1d3d2d1-1f2d-3d4e-5d6f-7d8a9d0e1d2',
Expand Down
Loading

0 comments on commit 840aad5

Please sign in to comment.