import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { SSOTokenResponse, TokenPayload, TokenResponse } from '../../typings/token.typing'; import { TokenAdapterService } from './token-adapter.service'; @Injectable({ providedIn: 'root' }) export class TokenRetrievalResources { constructor ( private portal: PortalDeterminationService, private httpRestService: HttpClient, private adapter: TokenAdapterService ) { } private getTokenForManager (data: TokenPayload) { return this.httpRestService.post('/api/token/manager', data).toPromise(); } private getTokenForPortal (data: TokenPayload) { return this.httpRestService.post('/api/token/portal', data).toPromise(); } private getTokenForPlatformAdmin (data: TokenPayload) { return this.httpRestService.post('/api/token/admin', data).toPromise(); } async getToken (data: TokenPayload) { let returnVal: TokenResponse; if (this.portal.isPlatform) { returnVal = await this.getTokenForPlatformAdmin(data); } else if (this.portal.isManager) { returnVal = await this.getTokenForManager(data); } else { returnVal = await this.getTokenForPortal(data); } return this.adapter.handleTokenRequest(returnVal); } getTokenFromSSO (token: string, accessToken: string, clientIdentifier: string): Promise { if (this.portal.isApply) { return this.getTokenFromSSOApplicant(token, accessToken, clientIdentifier); } else if (this.portal.isManager) { return this.getTokenFromSSOManager(token, accessToken, clientIdentifier); } return null; } private async getTokenFromSSOManager (token: string, accessToken: string, clientIdentifier: string): Promise { const response = await this.httpRestService .post('/api/token/ManagerSso', { token, accessToken, clientIdentifier }).toPromise(); return this.adapter.handleTokenRequest(response); } private async getTokenFromSSOApplicant (token: string, accessToken: string, clientIdentifier: string): Promise { const response = await this.httpRestService .post('/api/token/PortalSso', { token, accessToken, clientIdentifier }).toPromise(); return this.adapter.handleTokenRequest(response); } getPlatformAdminToken ( code: string, clientIdentifier: string ): Promise { const endpoint = 'api/admin/Sso/Token'; return this.httpRestService.post(endpoint, { code, clientIdentifier }).toPromise(); } }