import { TokenCredentialsBase } from "./tokenCredentialsBase" ; import { Environment } from "@azure/ms-rest-azure-env" ; import { AuthConstants , TokenAudience } from "../util/authConstants" ; import { TokenCache , TokenResponse } from "adal-node" ; export abstract class ApplicationTokenCredentialsBase extends TokenCredentialsBase { /**
     * Creates a new ApplicationTokenCredentials object.
     * See  https * for detailed instructions on creating an Azure Active Directory application.
     *
     *  param clientId param domain param tokenAudience param environment param tokenCache public constructor ( clientId , domain , tokenAudience ? , environment ? , tokenCache ? ) { super ( clientId , domain , tokenAudience , environment , tokenCache ) ; } protected async getTokenFromCache ( ) { try { return await super . getTokenFromCache ( undefined ) ; } catch ( error ) { const status = await this . removeInvalidItemsFromCache ( { _clientId : this . clientId } ) ; if ( status . result ) { throw error ; } const message = status && status . details && status . details . message ? status . details . message : status . details ; throw new Error ( AuthConstants . SDK_INTERNAL_ERROR + " : " + "critical failure while removing expired token for service principal from token cache. " + message ) ; } } param query returns private removeInvalidItemsFromCache ( query ) { const self = this ; return new Promise < { result : boolean ; details ? : Error ; } > ( ( resolve ) => { self . tokenCache . find ( query , ( error , entries ) => { if ( error ) { return resolve ( { result : false , details : error } ) ; } if ( entries && entries . length > 0 ) { return self . tokenCache . remove ( entries , ( err ) => { if ( err ) { return resolve ( { result : false , details : err } ) ; } return resolve ( { result : true } ) ; } ) ; } else { return resolve ( { result : true } ) ; } } ) ; } ) ; } }