mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor(core): refactor
This commit is contained in:
parent
e327754008
commit
5d55776d60
5 changed files with 31 additions and 6 deletions
|
@ -35,6 +35,7 @@ const logtoConfigs: LogtoConfigLibrary = {
|
|||
}),
|
||||
getOidcConfigs: jest.fn(),
|
||||
upsertJwtCustomizer: jest.fn(),
|
||||
getJwtCustomizer: jest.fn(),
|
||||
};
|
||||
|
||||
describe('getAccessToken()', () => {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import {
|
||||
LogtoConfigs,
|
||||
cloudApiIndicator,
|
||||
cloudConnectionDataGuard,
|
||||
logtoOidcConfigGuard,
|
||||
|
@ -6,13 +7,16 @@ import {
|
|||
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: {
|
||||
|
@ -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 };
|
||||
};
|
||||
|
|
|
@ -58,6 +58,7 @@ const cloudConnection = createCloudConnectionLibrary({
|
|||
}),
|
||||
getOidcConfigs: jest.fn(),
|
||||
upsertJwtCustomizer: jest.fn(),
|
||||
getJwtCustomizer: jest.fn(),
|
||||
});
|
||||
|
||||
const getLogtoConnectors = jest.spyOn(connectorLibrary, 'getLogtoConnectors');
|
||||
|
|
|
@ -54,7 +54,6 @@ const logtoConfigQueries = {
|
|||
}),
|
||||
updateOidcConfigsByKey: jest.fn(),
|
||||
getRowsByKeys: jest.fn(async () => mockLogtoConfigRows),
|
||||
getJwtCustomizer: jest.fn(),
|
||||
};
|
||||
|
||||
const logtoConfigLibraries = {
|
||||
|
@ -63,6 +62,7 @@ const logtoConfigLibraries = {
|
|||
[LogtoOidcConfigKey.CookieKeys]: mockCookieKeys,
|
||||
})),
|
||||
upsertJwtCustomizer: jest.fn(),
|
||||
getJwtCustomizer: jest.fn(),
|
||||
};
|
||||
|
||||
const settingRoutes = await pickDefault(import('./logto-config.js'));
|
||||
|
@ -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');
|
||||
|
|
|
@ -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',
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue