import { AWSError } from "aws-sdk" ; import { validate } from "email-validator" ; import { v4 as uuid } from "uuid" ; import { Session } from "../../common" ; import { Verification , VerificationCode } from "../../verification" ; import { Client } from "../_" ; param seconds return export function expires ( seconds ) { return new Date ( Date . now ( ) + seconds * 1000 ) ; } param err return export function translate ( err ) { switch ( err . code ) { case "InvalidParameterException" : err . code = "TypeError" ; err . message = "Invalid request" ; return err ; case "UserNotFoundException" : err . code = "NotAuthorizedException" ; err . message = "Incorrect username or password" ; return err ; default : return err ; } } /**
 * Authentication client
 */ export class AuthenticationClient extends Client { param verification public constructor ( protected verification = new Verification ( ) ) { super ( ) ; } param email param password return public async register ( email , password ) { const username = uuid ( ) ; if ( ! validate ( email ) ) throw new Error ( "Invalid email address" ) ; await this . cognito . signUp ( { ClientId : process . env . COGNITO_USER_POOL_CLIENT_ID ! , Username : username , Password : password , UserAttributes : [ { Name : "email" , Value : email } ] } ) . promise ( ) ; return this . verification . issue ( "register" , username ) ; } param username param password return public async authenticateWithCredentials ( username , password ) { try { const { AuthenticationResult , ChallengeName } = await this . cognito . initiateAuth ( { ClientId : process . env . COGNITO_USER_POOL_CLIENT_ID ! , AuthFlow : "USER_PASSWORD_AUTH" , AuthParameters : { USERNAME : username , PASSWORD : password } } ) . promise ( ) ; if ( ! AuthenticationResult ) throw new Error ( `Invalid authentication: challenge "${ ChallengeName }"` ) ; return { id : { token : AuthenticationResult . IdToken ! , expires : expires ( 60 * 60 ) } , access : { token : AuthenticationResult . AccessToken ! , expires : expires ( 60 * 60 ) } , refresh : { token : AuthenticationResult . RefreshToken ! , expires : expires ( 60 * 60 * 24 * 30 ) } } ; } catch ( err ) { throw translate ( err ) ; } } param token return public async authenticateWithToken ( token ) { try { const { AuthenticationResult , ChallengeName } = await this . cognito . initiateAuth ( { ClientId : process . env . COGNITO_USER_POOL_CLIENT_ID ! , AuthFlow : "REFRESH_TOKEN" , AuthParameters : { REFRESH_TOKEN : token } } ) . promise ( ) ; if ( ! AuthenticationResult ) throw new Error ( `Invalid authentication: challenge "${ ChallengeName }"` ) ; return { id : { token : AuthenticationResult . IdToken ! , expires : expires ( 60 * 60 ) } , access : { token : AuthenticationResult . AccessToken ! , expires : expires ( 60 * 60 ) } } ; } catch ( err ) { throw translate ( err ) ; } } param username return public async forgotPassword ( username ) { const { Username : id } = await this . cognito . adminGetUser ( { UserPoolId : process . env . COGNITO_USER_POOL_ID ! , Username : username } ) . promise ( ) ; return this . verification . issue ( "reset" , id ) ; } }