mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor(core): throw proper error in OIDC functions (#2331)
This commit is contained in:
parent
183ebfd856
commit
60a23f1b19
6 changed files with 33 additions and 9 deletions
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"exec": "tsc -p tsconfig.build.json --incremental && node ./build/index.js",
|
||||
"exec": "tsc -p tsconfig.build.json --incremental && node ./build/index.js || exit 1",
|
||||
"ignore": [
|
||||
"node_modules/**/node_modules"
|
||||
],
|
||||
|
|
|
@ -27,11 +27,6 @@ jest.mock('@/queries/oidc-model-instance', () => ({
|
|||
revokeInstanceByGrantId: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock('@logto/shared', () => ({
|
||||
// eslint-disable-next-line unicorn/consistent-function-scoping
|
||||
buildIdGenerator: jest.fn(() => () => 'randomId'),
|
||||
}));
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
jest.mock(
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import type { CreateApplication, OidcClientMetadata } from '@logto/schemas';
|
||||
import { ApplicationType } from '@logto/schemas';
|
||||
import { adminConsoleApplicationId, demoAppApplicationId } from '@logto/schemas/lib/seeds';
|
||||
import { tryThat } from '@logto/shared';
|
||||
import { addSeconds } from 'date-fns';
|
||||
import type { AdapterFactory, AllClientMetadata } from 'oidc-provider';
|
||||
import { errors } from 'oidc-provider';
|
||||
import snakecaseKeys from 'snakecase-keys';
|
||||
|
||||
import envSet, { MountedApps } from '@/env-set';
|
||||
|
@ -83,7 +85,9 @@ export default function postgresAdapter(modelName: string): ReturnType<AdapterFa
|
|||
return buildAdminConsoleClientMetadata();
|
||||
}
|
||||
|
||||
return transpileClient(await findApplicationById(id));
|
||||
return transpileClient(
|
||||
await tryThat(findApplicationById(id), new errors.InvalidClient(`invalid client ${id}`))
|
||||
);
|
||||
},
|
||||
findByUserCode: reject,
|
||||
findByUid: reject,
|
||||
|
|
|
@ -4,6 +4,7 @@ import { readFileSync } from 'fs';
|
|||
|
||||
import { userClaims } from '@logto/core-kit';
|
||||
import { CustomClientMetadataKey } from '@logto/schemas';
|
||||
import { tryThat } from '@logto/shared';
|
||||
import type Koa from 'koa';
|
||||
import mount from 'koa-mount';
|
||||
import { Provider, errors } from 'oidc-provider';
|
||||
|
@ -162,7 +163,10 @@ export default async function initOidc(app: Koa): Promise<Provider> {
|
|||
extraTokenClaims: async (_ctx, token) => {
|
||||
if (token.kind === 'AccessToken') {
|
||||
const { accountId } = token;
|
||||
const { roleNames } = await findUserById(accountId);
|
||||
const { roleNames } = await tryThat(
|
||||
findUserById(accountId),
|
||||
new errors.InvalidClient(`invalid user ${accountId}`)
|
||||
);
|
||||
|
||||
return snakecaseKeys({
|
||||
roleNames,
|
||||
|
@ -172,7 +176,11 @@ export default async function initOidc(app: Koa): Promise<Provider> {
|
|||
// `token.kind === 'ClientCredentials'`
|
||||
const { clientId } = token;
|
||||
assertThat(clientId, 'oidc.invalid_grant');
|
||||
const { roleNames } = await findApplicationById(clientId);
|
||||
|
||||
const { roleNames } = await tryThat(
|
||||
findApplicationById(clientId),
|
||||
new errors.InvalidClient(`invalid client ${clientId}`)
|
||||
);
|
||||
|
||||
return snakecaseKeys({ roleNames });
|
||||
},
|
||||
|
|
16
packages/shared/src/utils/function.ts
Normal file
16
packages/shared/src/utils/function.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
export const tryThat = async <T, E extends Error>(
|
||||
exec: Promise<T> | (() => Promise<T>),
|
||||
onError: E | ((error: unknown) => never)
|
||||
): Promise<T> => {
|
||||
try {
|
||||
return await (typeof exec === 'function' ? exec() : exec);
|
||||
} catch (error: unknown) {
|
||||
if (onError instanceof Error) {
|
||||
// https://github.com/typescript-eslint/typescript-eslint/issues/3814
|
||||
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
||||
throw onError;
|
||||
}
|
||||
|
||||
return onError(error);
|
||||
}
|
||||
};
|
|
@ -1,2 +1,3 @@
|
|||
export * from './id';
|
||||
export * from './function';
|
||||
export { default as findPackage } from './find-package';
|
||||
|
|
Loading…
Reference in a new issue