import { readFileSync } from "fs" ; import { createHash } from "crypto" ; import { ApplicationTokenCredentialsBase } from "./applicationTokenCredentialsBase" ; import { Environment } from "@azure/ms-rest-azure-env" ; import { AuthConstants , TokenAudience } from "../util/authConstants" ; import { TokenResponse , ErrorResponse , TokenCache } from "adal-node" ; import { AzureTokenCredentialsOptions } from "../login" ; export class ApplicationTokenCertificateCredentials extends ApplicationTokenCredentialsBase { readonly certificate : string ; readonly thumbprint : string ; /**
     * Creates a new ApplicationTokenCredentials object.
     * See  https * for detailed instructions on creating an Azure Active Directory application.
     *
     *  param clientId param domain param certificate param thumbprint param tokenAudience param environment param tokenCache public constructor ( clientId , domain , certificate , thumbprint , tokenAudience ? , environment ? , tokenCache ? ) { if ( ! certificate || typeof certificate . valueOf ( ) !== "string" ) { throw new Error ( "certificate must be a non empty string." ) ; } if ( ! thumbprint || typeof thumbprint . valueOf ( ) !== "string" ) { throw new Error ( "thumbprint must be a non empty string." ) ; } super ( clientId , domain , tokenAudience , environment , tokenCache ) ; this . certificate = certificate ; this . thumbprint = thumbprint ; } returns public async getToken ( ) { try { return await this . getTokenFromCache ( ) ; } catch ( error ) { if ( error . message . startsWith ( AuthConstants . SDK_INTERNAL_ERROR ) ) { throw error ; } return new Promise ( ( resolve , reject ) => { const resource = this . getActiveDirectoryResourceId ( ) ; this . authContext . acquireTokenWithClientCertificate ( resource , this . clientId , this . certificate , this . thumbprint , ( error , tokenResponse ) => { if ( error ) { return reject ( error ) ; } if ( tokenResponse . error || tokenResponse . errorDescription ) { return reject ( tokenResponse ) ; } return resolve ( tokenResponse as TokenResponse ) ; } ) ; } ) ; } } param clientId The active directory application client id also known as the SPN (ServicePrincipal Name).
     * See  https * for an example.
     *  param certificateStringOrFilePath param domain param options returns public static create ( clientId , certificateStringOrFilePath , domain , options ) { if ( ! certificateStringOrFilePath || typeof certificateStringOrFilePath . valueOf ( ) !== "string" ) { throw new Error ( "'certificateStringOrFilePath' must be a non empty string." ) ; } if ( ! certificateStringOrFilePath . startsWith ( "-----BEGIN" ) ) { certificateStringOrFilePath = readFileSync ( certificateStringOrFilePath , "utf8" ) ; } const certificatePattern = /(-+BEGIN CERTIFICATE-+)(\n\r?|\r\n?)([A-Za-z0-9\+\/\n\r]+\=*)(\n\r?|\r\n?)(-+END CERTIFICATE-+)/ ; const matchCert = certificateStringOrFilePath . match ( certificatePattern ) ; const rawCertificate = matchCert ? matchCert [ 3 ] : "" ; if ( ! rawCertificate ) { throw new Error ( "Unable to correctly parse the certificate from the value provided in 'certificateStringOrFilePath' " ) ; } const thumbprint = createHash ( "sha1" ) . update ( Buffer . from ( rawCertificate , "base64" ) ) . digest ( "hex" ) ; return new ApplicationTokenCertificateCredentials ( clientId , domain , certificateStringOrFilePath , thumbprint , options . tokenAudience , options . environment , options . tokenCache ) ; } }