ekyc

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

validate.ts (1134B)


      1 import { ClientRepository } from "./client_repository.ts";
      2 import { EntityNotFound } from "../repository_error.ts";
      3 import { Scope } from "#core/domain/scope.ts";
      4 import { UUID } from "#core/domain/uuid.ts";
      5 
      6 export type OAuth2FlowValidateRequest = {
      7   clientId: string;
      8   scope: string | null;
      9 };
     10 
     11 export type OAuth2FlowValidateResponse = {
     12   valid: boolean;
     13   scope: string[];
     14   description: string | null;
     15 };
     16 
     17 export class OAuth2FlowValidateUseCase {
     18   constructor(private readonly clientRepo: ClientRepository) {
     19   }
     20 
     21   async execute(
     22     request: OAuth2FlowValidateRequest,
     23   ): Promise<OAuth2FlowValidateResponse> {
     24     try {
     25       const clientId = new UUID(request.clientId);
     26       const scope = Scope.of(request.scope);
     27       const client = await this.clientRepo.find(clientId);
     28       return {
     29         valid: true,
     30         scope: client.scope.intersect(scope).values,
     31         description: client.description,
     32       };
     33     } catch (error) {
     34       if (error instanceof EntityNotFound) {
     35         return {
     36           valid: false,
     37           scope: [],
     38           description: null,
     39         };
     40       }
     41       throw error;
     42     }
     43   }
     44 }