0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(core): revert add oidc config to application response (#566)

This commit is contained in:
Xiao Yijun 2022-04-18 22:30:57 +08:00 committed by GitHub
parent afaaed9f29
commit 975e715c47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 108 deletions

View file

@ -1,4 +1,4 @@
import { Application, ApplicationDTO } from '@logto/schemas'; import { Application, SnakeCaseOidcConfig } from '@logto/schemas';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form'; import { Controller, useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
@ -36,11 +36,15 @@ const ApplicationDetails = () => {
const location = useLocation(); const location = useLocation();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const { data, error, mutate } = useSWR<ApplicationDTO, RequestError>( const { data, error, mutate } = useSWR<Application, RequestError>(
id && `/api/applications/${id}` id && `/api/applications/${id}`
); );
const isLoading = !data && !error; const { data: oidcConfig, error: fetchOidcConfigError } = useSWR<
SnakeCaseOidcConfig,
RequestError
>('/oidc/.well-known/openid-configuration');
const isLoading = !data && !error && !oidcConfig && !fetchOidcConfigError;
const [isReadmeOpen, setIsReadmeOpen] = useState(false); const [isReadmeOpen, setIsReadmeOpen] = useState(false);
@ -80,14 +84,14 @@ const ApplicationDetails = () => {
}, },
}, },
}) })
.json<ApplicationDTO>(); .json<Application>();
void mutate(updatedApplication); void mutate(updatedApplication);
toast.success(t('application_details.save_success')); toast.success(t('application_details.save_success'));
}); });
const isAdvancedSettings = location.pathname.includes('advanced-settings'); const isAdvancedSettings = location.pathname.includes('advanced-settings');
const SettingsPage = data && ( const SettingsPage = oidcConfig && (
<> <>
<FormField <FormField
isRequired isRequired
@ -103,10 +107,7 @@ const ApplicationDetails = () => {
title="admin_console.application_details.authorization_endpoint" title="admin_console.application_details.authorization_endpoint"
className={styles.textField} className={styles.textField}
> >
<CopyToClipboard <CopyToClipboard className={styles.textField} value={oidcConfig.authorization_endpoint} />
className={styles.textField}
value={data.oidcConfig.authorizationEndpoint}
/>
</FormField> </FormField>
<FormField <FormField
isRequired isRequired
@ -163,13 +164,13 @@ const ApplicationDetails = () => {
</> </>
); );
const AdvancedSettingsPage = data && ( const AdvancedSettingsPage = oidcConfig && (
<> <>
<FormField title="admin_console.application_details.token_endpoint"> <FormField title="admin_console.application_details.token_endpoint">
<CopyToClipboard className={styles.textField} value={data.oidcConfig.tokenEndpoint} /> <CopyToClipboard className={styles.textField} value={oidcConfig.token_endpoint} />
</FormField> </FormField>
<FormField title="admin_console.application_details.user_info_endpoint"> <FormField title="admin_console.application_details.user_info_endpoint">
<CopyToClipboard className={styles.textField} value={data.oidcConfig.userinfoEndpoint} /> <CopyToClipboard className={styles.textField} value={oidcConfig.userinfo_endpoint} />
</FormField> </FormField>
</> </>
); );
@ -184,7 +185,7 @@ const ApplicationDetails = () => {
/> />
{isLoading && <div>loading</div>} {isLoading && <div>loading</div>}
{error && <div>{`error occurred: ${error.body.message}`}</div>} {error && <div>{`error occurred: ${error.body.message}`}</div>}
{data && ( {data && oidcConfig && (
<> <>
<Card className={styles.header}> <Card className={styles.header}>
<ImagePlaceholder size={76} borderRadius={16} /> <ImagePlaceholder size={76} borderRadius={16} />

View file

@ -1,4 +1,4 @@
import { ApplicationDTO } from '@logto/schemas'; import { Application } from '@logto/schemas';
import classNames from 'classnames'; import classNames from 'classnames';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
@ -33,7 +33,7 @@ const Applications = () => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const [query, setQuery] = useSearchParams(); const [query, setQuery] = useSearchParams();
const pageIndex = Number(query.get('page') ?? '1'); const pageIndex = Number(query.get('page') ?? '1');
const { data, error, mutate } = useSWR<[ApplicationDTO[], number], RequestError>( const { data, error, mutate } = useSWR<[Application[], number], RequestError>(
`/api/applications?page=${pageIndex}&page_size=${pageSize}` `/api/applications?page=${pageIndex}&page_size=${pageSize}`
); );
const isLoading = !data && !error; const isLoading = !data && !error;

View file

@ -23,7 +23,6 @@
"@logto/phrases": "^0.1.0", "@logto/phrases": "^0.1.0",
"@logto/schemas": "^0.1.0", "@logto/schemas": "^0.1.0",
"@silverhand/essentials": "^1.1.0", "@silverhand/essentials": "^1.1.0",
"camelcase-keys": "^7.0.2",
"dayjs": "^1.10.5", "dayjs": "^1.10.5",
"decamelize": "^5.0.0", "decamelize": "^5.0.0",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",

View file

@ -1,29 +1,20 @@
import { import {
Application, Application,
ApplicationDTO,
ApplicationType, ApplicationType,
Passcode, Passcode,
PasscodeType, PasscodeType,
Resource, Resource,
Role, Role,
Setting, Setting,
SnakeCaseOidcConfig,
UserLog, UserLog,
UserLogResult, UserLogResult,
UserLogType, UserLogType,
} from '@logto/schemas'; } from '@logto/schemas';
import camelcaseKeys from 'camelcase-keys';
export * from './connector'; export * from './connector';
export * from './sign-in-experience'; export * from './sign-in-experience';
export * from './user'; export * from './user';
export const mockOidcConfig: SnakeCaseOidcConfig = {
authorization_endpoint: 'https://logto.dev/oidc/auth',
userinfo_endpoint: 'https://logto.dev/oidc/userinfo',
token_endpoint: 'https://logto.dev/oidc/token',
};
export const mockApplication: Application = { export const mockApplication: Application = {
id: 'foo', id: 'foo',
name: 'foo', name: 'foo',
@ -41,11 +32,6 @@ export const mockApplication: Application = {
createdAt: 1_645_334_775_356, createdAt: 1_645_334_775_356,
}; };
export const mockApplicationDTO: ApplicationDTO = {
...mockApplication,
oidcConfig: camelcaseKeys(mockOidcConfig),
};
export const mockResource: Resource = { export const mockResource: Resource = {
id: 'logto_api', id: 'logto_api',
name: 'management api', name: 'management api',

View file

@ -1,8 +1,6 @@
import { Application, CreateApplication, ApplicationType } from '@logto/schemas'; import { Application, CreateApplication, ApplicationType } from '@logto/schemas';
import nock from 'nock';
import { mockApplicationDTO, mockOidcConfig } from '@/__mocks__'; import { mockApplication } from '@/__mocks__';
import { port } from '@/env/consts';
import { findApplicationById } from '@/queries/application'; import { findApplicationById } from '@/queries/application';
import { createRequester } from '@/utils/test-utils'; import { createRequester } from '@/utils/test-utils';
@ -10,22 +8,22 @@ import applicationRoutes from './application';
jest.mock('@/queries/application', () => ({ jest.mock('@/queries/application', () => ({
findTotalNumberOfApplications: jest.fn(async () => ({ count: 10 })), findTotalNumberOfApplications: jest.fn(async () => ({ count: 10 })),
findAllApplications: jest.fn(async () => [mockApplicationDTO]), findAllApplications: jest.fn(async () => [mockApplication]),
findApplicationById: jest.fn(async () => mockApplicationDTO), findApplicationById: jest.fn(async () => mockApplication),
deleteApplicationById: jest.fn(), deleteApplicationById: jest.fn(),
insertApplication: jest.fn( insertApplication: jest.fn(
async (body: CreateApplication): Promise<Application> => ({ async (body: CreateApplication): Promise<Application> => ({
...mockApplicationDTO, ...mockApplication,
...body, ...body,
oidcClientMetadata: { oidcClientMetadata: {
...mockApplicationDTO.oidcClientMetadata, ...mockApplication.oidcClientMetadata,
...body.oidcClientMetadata, ...body.oidcClientMetadata,
}, },
}) })
), ),
updateApplicationById: jest.fn( updateApplicationById: jest.fn(
async (_, data: Partial<CreateApplication>): Promise<Application> => ({ async (_, data: Partial<CreateApplication>): Promise<Application> => ({
...mockApplicationDTO, ...mockApplication,
...data, ...data,
}) })
), ),
@ -45,15 +43,10 @@ const customClientMetadata = {
describe('application route', () => { describe('application route', () => {
const applicationRequest = createRequester({ authedRoutes: applicationRoutes }); const applicationRequest = createRequester({ authedRoutes: applicationRoutes });
beforeEach(() => {
const discoveryUrl = `http://localhost:${port}/oidc/.well-known/openid-configuration`;
nock(discoveryUrl).get('').reply(200, mockOidcConfig);
});
it('GET /applications', async () => { it('GET /applications', async () => {
const response = await applicationRequest.get('/applications'); const response = await applicationRequest.get('/applications');
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual([mockApplicationDTO]); expect(response.body).toEqual([mockApplication]);
expect(response.header).toHaveProperty('total-number', '10'); expect(response.header).toHaveProperty('total-number', '10');
}); });
@ -67,7 +60,7 @@ describe('application route', () => {
.send({ name, type, description }); .send({ name, type, description });
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual({ expect(response.body).toEqual({
...mockApplicationDTO, ...mockApplication,
id: 'randomId', id: 'randomId',
name, name,
description, description,
@ -84,7 +77,7 @@ describe('application route', () => {
.send({ name, type, customClientMetadata }); .send({ name, type, customClientMetadata });
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual({ expect(response.body).toEqual({
...mockApplicationDTO, ...mockApplication,
id: 'randomId', id: 'randomId',
name, name,
type, type,
@ -123,7 +116,7 @@ describe('application route', () => {
const response = await applicationRequest.get('/applications/foo'); const response = await applicationRequest.get('/applications/foo');
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual(mockApplicationDTO); expect(response.body).toEqual(mockApplication);
}); });
it('PATCH /applications/:applicationId', async () => { it('PATCH /applications/:applicationId', async () => {
@ -134,12 +127,7 @@ describe('application route', () => {
.patch('/applications/foo') .patch('/applications/foo')
.send({ name, description, customClientMetadata }); .send({ name, description, customClientMetadata });
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual({ expect(response.body).toEqual({ ...mockApplication, name, description, customClientMetadata });
...mockApplicationDTO,
name,
description,
customClientMetadata,
});
}); });
it('PATCH /applications/:applicationId expect to throw with invalid properties', async () => { it('PATCH /applications/:applicationId expect to throw with invalid properties', async () => {

View file

@ -1,9 +1,6 @@
import { Applications, SnakeCaseOidcConfig } from '@logto/schemas'; import { Applications } from '@logto/schemas';
import camelcaseKeys from 'camelcase-keys';
import got from 'got';
import { object, string } from 'zod'; import { object, string } from 'zod';
import { port } from '@/env/consts';
import koaGuard from '@/middleware/koa-guard'; import koaGuard from '@/middleware/koa-guard';
import koaPagination from '@/middleware/koa-pagination'; import koaPagination from '@/middleware/koa-pagination';
import { buildOidcClientMetadata } from '@/oidc/utils'; import { buildOidcClientMetadata } from '@/oidc/utils';
@ -21,24 +18,18 @@ import { AuthedRouter } from './types';
const applicationId = buildIdGenerator(21); const applicationId = buildIdGenerator(21);
const discoveryUrl = `http://localhost:${port}/oidc/.well-known/openid-configuration`;
export default function applicationRoutes<T extends AuthedRouter>(router: T) { export default function applicationRoutes<T extends AuthedRouter>(router: T) {
router.get('/applications', koaPagination(), async (ctx, next) => { router.get('/applications', koaPagination(), async (ctx, next) => {
const { limit, offset } = ctx.pagination; const { limit, offset } = ctx.pagination;
const [{ count }, applications, oidcConfig] = await Promise.all([ const [{ count }, applications] = await Promise.all([
findTotalNumberOfApplications(), findTotalNumberOfApplications(),
findAllApplications(limit, offset), findAllApplications(limit, offset),
got(discoveryUrl).json<SnakeCaseOidcConfig>(),
]); ]);
// Return totalCount to pagination middleware // Return totalCount to pagination middleware
ctx.pagination.totalCount = count; ctx.pagination.totalCount = count;
ctx.body = applications.map((application) => ({ ctx.body = applications;
...application,
oidcConfig: camelcaseKeys(oidcConfig),
}));
return next(); return next();
}); });
@ -54,19 +45,11 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
async (ctx, next) => { async (ctx, next) => {
const { oidcClientMetadata, ...rest } = ctx.guard.body; const { oidcClientMetadata, ...rest } = ctx.guard.body;
const [application, oidcConfig] = await Promise.all([ ctx.body = await insertApplication({
insertApplication({ id: applicationId(),
id: applicationId(), oidcClientMetadata: buildOidcClientMetadata(oidcClientMetadata),
oidcClientMetadata: buildOidcClientMetadata(oidcClientMetadata), ...rest,
...rest, });
}),
got(discoveryUrl).json<SnakeCaseOidcConfig>(),
]);
ctx.body = {
...application,
oidcConfig: camelcaseKeys(oidcConfig),
};
return next(); return next();
} }
@ -82,15 +65,7 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
params: { id }, params: { id },
} = ctx.guard; } = ctx.guard;
const [application, oidcConfig] = await Promise.all([ ctx.body = await findApplicationById(id);
findApplicationById(id),
got(discoveryUrl).json<SnakeCaseOidcConfig>(),
]);
ctx.body = {
...application,
oidcConfig: camelcaseKeys(oidcConfig),
};
return next(); return next();
} }
@ -108,17 +83,9 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
body, body,
} = ctx.guard; } = ctx.guard;
const [application, oidcConfig] = await Promise.all([ ctx.body = await updateApplicationById(id, {
updateApplicationById(id, { ...body,
...body, });
}),
got(discoveryUrl).json<SnakeCaseOidcConfig>(),
]);
ctx.body = {
...application,
oidcConfig: camelcaseKeys(oidcConfig),
};
return next(); return next();
} }

View file

@ -1,6 +0,0 @@
import { Application } from '../db-entries';
import { OidcConfig } from './oidc-config';
export interface ApplicationDTO extends Application {
oidcConfig: OidcConfig;
}

View file

@ -1,4 +1,3 @@
export * from './user'; export * from './user';
export * from './connector'; export * from './connector';
export * from './oidc-config'; export * from './oidc-config';
export * from './application';

View file

@ -167,7 +167,6 @@ importers:
'@types/node': ^16.3.1 '@types/node': ^16.3.1
'@types/oidc-provider': ^7.8.0 '@types/oidc-provider': ^7.8.0
'@types/supertest': ^2.0.11 '@types/supertest': ^2.0.11
camelcase-keys: ^7.0.2
copyfiles: ^2.4.1 copyfiles: ^2.4.1
dayjs: ^1.10.5 dayjs: ^1.10.5
decamelize: ^5.0.0 decamelize: ^5.0.0
@ -209,7 +208,6 @@ importers:
'@logto/phrases': link:../phrases '@logto/phrases': link:../phrases
'@logto/schemas': link:../schemas '@logto/schemas': link:../schemas
'@silverhand/essentials': 1.1.2 '@silverhand/essentials': 1.1.2
camelcase-keys: 7.0.2
dayjs: 1.10.7 dayjs: 1.10.7
decamelize: 5.0.1 decamelize: 5.0.1
dotenv: 10.0.0 dotenv: 10.0.0
@ -7378,6 +7376,7 @@ packages:
/camelcase/6.3.0: /camelcase/6.3.0:
resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==}
engines: {node: '>=10'} engines: {node: '>=10'}
dev: false
/caniuse-api/3.0.0: /caniuse-api/3.0.0:
resolution: {integrity: sha512-bsTwuIg/BZZK/vreVTYYbSWoe2F+71P7K5QGEX+pT250DZbfU1MQ5prOKpPR+LL6uWKK3KMwMCAS74QB3Um1uw==} resolution: {integrity: sha512-bsTwuIg/BZZK/vreVTYYbSWoe2F+71P7K5QGEX+pT250DZbfU1MQ5prOKpPR+LL6uWKK3KMwMCAS74QB3Um1uw==}
@ -11901,7 +11900,7 @@ packages:
engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0}
dependencies: dependencies:
'@jest/types': 27.5.1 '@jest/types': 27.5.1
camelcase: 6.3.0 camelcase: 6.2.1
chalk: 4.1.2 chalk: 4.1.2
jest-get-type: 27.5.1 jest-get-type: 27.5.1
leven: 3.1.0 leven: 3.1.0