/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { Injectable } from '@angular/core'; import { environment } from 'environments/environment'; import { getApplicationLocation, isInArray, isNullOrUndefined } from 'common/utils'; import { HTTPMimeType, HTTPRequestModel, HttpWrapperService, ModelResponseHttp } from 'common/service'; @Injectable() export class SSOService { signUpEnable: boolean = undefined; constructor( private http: HttpWrapperService, ) { console.log('Start SSOService'); } utf8_to_b64( str:string ): string { // remove unneeded "=" padding return window.btoa(encodeURIComponent( str )).replace("=", ""); } b64_to_utf8( str:string ): string { return decodeURIComponent(window.atob( str )); } /** * Request SSO connection */ hashLocalData(data?: string): string { if (!isNullOrUndefined(data) && !isInArray(data, ["", "null", "NULL", "undefined", "---", "unregistered", "unregistered/", "forbidden", "forbidden/"])) { return this.utf8_to_b64(data); } let pathName = getApplicationLocation(); if (isInArray(pathName, ["sso", "/sso", "/sso/"])) { return this.utf8_to_b64('home'); } if (!isNullOrUndefined(pathName) && !isInArray(pathName, ["", "null", "NULL", "undefined", "---", "unregistered", "unregistered/", "forbidden", "forbidden/"])) { return this.utf8_to_b64(pathName); } return this.utf8_to_b64('home'); } /** * Request SSO connection */ unHashLocalData(data: string): string | undefined { if (isNullOrUndefined(data) || isInArray(data, ["", "null", "NULL", "undefined", "---"])) { return undefined; } try { return this.b64_to_utf8(data); } catch (ex) { console.error(`Can not convert the data: ${data}`); } return undefined; } /** * Request SSO connection */ requestSignIn(name?: string): void { window.location.href = environment.ssoSignIn + this.hashLocalData(name); } /** * Request SSO Disconnect */ requestSignOut(name?: string): void { window.location.href = environment.ssoSignOut + this.hashLocalData(name); } /** * Request SSO signUp */ requestSignUp(name?: string): void { window.location.href = environment.ssoSignUp + this.hashLocalData(name); } /** * Checo if the signUp is authorized (generate by te SSO) * @returns a promise of the State for sign-up */ checkSignUpEnable(): Promise { let self = this; return new Promise((resolve, reject) => { if (isNullOrUndefined(self.signUpEnable)) { this.http.requestJson({ server: 'karauth', endPoint: 'system_config/is_sign_up_availlable', requestType: HTTPRequestModel.GET, accept: HTTPMimeType.JSON, contentType: HTTPMimeType.JSON, }).then((response: ModelResponseHttp) =>{ self.signUpEnable = response.data.signup; resolve(self.signUpEnable); }).catch((error:any) => { reject(`return ERROR ${ JSON.stringify(error, null, 2)}`); }); return; } resolve(self.signUpEnable); }); } }