Skip to content

Commit

Permalink
chore: update util function interface and update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Apr 18, 2024
1 parent ff163b7 commit 841063d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
31 changes: 20 additions & 11 deletions packages/core/src/libraries/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ export const createLogtoConfigLibrary = ({
* @params payload - The latest JWT customizer payload needs to be deployed.
* @params payload.key - The tokenType of the JWT customizer.
* @params payload.value - JWT customizer value
* @params payload.isTest - Whether the JWT customizer is for test environment.
* @params payload.useCase - The use case of JWT customizer script, can be either `test` or `production`.
*/
const deployJwtCustomizerScript = async <T extends LogtoJwtTokenKey>(
cloudConnection: CloudConnectionLibrary,
payload: {
key: T;
value: JwtCustomizerType[T];
isTest?: boolean;
useCase: 'test' | 'production';
}
) => {
const [client, jwtCustomizers] = await Promise.all([
Expand All @@ -166,16 +166,25 @@ export const createLogtoConfigLibrary = ({

const newCustomizerScripts: CustomJwtDeployRequestBody = {
/**
* Only add `/test` endpoint for Cloudflare workers when testing.
* O/w overwrite the existing JWT customizer script.
* There are at most 4 custom JWT scripts in the `CustomJwtDeployRequestBody`-typed object,
* and can be indexed by `data[CustomJwtType][UseCase]`.
*
* Per our design, each script will be deployed as a API endpoint in the Cloudflare
* worker service. A production script will be deployed to `/api/custom-jwt`
* endpoint and a test script will be deployed to `/api/custom-jwt/test` endpoint.
*
* If the current use case is `test`, then the script should be deployed to a `/test` endpoint;
* otherwise, the script should be deployed to the `/api/custom-jwt` endpoint and overwrite
* previous handler of the API endpoint.
*/
[payload.key]: payload.isTest
? {
test: payload.value.script,
}
: {
production: payload.value.script,
},
[payload.key]:
payload.useCase === 'test'
? {
test: payload.value.script,
}
: {
production: payload.value.script,
},
};

await client.put(`/api/services/custom-jwt/worker`, {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/routes/logto-config/jwt-customizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('configs JWT customizer routes', () => {
{
key: LogtoJwtTokenKey.AccessToken,
value: mockJwtCustomizerConfigForAccessToken.value,
useCase: 'production',
}
);

Expand Down Expand Up @@ -104,6 +105,7 @@ describe('configs JWT customizer routes', () => {
{
key: LogtoJwtTokenKey.AccessToken,
value: mockJwtCustomizerConfigForAccessToken.value,
useCase: 'production',
}
);

Expand Down Expand Up @@ -168,7 +170,7 @@ describe('configs JWT customizer routes', () => {
{
key: LogtoJwtTokenKey.ClientCredentials,
value: payload,
isTest: true,
useCase: 'test',
}
);

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/routes/logto-config/jwt-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default function logtoConfigJwtCustomizerRoutes<T extends AuthedRouter>(
await deployJwtCustomizerScript(cloudConnection, {
key,
value: body,
useCase: 'production',
});
}

Expand Down Expand Up @@ -129,6 +130,7 @@ export default function logtoConfigJwtCustomizerRoutes<T extends AuthedRouter>(
await deployJwtCustomizerScript(cloudConnection, {
key,
value: body,
useCase: 'production',
});
}

Expand Down Expand Up @@ -228,7 +230,7 @@ export default function logtoConfigJwtCustomizerRoutes<T extends AuthedRouter>(
? LogtoJwtTokenKey.AccessToken
: LogtoJwtTokenKey.ClientCredentials,
value: body,
isTest: true,
useCase: 'test',
});

const client = await cloudConnection.getClient();
Expand Down

0 comments on commit 841063d

Please sign in to comment.