Skip to content
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

feat(core): add subject token context to jwt customizer #6185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';

import { isDevFeaturesEnabled } from '@/consts/env';
import { type JwtCustomizerForm } from '@/pages/CustomizeJwtDetails/type';
import {
environmentVariablesCodeExample,
Expand Down Expand Up @@ -78,7 +79,7 @@ function InstructionTab({ isActive }: Props) {
/>
</GuideCard>
)}
{tokenType === LogtoJwtTokenKeyType.AccessToken && (
{isDevFeaturesEnabled && tokenType === LogtoJwtTokenKeyType.AccessToken && (
<GuideCard
name={CardType.GrantData}
isExpanded={expendCard === CardType.GrantData}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type EditorProps } from '@monaco-editor/react';
import TokenFileIcon from '@/assets/icons/token-file-icon.svg';
import UserFileIcon from '@/assets/icons/user-file-icon.svg';

import { isDevFeaturesEnabled } from '../../../consts/env.js';
import type { ModelSettings } from '../MainContent/MonacoCodeEditor/type.js';

import {
Expand Down Expand Up @@ -209,10 +210,14 @@ const defaultGrantContext: Partial<JwtCustomizerGrantContext> = {
},
};

export const defaultUserTokenContextData = {
user: defaultUserContext,
grant: defaultGrantContext,
};
export const defaultUserTokenContextData = isDevFeaturesEnabled
? {
user: defaultUserContext,
grant: defaultGrantContext,
}
: {
user: defaultUserContext,
};

export const accessTokenPayloadTestModel: ModelSettings = {
language: 'json',
Expand Down
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 @@ -142,6 +142,11 @@
(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 149 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#L145-L149

Added lines #L145 - L149 were not covered by tests
const payload: CustomJwtFetcher = {
script,
environmentVariables,
Expand All @@ -150,10 +155,20 @@
? { tokenType: LogtoJwtTokenKeyType.ClientCredentials }
: {
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.

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[no-warning-comments] Unexpected 'todo' comment: 'TODO (LOG-8555): the newly added...'.
// `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,
},
wangsijie marked this conversation as resolved.
Show resolved Hide resolved
}
),
},

Check warning on line 171 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#L160-L171

Added lines #L160 - L171 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
Loading