Skip to content

Commit

Permalink
Fixes retrieving new access token
Browse files Browse the repository at this point in the history
  • Loading branch information
milanholemans committed Mar 8, 2024
1 parent c74fe4b commit b22ac72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
20 changes: 11 additions & 9 deletions src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`);
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -496,7 +496,7 @@ export class Auth {
});
}

private async ensureAccessTokenWithCertificate(resource: string, logger: Logger, debug: boolean): Promise<AccessToken | null> {
private async ensureAccessTokenWithCertificate(resource: string, logger: Logger, debug: boolean, fetchNew: boolean): Promise<AccessToken | null> {
const nodeForge = (await import('node-forge')).default;
const { pem, pki, asn1, pkcs12 } = nodeForge;

Expand Down Expand Up @@ -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
});
}

Expand Down Expand Up @@ -706,10 +707,11 @@ export class Auth {
}
}

private async ensureAccessTokenWithSecret(resource: string, logger: Logger, debug: boolean): Promise<AccessToken | null> {
private async ensureAccessTokenWithSecret(resource: string, logger: Logger, debug: boolean, fetchNew: boolean): Promise<AccessToken | null> {
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
});
}

Expand All @@ -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' ||
Expand Down

0 comments on commit b22ac72

Please sign in to comment.