Skip to content

Commit

Permalink
refactor(core): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Mar 6, 2024
1 parent e327754 commit 5d55776
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/libraries/cloud-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const logtoConfigs: LogtoConfigLibrary = {
}),
getOidcConfigs: jest.fn(),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
};

describe('getAccessToken()', () => {
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/libraries/logto-config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import {
LogtoConfigs,
cloudApiIndicator,
cloudConnectionDataGuard,
logtoOidcConfigGuard,
LogtoOidcConfigKey,
jwtCustomizerConfigGuard,
} from '@logto/schemas';
import type { LogtoOidcConfigType, LogtoJwtTokenKey } from '@logto/schemas';
import { convertToIdentifiers } from '@logto/shared';
import chalk from 'chalk';
import { z, ZodError } from 'zod';

import RequestError from '#src/errors/RequestError/index.js';
import type Queries from '#src/tenants/Queries.js';
import { consoleLog } from '#src/utils/console.js';

export type LogtoConfigLibrary = ReturnType<typeof createLogtoConfigLibrary>;
const { table } = convertToIdentifiers(LogtoConfigs);

export const createLogtoConfigLibrary = ({
logtoConfigs: {
Expand Down Expand Up @@ -77,5 +81,21 @@ export const createLogtoConfigLibrary = ({
};
};

return { getOidcConfigs, getCloudConnectionData, upsertJwtCustomizer };
const getJwtCustomizer = async <T extends LogtoJwtTokenKey>(key: T) => {
const { rows } = await getRowsByKeys([key]);

// If the record does not exist (`rows` is empty)
if (rows.length === 0) {
throw new RequestError({
code: 'entity.not_exists',
name: table,
id: key,
status: 404,
});
}

return z.object({ value: jwtCustomizerConfigGuard[key] }).parse(rows[0]);
};

return { getOidcConfigs, getCloudConnectionData, upsertJwtCustomizer, getJwtCustomizer };
};
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const cloudConnection = createCloudConnectionLibrary({
}),
getOidcConfigs: jest.fn(),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
});

const getLogtoConnectors = jest.spyOn(connectorLibrary, 'getLogtoConnectors');
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/routes/logto-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const logtoConfigQueries = {
}),
updateOidcConfigsByKey: jest.fn(),
getRowsByKeys: jest.fn(async () => mockLogtoConfigRows),
getJwtCustomizer: jest.fn(),
};

const logtoConfigLibraries = {
Expand All @@ -63,6 +62,7 @@ const logtoConfigLibraries = {
[LogtoOidcConfigKey.CookieKeys]: mockCookieKeys,
})),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
};

const settingRoutes = await pickDefault(import('./logto-config.js'));
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('configs routes', () => {
});

it('GET /configs/jwt-customizer/:tokenType should return the record', async () => {
logtoConfigQueries.getJwtCustomizer.mockResolvedValueOnce(
logtoConfigLibraries.getJwtCustomizer.mockResolvedValueOnce(
mockJwtCustomizerConfigForAccessToken
);
const response = await routeRequester.get('/configs/jwt-customizer/access-token');
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/routes/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
) {
const { getAdminConsoleConfig, getRowsByKeys, updateAdminConsoleConfig, updateOidcConfigsByKey } =
queries.logtoConfigs;
const { getOidcConfigs, upsertJwtCustomizer } = logtoConfigs;
const { getOidcConfigs, upsertJwtCustomizer, getJwtCustomizer } = logtoConfigs;

router.get(
'/configs/admin-console',
Expand Down Expand Up @@ -254,8 +254,11 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
const {
params: { tokenTypePath },
} = ctx.guard;

const { value } = await getJwtCustomizer(getLogtoJwtTokenKey(tokenTypePath));
const { value } = await getJwtCustomizer(
tokenTypePath === LogtoJwtTokenPath.AccessToken
? LogtoJwtTokenKey.AccessToken
: LogtoJwtTokenKey.ClientCredentials
);
ctx.body = value;
return next();
}
Expand Down

0 comments on commit 5d55776

Please sign in to comment.