ekyc

Electronic KYC process with uploading ID document using OAuth 2.1 (experimental)
Log | Files | Refs | README | LICENSE

authorize.ts (1481B)


      1 import { ClientRepository } from "#core/application/oauth2/client_repository.ts";
      2 import { EntityNotFound } from "#core/application/repository_error.ts";
      3 import { InvalidOAuth2Flow, OAuth2Flow } from "#core/domain/oauth2flow.ts";
      4 import { InvalidUUID, UUID } from "#core/domain/uuid.ts";
      5 
      6 export type OAuth2FlowAuthorizeRequest = {
      7   clientId: string;
      8   flowId: string;
      9   resourceOwner: string;
     10 };
     11 
     12 export type OAuth2FlowAuthorizeResponse = {
     13   authorized: boolean;
     14   redirectUri: URL | null;
     15 };
     16 
     17 export class OAuth2FlowAuthorizeUseCase {
     18   constructor(
     19     private readonly clientRepo: ClientRepository,
     20     private readonly flowRepo: Repository<OAuth2Flow>,
     21   ) {}
     22 
     23   async execute(
     24     request: OAuth2FlowAuthorizeRequest,
     25   ): Promise<OAuth2FlowAuthorizeResponse> {
     26     try {
     27       const clientId = new UUID(request.clientId);
     28       const resourceOwner = new UUID(request.resourceOwner);
     29       const client = await this.clientRepo.find(clientId);
     30       const flow = await this.flowRepo.find(request.flowId);
     31       const redirectUri = client.authorize(flow, resourceOwner);
     32       await this.flowRepo.store(flow);
     33       return {
     34         authorized: true,
     35         redirectUri,
     36       };
     37     } catch (error) {
     38       if (
     39         error instanceof InvalidUUID ||
     40         error instanceof EntityNotFound ||
     41         error instanceof InvalidOAuth2Flow
     42       ) {
     43         return {
     44           authorized: false,
     45           redirectUri: null,
     46         };
     47       }
     48       throw error;
     49     }
     50   }
     51 }