diff --git a/lib/credentials/coreAuthHelpers.ts b/lib/credentials/coreAuthHelpers.ts index 5fec4da..1f31898 100644 --- a/lib/credentials/coreAuthHelpers.ts +++ b/lib/credentials/coreAuthHelpers.ts @@ -2,7 +2,7 @@ import { AccessToken } from "@azure/core-auth"; interface TokenResponseLike { accessToken: string; - expiresOn: Date | string; + expiresOn?: Date | string; } /** @@ -16,12 +16,19 @@ export function prepareToken( scopes: string | string[] | undefined): T | AccessToken { // Scopes will contain _some_ value if a parameter was passed to getToken if (scopes !== undefined) { - return { - token: token.accessToken, - expiresOnTimestamp: + // Start with a default 'expiresOn' and then replace with + // the actual 'expiresOn' if one is given + let expiresOnTimestamp: number = Date.now() + 60 * 60 * 1000; + if (token.expiresOn) { + expiresOnTimestamp = typeof token.expiresOn === "string" ? Date.parse(token.expiresOn) - : token.expiresOn.getTime() + : token.expiresOn.getTime(); + } + + return { + token: token.accessToken, + expiresOnTimestamp } as AccessToken; } else { return token; diff --git a/lib/credentials/tokenClientCredentials.ts b/lib/credentials/tokenClientCredentials.ts index 405730b..11b57b0 100644 --- a/lib/credentials/tokenClientCredentials.ts +++ b/lib/credentials/tokenClientCredentials.ts @@ -6,7 +6,6 @@ import { ServiceClientCredentials } from "@azure/ms-rest-js"; export interface TokenResponse { readonly tokenType: string; readonly accessToken: string; - readonly expiresOn: Date; readonly [x: string]: any; } diff --git a/test/credentials/coreAuthHelpersTests.ts b/test/credentials/coreAuthHelpersTests.ts index 3c168b9..85f30e0 100644 --- a/test/credentials/coreAuthHelpersTests.ts +++ b/test/credentials/coreAuthHelpersTests.ts @@ -3,6 +3,7 @@ import { assert } from "chai"; import { prepareToken } from "../../lib/credentials/coreAuthHelpers"; +import { AccessToken } from "@azure/core-auth"; describe("prepareToken", function() { it("returns an AccessToken when scope is set", function() { @@ -31,6 +32,14 @@ describe("prepareToken", function() { }); }); + it("returns an AccessToken with expiresOnTimestamp when expiresOn is empty", function() { + const result = prepareToken({ + accessToken: "token", + }, "scope") as AccessToken; + + assert.isNumber(result.expiresOnTimestamp); + }); + it("returns an unmodified TokenResponse when scope is not set", function() { const expiresOn = new Date(); const tokenResponse = {