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

fix: update according to CR

This commit is contained in:
Darcy Ye 2024-11-29 14:56:53 +08:00
parent 225bdf4ba9
commit d55112b498
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
5 changed files with 35 additions and 23 deletions

View file

@ -6,6 +6,7 @@ import {
import { generateStandardId } from '@logto/shared'; import { generateStandardId } from '@logto/shared';
import { removeUndefinedKeys } from '@silverhand/essentials'; import { removeUndefinedKeys } from '@silverhand/essentials';
import RequestError from '#src/errors/RequestError/index.js';
import type Queries from '#src/tenants/Queries.js'; import type Queries from '#src/tenants/Queries.js';
import assertThat from '#src/utils/assert-that.js'; import assertThat from '#src/utils/assert-that.js';
@ -42,7 +43,13 @@ export const createSamlApplicationsLibrary = (queries: Queries) => {
const findSamlApplicationById = async (id: string): Promise<SamlApplicationResponse> => { const findSamlApplicationById = async (id: string): Promise<SamlApplicationResponse> => {
const application = await findApplicationById(id); const application = await findApplicationById(id);
assertThat(application.type === ApplicationType.SAML, 'application.saml.saml_application_only'); assertThat(
application.type === ApplicationType.SAML,
new RequestError({
code: 'application.saml.saml_application_only',
status: 422,
})
);
const samlConfig = await findSamlApplicationConfigByApplicationId(application.id); const samlConfig = await findSamlApplicationConfigByApplicationId(application.id);
@ -53,24 +60,20 @@ export const createSamlApplicationsLibrary = (queries: Queries) => {
id: string, id: string,
patchApplicationObject: PatchSamlApplication patchApplicationObject: PatchSamlApplication
): Promise<SamlApplicationResponse> => { ): Promise<SamlApplicationResponse> => {
const { name, description, customData, config } = patchApplicationObject; const { config, ...applicationData } = patchApplicationObject;
const originalApplication = await findApplicationById(id); const originalApplication = await findApplicationById(id);
assertThat( assertThat(
originalApplication.type === ApplicationType.SAML, originalApplication.type === ApplicationType.SAML,
'application.saml.saml_application_only' new RequestError({
code: 'application.saml.saml_application_only',
status: 422,
})
); );
const [updatedApplication, upToDateSamlConfig] = await Promise.all([ const [updatedApplication, upToDateSamlConfig] = await Promise.all([
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing Object.keys(applicationData).length > 0
name || description || customData ? updateApplicationById(id, removeUndefinedKeys(applicationData))
? updateApplicationById(
id,
removeUndefinedKeys({
name,
description,
customData,
})
)
: originalApplication, : originalApplication,
config config
? updateSamlApplicationConfig({ ? updateSamlApplicationConfig({

View file

@ -90,8 +90,9 @@ export const ensembleSamlApplication = ({
* Only HTTP-POST binding is supported for receiving SAML assertions at the moment. * Only HTTP-POST binding is supported for receiving SAML assertions at the moment.
*/ */
export const validateAcsUrl = (acsUrl: SamlAcsUrl) => { export const validateAcsUrl = (acsUrl: SamlAcsUrl) => {
const { binding } = acsUrl;
assertThat( assertThat(
acsUrl.binding === BindingType.POST, binding === BindingType.POST,
new RequestError({ new RequestError({
code: 'application.saml.acs_url_binding_not_supported', code: 'application.saml.acs_url_binding_not_supported',
status: 422, status: 422,

View file

@ -8,13 +8,15 @@ import { generateStandardId } from '@logto/shared';
import { removeUndefinedKeys } from '@silverhand/essentials'; import { removeUndefinedKeys } from '@silverhand/essentials';
import { z } from 'zod'; import { z } from 'zod';
import RequestError from '#src/errors/RequestError/index.js';
import koaGuard from '#src/middleware/koa-guard.js'; import koaGuard from '#src/middleware/koa-guard.js';
import { buildOidcClientMetadata } from '#src/oidc/utils.js'; import { buildOidcClientMetadata } from '#src/oidc/utils.js';
import { generateInternalSecret } from '#src/routes/applications/application-secret.js'; import { generateInternalSecret } from '#src/routes/applications/application-secret.js';
import type { ManagementApiRouter, RouterInitArgs } from '#src/routes/types.js'; import type { ManagementApiRouter, RouterInitArgs } from '#src/routes/types.js';
import { ensembleSamlApplication, validateAcsUrl } from '#src/saml-applications/libraries/utils.js';
import assertThat from '#src/utils/assert-that.js'; import assertThat from '#src/utils/assert-that.js';
import { ensembleSamlApplication, validateAcsUrl } from '../libraries/utils.js';
export default function samlApplicationRoutes<T extends ManagementApiRouter>( export default function samlApplicationRoutes<T extends ManagementApiRouter>(
...[router, { queries, libraries }]: RouterInitArgs<T> ...[router, { queries, libraries }]: RouterInitArgs<T>
) { ) {
@ -84,7 +86,7 @@ export default function samlApplicationRoutes<T extends ManagementApiRouter>(
id: z.string(), id: z.string(),
}), }),
response: samlApplicationResponseGuard, response: samlApplicationResponseGuard,
status: [200, 400, 404], status: [200, 404, 422],
}), }),
async (ctx, next) => { async (ctx, next) => {
const { id } = ctx.guard.params; const { id } = ctx.guard.params;
@ -104,7 +106,7 @@ export default function samlApplicationRoutes<T extends ManagementApiRouter>(
params: z.object({ id: z.string() }), params: z.object({ id: z.string() }),
body: samlApplicationPatchGuard, body: samlApplicationPatchGuard,
response: samlApplicationResponseGuard, response: samlApplicationResponseGuard,
status: [200, 400, 404], status: [200, 404, 422],
}), }),
async (ctx, next) => { async (ctx, next) => {
const { id } = ctx.guard.params; const { id } = ctx.guard.params;
@ -122,13 +124,19 @@ export default function samlApplicationRoutes<T extends ManagementApiRouter>(
'/saml-applications/:id', '/saml-applications/:id',
koaGuard({ koaGuard({
params: z.object({ id: z.string() }), params: z.object({ id: z.string() }),
status: [204, 400, 404], status: [204, 422, 404],
}), }),
async (ctx, next) => { async (ctx, next) => {
const { id } = ctx.guard.params; const { id } = ctx.guard.params;
const { type } = await findApplicationById(id); const { type } = await findApplicationById(id);
assertThat(type === ApplicationType.SAML, 'application.saml.saml_application_only'); assertThat(
type === ApplicationType.SAML,
new RequestError({
code: 'application.saml.saml_application_only',
status: 422,
})
);
await deleteApplicationById(id); await deleteApplicationById(id);

View file

@ -91,15 +91,15 @@ describe('SAML application', () => {
await expectRejects(deleteSamlApplication(application.id), { await expectRejects(deleteSamlApplication(application.id), {
code: 'application.saml.saml_application_only', code: 'application.saml.saml_application_only',
status: 400, status: 422,
}); });
await expectRejects(updateSamlApplication(application.id, { name: 'updated' }), { await expectRejects(updateSamlApplication(application.id, { name: 'updated' }), {
code: 'application.saml.saml_application_only', code: 'application.saml.saml_application_only',
status: 400, status: 422,
}); });
await expectRejects(getSamlApplication(application.id), { await expectRejects(getSamlApplication(application.id), {
code: 'application.saml.saml_application_only', code: 'application.saml.saml_application_only',
status: 400, status: 422,
}); });
await deleteApplication(application.id); await deleteApplication(application.id);
}); });

View file

@ -19,5 +19,5 @@ export type SamlAcsUrl = {
export const samlAcsUrlGuard = z.object({ export const samlAcsUrlGuard = z.object({
binding: z.nativeEnum(BindingType), binding: z.nativeEnum(BindingType),
url: z.string(), url: z.string().url(),
}) satisfies ToZodObject<SamlAcsUrl>; }) satisfies ToZodObject<SamlAcsUrl>;