From b22ac72e5f6959a032be55e494b9ece9798e5bf7 Mon Sep 17 00:00:00 2001 From: Milan Holemans <11723921+milanholemans@users.noreply.github.com> Date: Fri, 8 Mar 2024 22:02:47 +0100 Subject: [PATCH] Fixes retrieving new access token --- src/Auth.spec.ts | 4 ++-- src/Auth.ts | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Auth.spec.ts b/src/Auth.spec.ts index 0704b734964..4acddbd85d1 100644 --- a/src/Auth.spec.ts +++ b/src/Auth.spec.ts @@ -304,7 +304,7 @@ describe('Auth', () => { done('Got access token'); }, () => { try { - assert(loggerLogToStderrSpy.calledWith('getTokenPromise authentication result is null')); + assert(loggerLogToStderrSpy.calledWith('getTokenPromise authentication result is null.')); done(); } catch (e) { @@ -321,7 +321,7 @@ describe('Auth', () => { done('Got access token'); }, () => { try { - assert(loggerLogToStderrSpy.calledWith('getTokenPromise authentication result is null')); + assert(loggerLogToStderrSpy.calledWith('getTokenPromise authentication result is null.')); done(); } catch (e) { diff --git a/src/Auth.ts b/src/Auth.ts index 0a5562f4844..5a178a870ea 100644 --- a/src/Auth.ts +++ b/src/Auth.ts @@ -216,10 +216,10 @@ export class Auth { else { if (debug) { if (!accessToken) { - await logger.logToStderr(`No token found for resource ${resource}`); + await logger.logToStderr(`No token found for resource ${resource}.`); } else { - await logger.logToStderr(`Access token expired. Token: ${accessToken.accessToken}, ExpiresAt: ${accessToken.expiresOn}`); + await logger.logToStderr(`Access token expired. Token: ${accessToken.accessToken}, ExpiresAt: ${accessToken.expiresOn}.`); } } } @@ -269,9 +269,9 @@ export class Auth { const response = await getTokenPromise(resource, logger, debug, fetchNew); if (!response) { if (debug) { - await logger.logToStderr(`getTokenPromise authentication result is null`); + await logger.logToStderr('getTokenPromise authentication result is null.'); } - throw `Failed to retrieve an access token. Please try again`; + throw 'Failed to retrieve an access token. Please try again.'; } else { if (debug) { @@ -496,7 +496,7 @@ export class Auth { }); } - private async ensureAccessTokenWithCertificate(resource: string, logger: Logger, debug: boolean): Promise { + private async ensureAccessTokenWithCertificate(resource: string, logger: Logger, debug: boolean, fetchNew: boolean): Promise { const nodeForge = (await import('node-forge')).default; const { pem, pki, asn1, pkcs12 } = nodeForge; @@ -567,7 +567,8 @@ export class Auth { this.clientApplication = await this.getConfidentialClient(logger, debug, this.connection.thumbprint as string, cert); return (this.clientApplication as Msal.ConfidentialClientApplication).acquireTokenByClientCredential({ - scopes: [`${resource}/.default`] + scopes: [`${resource}/.default`], + skipCache: fetchNew }); } @@ -706,10 +707,11 @@ export class Auth { } } - private async ensureAccessTokenWithSecret(resource: string, logger: Logger, debug: boolean): Promise { + private async ensureAccessTokenWithSecret(resource: string, logger: Logger, debug: boolean, fetchNew: boolean): Promise { this.clientApplication = await this.getConfidentialClient(logger, debug, undefined, undefined, this.connection.secret); return (this.clientApplication as Msal.ConfidentialClientApplication).acquireTokenByClientCredential({ - scopes: [`${resource}/.default`] + scopes: [`${resource}/.default`], + skipCache: fetchNew }); } @@ -726,7 +728,7 @@ export class Auth { let resource: string = url; const pos: number = resource.indexOf('/', 8); if (pos > -1) { - resource = resource.substr(0, pos); + resource = resource.substring(0, pos); } if (resource === 'https://api.bap.microsoft.com' ||