import * as Hapi from '@hapi/hapi' ; import * as Joi from 'joi' ; import { authenticateSystem , createToken } from '@auth/features/authenticate/service' ; import { unauthorized } from '@hapi/boom' ; import { WEB_USER_JWT_AUDIENCES , JWT_ISSUER , NOTIFICATION_API_USER_AUDIENCE , VALIDATOR_API_USER_AUDIENCE , AGE_VERIFICATION_USER_AUDIENCE , NATIONAL_ID_USER_AUDIENCE , USER_MANAGEMENT_URL } from '@auth/constants' ; import fetch from 'node-fetch' ; import { resolve } from 'url' ; interface ISystemAuthPayload { client_id : string ; client_secret : string ; } interface ISystemAuthResponse { token ? : string ; } export async function authenticateSystemClientHandler ( request , h ) { const payload = request . payload as ISystemAuthPayload ; let result ; try { result = await authenticateSystem ( payload . client_id , payload . client_secret ) ; } catch ( err ) { throw unauthorized ( ) ; } const isNotificationAPIUser = result . scope . indexOf ( 'notification-api' ) > - 1 ; const isValidatorAPIUser = result . scope . indexOf ( 'validator-api' ) > - 1 ; const isAgeVerificationAPIUser = result . scope . indexOf ( 'age-verification-api' ) > - 1 ; const isNationalIDAPIUser = result . scope . indexOf ( 'nationalId' ) > - 1 ; const response = { } ; response . token = await createToken ( result . systemId , result . scope , isNotificationAPIUser ? WEB_USER_JWT_AUDIENCES . concat ( [ NOTIFICATION_API_USER_AUDIENCE ] ) : isValidatorAPIUser ? WEB_USER_JWT_AUDIENCES . concat ( [ VALIDATOR_API_USER_AUDIENCE ] ) : isAgeVerificationAPIUser ? WEB_USER_JWT_AUDIENCES . concat ( [ AGE_VERIFICATION_USER_AUDIENCE ] ) : isNationalIDAPIUser ? WEB_USER_JWT_AUDIENCES . concat ( [ NATIONAL_ID_USER_AUDIENCE ] ) : WEB_USER_JWT_AUDIENCES , JWT_ISSUER , true ) ; return response ; } export const requestSchema = Joi . object ( { client_id : Joi . string ( ) , client_secret : Joi . string ( ) } ) ; export const responseSchema = Joi . object ( { token : Joi . string ( ) . optional ( ) } ) ; export async function registerSystemClient ( request , h ) { const authHeader = { Authorization : request . headers . authorization } ; const url = resolve ( USER_MANAGEMENT_URL , '/registerSystemClient' ) ; const res = await fetch ( url , { method : 'POST' , body : JSON . stringify ( request . payload ) , headers : { 'Content-Type' : 'application/json' , ... authHeader } } ) . then ( response => { return response . json ( ) ; } ) . catch ( error => { return Promise . reject ( new Error ( ` request failed: ${ error . message }` ) ) ; } ) ; return res ; } export const registerRquestSchema = Joi . object ( { scope : Joi . string ( ) } ) ;