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 e318732 commit bfcf20b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 84 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));

Check warning on line 148 in packages/core/src/oidc/extra-token-claims.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/oidc/extra-token-claims.ts#L144-L148

Added lines #L144 - L148 were not covered by tests
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,
},
}
),
},

Check warning on line 170 in packages/core/src/oidc/extra-token-claims.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/oidc/extra-token-claims.ts#L159-L170

Added lines #L159 - L170 were not covered by tests
}),
};

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 bfcf20b

Please sign in to comment.