Skip to content

Commit

Permalink
refactor(console,core): jwt test api integration
Browse files Browse the repository at this point in the history
jwt test api integration
  • Loading branch information
simeng-li committed Mar 21, 2024
1 parent 776ce7f commit 0ea1dd6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Props = {

const userTokenModelSettings = [accessTokenPayloadTestModel, userContextTestModel];
const machineToMachineTokenModelSettings = [clientCredentialsPayloadTestModel];
const testEndpointPath = 'api/config/jwt-customizer/test';
const testEndpointPath = 'api/configs/jwt-customizer/test';

function TestTab({ isActive }: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.jwt_claims' });
Expand Down
28 changes: 18 additions & 10 deletions packages/console/src/pages/JwtClaims/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,29 @@ export const formatFormDataToRequestData = (data: JwtClaimsFormType) => {
};
};

export const formatFormDataToTestRequestPayload = (data: JwtClaimsFormType) => {
const defaultTokenPayload =
data.tokenType === LogtoJwtTokenPath.AccessToken
export const formatFormDataToTestRequestPayload = ({
tokenType,
script,
environmentVariables,
testSample,
}: JwtClaimsFormType) => {
const defaultTokenSample =
tokenType === LogtoJwtTokenPath.AccessToken
? defaultAccessTokenPayload
: defaultClientCredentialsPayload;

const defaultContext =
data.tokenType === LogtoJwtTokenPath.AccessToken ? defaultUserTokenContextData : undefined;
const defaultContextSample =
tokenType === LogtoJwtTokenPath.AccessToken ? defaultUserTokenContextData : undefined;

return {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- parse empty string as undefined
script: data.script || undefined,
envVars: formatEnvVariablesFormData(data.environmentVariables),
token: formatSampleCodeStringToJson(data.testSample?.tokenSample) ?? defaultTokenPayload,
context: formatSampleCodeStringToJson(data.testSample?.contextSample) ?? defaultContext,
tokenType,
payload: {
script,
envVars: formatEnvVariablesFormData(environmentVariables),
tokenSample: formatSampleCodeStringToJson(testSample?.tokenSample) ?? defaultTokenSample,
contextSample:
formatSampleCodeStringToJson(testSample?.contextSample) ?? defaultContextSample,
},
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/routes/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
*/
body: z.discriminatedUnion('tokenType', [
z.object({
tokenType: z.literal(LogtoJwtTokenKey.AccessToken),
tokenType: z.literal(LogtoJwtTokenPath.AccessToken),
payload: accessTokenJwtCustomizerGuard.required({
script: true,
tokenSample: true,
}),
}),
z.object({
tokenType: z.literal(LogtoJwtTokenKey.ClientCredentials),
tokenType: z.literal(LogtoJwtTokenPath.ClientCredentials),
payload: clientCredentialsJwtCustomizerGuard.required({
script: true,
tokenSample: true,
Expand Down

0 comments on commit 0ea1dd6

Please sign in to comment.