diff --git a/packages/core/src/libraries/logto-config.ts b/packages/core/src/libraries/logto-config.ts index 2aaf2453a438..ad244beadabf 100644 --- a/packages/core/src/libraries/logto-config.ts +++ b/packages/core/src/libraries/logto-config.ts @@ -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 ( cloudConnection: CloudConnectionLibrary, payload: { key: T; value: JwtCustomizerType[T]; - isTest?: boolean; + useCase: 'test' | 'production'; } ) => { const [client, jwtCustomizers] = await Promise.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`, { diff --git a/packages/core/src/routes/logto-config/jwt-customizer.test.ts b/packages/core/src/routes/logto-config/jwt-customizer.test.ts index ae763299aef4..88c0424c7247 100644 --- a/packages/core/src/routes/logto-config/jwt-customizer.test.ts +++ b/packages/core/src/routes/logto-config/jwt-customizer.test.ts @@ -60,6 +60,7 @@ describe('configs JWT customizer routes', () => { { key: LogtoJwtTokenKey.AccessToken, value: mockJwtCustomizerConfigForAccessToken.value, + useCase: 'production', } ); @@ -104,6 +105,7 @@ describe('configs JWT customizer routes', () => { { key: LogtoJwtTokenKey.AccessToken, value: mockJwtCustomizerConfigForAccessToken.value, + useCase: 'production', } ); @@ -168,7 +170,7 @@ describe('configs JWT customizer routes', () => { { key: LogtoJwtTokenKey.ClientCredentials, value: payload, - isTest: true, + useCase: 'test', } ); diff --git a/packages/core/src/routes/logto-config/jwt-customizer.ts b/packages/core/src/routes/logto-config/jwt-customizer.ts index ec86e92ba7da..da629a241ff9 100644 --- a/packages/core/src/routes/logto-config/jwt-customizer.ts +++ b/packages/core/src/routes/logto-config/jwt-customizer.ts @@ -86,6 +86,7 @@ export default function logtoConfigJwtCustomizerRoutes( await deployJwtCustomizerScript(cloudConnection, { key, value: body, + useCase: 'production', }); } @@ -129,6 +130,7 @@ export default function logtoConfigJwtCustomizerRoutes( await deployJwtCustomizerScript(cloudConnection, { key, value: body, + useCase: 'production', }); } @@ -228,7 +230,7 @@ export default function logtoConfigJwtCustomizerRoutes( ? LogtoJwtTokenKey.AccessToken : LogtoJwtTokenKey.ClientCredentials, value: body, - isTest: true, + useCase: 'test', }); const client = await cloudConnection.getClient();