Skip to content

Commit

Permalink
Make prepareToken more tolerant of omitted expiresOn
Browse files Browse the repository at this point in the history
  • Loading branch information
daviwil committed Jul 22, 2019
1 parent cbdb4a6 commit 565a9fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/credentials/coreAuthHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AccessToken } from "@azure/core-auth";

interface TokenResponseLike {
accessToken: string;
expiresOn: Date | string;
expiresOn?: Date | string;
}

/**
Expand All @@ -16,12 +16,19 @@ export function prepareToken<T extends TokenResponseLike>(
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;
Expand Down
1 change: 0 additions & 1 deletion lib/credentials/tokenClientCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions test/credentials/coreAuthHelpersTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 565a9fe

Please sign in to comment.