import { Provider } from 'oidc-provider' ; import { Injectable } from '@nestjs/common' ; import { LoggerService } from '@fc/logger' ; import { OidcProviderGrantSaveException } from '../exceptions/oidc-provider-grant-save.exception' ; import { InteractionParams } from '../interfaces' ; @ Injectable ( ) export class OidcProviderGrantService { constructor ( private readonly logger ) { this . logger . setContext ( this . constructor . name ) ; } async generateGrant ( provider , req , res , accountId ) { const details = await provider . interactionDetails ( req , res ) ; const params = details . params as InteractionParams ; const grant = new provider . Grant ( ) ; grant . accountId = accountId ; grant . clientId = params . client_id as string ; params . scope . split ( ' ' ) . forEach ( ( scope ) => { grant . addOIDCScope ( scope ) ; } ) ; return grant ; } async saveGrant ( grant ) { try { const result = await grant . save ( ) ; return result ; } catch ( error ) { throw new OidcProviderGrantSaveException ( error ) ; } } }