"use strict" ; import { APIGatewayProxyEvent } from "aws-lambda" ; import AWS , { CognitoIdentityServiceProvider } from "aws-sdk" ; import { LoginResult } from "../lambda/definitions/types" ; export interface Config { userPoolId : string ; userPoolClientId : string ; } AWS . config . update ( { region : "ap-northeast-1" , } ) ; export class CognitoAdmin { cognito : CognitoIdentityServiceProvider ; user : CognitoIdentityServiceProvider . AdminInitiateAuthResponse | undefined ; config : Config ; constructor ( config ) { this . cognito = new AWS . CognitoIdentityServiceProvider ( { apiVersion : "2016-04-18" , } ) ; this . config = config ; this . user = undefined ; } param username async signUp ( username , password ? ) { const userPoolId = this . config . userPoolId ; const user = await this . cognito . adminCreateUser ( { UserPoolId : userPoolId , Username : username , } ) . promise ( ) ; console . log ( "登録完了" , JSON . stringify ( user , null , 4 ) ) ; if ( password == undefined ) { password = this . makePassword ( ) ; } await this . cognito . adminSetUserPassword ( { UserPoolId : userPoolId , Username : username , Password : password , Permanent : true , } ) . promise ( ) ; console . log ( "パスワード変更完了" ) ; return { username : username , password : password , user : user } ; } param username param password async signIn ( username , password ) { const userPoolId = this . config . userPoolId ; const clientId = this . config . userPoolClientId ; this . user = await this . cognito . adminInitiateAuth ( { UserPoolId : userPoolId , ClientId : clientId , AuthFlow : "ADMIN_NO_SRP_AUTH" , AuthParameters : { USERNAME : username , PASSWORD : password , } , } ) . promise ( ) ; console . log ( "サインイン完了" , JSON . stringify ( this . user , null , 4 ) ) ; return this . user ; } param refreshToken async refreshToken ( refreshToken ) { const userPoolId = this . config . userPoolId ; const clientId = this . config . userPoolClientId ; this . user = await this . cognito . adminInitiateAuth ( { UserPoolId : userPoolId , ClientId : clientId , AuthFlow : "REFRESH_TOKEN_AUTH" , AuthParameters : { REFRESH_TOKEN : refreshToken , } , } ) . promise ( ) ; console . log ( "サインイン完了" , JSON . stringify ( this . user , null , 4 ) ) ; return this . user ; } makePassword ( ) { let pwd = Math . random ( ) . toString ( 36 ) . substr ( 2 , 8 ) ; let count = 0 ; while ( ( pwd . match ( /[a-zA-Z]/ ) && pwd . match ( /\d/ ) ) == null ) { if ( count > 100 ) break ; pwd = Math . random ( ) . toString ( 36 ) . substr ( 2 , 8 ) ; count = count + 1 ; } return pwd ; } param username async initialize ( patientId ) { if ( ! patientId ) { throw new Error ( "user id is not set" ) ; } const userPoolId = this . config . userPoolId ; const password = this . makePassword ( ) ; await this . cognito . adminSetUserPassword ( { UserPoolId : userPoolId , Username : patientId , Password : password , Permanent : true , } ) . promise ( ) ; console . log ( "パスワード変更完了" ) ; const user = await this . signIn ( patientId , password ) ; if ( ! user . AuthenticationResult ?. IdToken || ! user . AuthenticationResult ?. RefreshToken ) { throw new Error ( "login failed" ) ; } return { username : patientId , idToken : user . AuthenticationResult ?. IdToken , refreshToken : user . AuthenticationResult . RefreshToken , } ; } param authHeader getUserId ( event ) { if ( ! event . headers [ "Authorization" ] ) return null ; const authHeader = event . headers [ "Authorization" ] ; return this . getUserIdFromHeader ( authHeader ) ; } param authHeader getUserIdFromHeader ( header ) { const authHeader = header ; const payload = Buffer . from ( authHeader . split ( "." ) [ 1 ] , "base64" ) . toString ( "ascii" ) ; console . log ( payload ) ; return JSON . parse ( payload ) [ "cognito:username" ] ; } }