Skip to content

Commit

Permalink
feat(core): add POST /configs/jwt-customizer/test API
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Mar 19, 2024
1 parent d3d0f51 commit 22f8676
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/core/src/routes/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import {
clientCredentialsJwtCustomizerGuard,
LogtoJwtTokenKey,
LogtoJwtTokenPath,
jsonObjectGuard,
customJwtFetcherGuard,
} from '@logto/schemas';
import { z } from 'zod';

import { EnvSet } from '#src/env-set/index.js';
import RequestError from '#src/errors/RequestError/index.js';
import koaGuard, { parse } from '#src/middleware/koa-guard.js';
import { exportJWK } from '#src/utils/jwks.js';
Expand Down Expand Up @@ -75,7 +78,7 @@ const getRedactedOidcKeyResponse = async (
);

export default function logtoConfigRoutes<T extends AuthedRouter>(
...[router, { queries, logtoConfigs, invalidateCache }]: RouterInitArgs<T>
...[router, { queries, logtoConfigs, invalidateCache, cloudConnection }]: RouterInitArgs<T>
) {
const {
getAdminConsoleConfig,
Expand Down Expand Up @@ -287,4 +290,45 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
return next();
}
);

if (!EnvSet.values.isCloud) {
return;
}

router.post(
'/configs/jwt-customizer/:tokenTypePath/test',
koaGuard({
params: z.object({
tokenTypePath: z.nativeEnum(LogtoJwtTokenPath),
}),
body: z.unknown(),
response: jsonObjectGuard,
/**
* 400 for cloud service zod error (data type does not match expectation, can be either request body or response body)
* 422 for cloud service syntax error
*/
status: [200, 400, 422],
}),
async (ctx, next) => {
const {
params: { tokenTypePath },
body: rawBody,
} = ctx.guard;
const {
body: { tokenSample, contextSample, ...rest },
} = getJwtTokenKeyAndBody(tokenTypePath, rawBody);

const client = await cloudConnection.getClient();
const testResult = await client.post(`/api/services/custom-jwt`, {
body: customJwtFetcherGuard.parse({
...rest,
tokenSample,
contextSample,
}),
});

ctx.body = testResult;
return next();
}
);
}
12 changes: 12 additions & 0 deletions packages/schemas/src/types/jwt-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export const customJwtFetcherGuard = jwtCustomizerGuard

export type CustomJwtFetcher = z.infer<typeof customJwtFetcherGuard>;

/**
* This guard is for testing use (request body guard), renamed previous `token` and `context`
* fields (in `customJwtFetcherGuard`) to `tokenSample` and `contextSample`, which can bring
* convenience to the testing use case.
*/
export const customJwtTesterGuard = customJwtFetcherGuard
.pick({ script: true, envVars: true })
.extend({
tokenSample: jsonObjectGuard,
contextSample: jsonObjectGuard.optional(),
});

export enum LogtoJwtTokenPath {
AccessToken = 'access-token',
ClientCredentials = 'client-credentials',
Expand Down

0 comments on commit 22f8676

Please sign in to comment.