/* * Copyright (c) 2022. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ import { ForbiddenError, NotFoundError } from '@ebec/http'; import { PermissionName, isRealmResourceWritable } from '@authup/common'; import type { Request, Response } from 'routup'; import { send, sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { IdentityProviderRepository } from '@authup/server-database'; import { useRequestEnv } from '../../../utils/env'; import { runOauth2ProviderValidation } from '../utils'; import { RequestHandlerOperation } from '../../../request/constants'; export async function updateIdentityProviderRouteHandler(req: Request, res: Response) : Promise { const id = useRequestParam(req, 'id'); const ability = useRequestEnv(req, 'ability'); if (!ability.has(PermissionName.PROVIDER_EDIT)) { throw new ForbiddenError(); } const result = await runOauth2ProviderValidation(req, RequestHandlerOperation.UPDATE); if (!result.data) { return sendAccepted(res); } const dataSource = await useDataSource(); const repository = new IdentityProviderRepository(dataSource); let entity = await repository.findOneBy({ id }); if (!entity) { throw new NotFoundError(); } if (!isRealmResourceWritable(useRequestEnv(req, 'realm'), entity.realm_id)) { throw new ForbiddenError(); } entity = repository.merge(entity, result.data); await repository.save(entity); await repository.saveAttributes(entity.id, result.meta.attributes); await repository.extendEntity(entity); return sendAccepted(res, entity); }