import * as request from 'web-request' ; import { injectable } from 'inversify' ; import config from '../../config' ; import { Logger } from '../../logger' ; export interface AuthClientInterface { tenantToken : string ; registerTenant ( email , password ) : Promise < TenantRegistrationResult > ; loginTenant ( email , password ) : Promise < AccessTokenResponse > ; verifyTenantToken ( token ) : Promise < TenantVerificationResult > ; logoutTenant ( token ) : Promise < void > ; createUser ( data ) : Promise < UserRegistrationResult > ; loginUser ( data ) : Promise < AccessTokenResponse > ; verifyUserToken ( token ) : Promise < UserVerificationResult > ; logoutUser ( token ) : Promise < void > ; deleteUser ( login ) : Promise < void > ; } @ injectable ( ) export class AuthClient implements AuthClientInterface { private logger : Logger = Logger . getInstance ( 'AUTH_CLIENT_SERVICE' ) ; tenantToken : string ; baseUrl : string ; constructor ( baseUrl = config . auth . baseUrl ) { this . tenantToken = config . auth . token ; this . baseUrl = baseUrl ; request . defaults ( { throwResponseError : true } ) ; } async registerTenant ( email , password ) { return request . json < TenantRegistrationResult > ( '/tenant' , { baseUrl : this . baseUrl , method : 'POST' , body : { email , password } } ) ; } async loginTenant ( email , password ) { return request . json < AccessTokenResponse > ( '/tenant/login' , { baseUrl : this . baseUrl , method : 'POST' , body : { email , password } } ) ; } async verifyTenantToken ( token ) { return ( await request . json < TenantVerificationResponse > ( '/tenant/verify' , { baseUrl : this . baseUrl , method : 'POST' , body : { token } } ) ) . decoded ; } async logoutTenant ( token ) { await request . json < TenantVerificationResult > ( '/tenant/logout' , { baseUrl : this . baseUrl , method : 'POST' , body : { token } } ) ; } async createUser ( data ) { return request . json < UserRegistrationResult > ( '/user' , { baseUrl : this . baseUrl , method : 'POST' , body : data , headers : { 'authorization' : `Bearer ${ this . tenantToken }` , 'accept' : 'application/json' , 'content-type' : 'application/json' } } ) ; } async deleteUser ( login ) { return request . json < void > ( `/user/${ login }` , { baseUrl : this . baseUrl , method : 'DELETE' , headers : { 'authorization' : `Bearer ${ this . tenantToken }` } } ) ; } async loginUser ( data ) { return request . json < AccessTokenResponse > ( '/auth' , { baseUrl : this . baseUrl , method : 'POST' , headers : { 'authorization' : `Bearer ${ this . tenantToken }` } , body : data } ) ; } async verifyUserToken ( token ) { return ( await request . json < UserVerificationResponse > ( '/auth/verify' , { baseUrl : this . baseUrl , method : 'POST' , headers : { 'authorization' : `Bearer ${ this . tenantToken }` } , body : { token } } ) ) . decoded ; } async logoutUser ( token ) { await request . json < string > ( '/auth/logout' , { baseUrl : this . baseUrl , method : 'POST' , headers : { 'authorization' : `Bearer ${ this . tenantToken }` } , body : { token } } ) ; } } const AuthClientType = Symbol ( 'AuthClientInterface' ) ; export { AuthClientType } ;