import { EventEmitter , Injectable } from '@angular/core' ; import * as Msal from 'msal' ; import { Config } from './config/config' ; import * as jwt_decode from 'jwt-decode' ; @ Injectable ( { providedIn : 'root' , } ) export class AuthHelperService { private clientApplication : Msal . UserAgentApplication = null ; public connectionEstablished = new EventEmitter < AccessTokenInfo > ( ) ; AccessTokenKey = 'sample.access.token' ; constructor ( private config ) { this . clientApplication = new Msal . UserAgentApplication ( this . config . get ( ) . CLIENT_ID , this . config . get ( ) . AUTHORITY , this . authCallback , { redirectUri : window . location . origin } ) ; } private authCallback ( errorDesc , token , error , tokenType ) { if ( error ) { console . error ( `${ error } ${ errorDesc }` ) ; } else { console . log ( `Received ${ tokenType } token.` ) ; } } public login ( ) { this . clientApplication . loginRedirect ( this . config . get ( ) . SCOPES ) ; } public logout ( ) { this . saveAccessTokenToCache ( null ) ; this . clientApplication . logout ( ) ; } public isOnline ( ) { return this . clientApplication . getUser ( ) != null ; } public getAuthenticationToken ( ) { const self = this ; const user = this . clientApplication . getUser ( ) ; const authority = this . config . get ( ) . AUTHORITY ; return this . clientApplication . acquireTokenSilent ( this . config . get ( ) . SCOPES_WEB , authority , user ) . then ( token => { console . log ( `Received Access token.` ) ; this . saveAccessTokenToCache ( token ) ; self . connectionEstablished . emit ( { accessToken : token , isConnected : true } ) ; return token ; } ) . catch ( error => { console . log ( `Error: Access token API : ${ error }.` ) ; this . clientApplication . acquireTokenRedirect ( this . config . get ( ) . SCOPES_WEB ) ; return null ; } ) ; } saveAccessTokenToCache ( accessToken ) { sessionStorage . setItem ( this . AccessTokenKey , accessToken ) ; } getAccessTokenFromCache ( ) { if ( sessionStorage . hasOwnProperty ( this . AccessTokenKey ) && sessionStorage [ this . AccessTokenKey ] !== '' ) { return sessionStorage [ this . AccessTokenKey ] ; } return null ; } getTokenExpirationDate ( token ) { const decoded = jwt_decode ( token ) ; if ( decoded . exp === undefined ) { return null ; } const date = new Date ( 0 ) ; date . setUTCSeconds ( decoded . exp ) ; return date ; } isTokenExpired ( token ? ) { if ( ! token ) { token = this . getAccessTokenFromCache ( ) ; } if ( ! token ) { return true ; } const date = this . getTokenExpirationDate ( token ) ; if ( date === undefined ) { return false ; } return ! ( date . valueOf ( ) > new Date ( ) . valueOf ( ) ) ; } } export class AccessTokenInfo { isConnected : boolean ; accessToken : string ; } export class TTokenDto { exp : number ; }