Skip to content

Commit

Permalink
feat(token-manager): Introduce accessToken
Browse files Browse the repository at this point in the history
Store token in accessToken and move varying token property names into subclass JWT.
Update tests to use accessToken field.
  • Loading branch information
Veronika committed May 4, 2020
1 parent fc4e9b6 commit 064b92f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
9 changes: 6 additions & 3 deletions auth/token-managers/jwt-token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type JwtTokenManagerOptions = TokenManagerOptions;
* to retrieve the bearer token from intended sources.
*/
export class JwtTokenManager extends TokenManager {
protected tokenName: string;
protected tokenInfo: any;

/**
* Create a new [[JwtTokenManager]] instance.
Expand All @@ -47,6 +49,7 @@ export class JwtTokenManager extends TokenManager {
super(options);

this.tokenName = 'access_token';
this.tokenInfo = {};
}

/**
Expand All @@ -70,17 +73,17 @@ export class JwtTokenManager extends TokenManager {
*/
protected saveTokenInfo(tokenResponse): void {
const responseBody = tokenResponse.result || {};
const accessToken = responseBody[this.tokenName];
this.accessToken = responseBody[this.tokenName];

if (!accessToken) {
if (!this.accessToken) {
const err = 'Access token not present in response';
logger.error(err);
throw new Error(err);
}

// the time of expiration is found by decoding the JWT access token
// exp is the time of expire and iat is the time of token retrieval
const decodedResponse = jwt.decode(accessToken);
const decodedResponse = jwt.decode(this.accessToken);
if (!decodedResponse) {
const err = 'Access token recieved is not a valid JWT';
logger.error(err);
Expand Down
15 changes: 6 additions & 9 deletions auth/token-managers/token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ export type TokenManagerOptions = {
*/
export class TokenManager {
protected url: string;
protected tokenName: string;
protected disableSslVerification: boolean;
protected headers: OutgoingHttpHeaders;
protected requestWrapperInstance: RequestWrapper;
protected tokenInfo: any;
protected accessToken: string;
protected expireTime: number;
protected refreshTime: number;
private requestTime: number;
Expand All @@ -68,8 +67,6 @@ export class TokenManager {
// all parameters are optional
options = options || {} as TokenManagerOptions;

this.tokenInfo = {};

if (options.url) {
// remove '/' from the end of the url
if (options.url.slice(-1) === '/') {
Expand All @@ -96,10 +93,10 @@ export class TokenManager {
* has expired.
*/
public getToken(): Promise<any> {
if (!this.tokenInfo[this.tokenName] || this.isTokenExpired()) {
if (!this.accessToken || this.isTokenExpired()) {
// 1. request a new token
return this.pacedRequestToken().then(() => {
return this.tokenInfo[this.tokenName];
return this.accessToken;
});
} else {
// If refresh needed, kick one off
Expand All @@ -109,7 +106,7 @@ export class TokenManager {
});
}
// 2. use valid, managed token
return Promise.resolve(this.tokenInfo[this.tokenName]);
return Promise.resolve(this.accessToken);
}
}

Expand Down Expand Up @@ -190,9 +187,9 @@ export class TokenManager {

/**
* Parse and save token information from the response.
* Save all token information into field `tokenInfo`.
* Save the requested token into field `accessToken`.
* Calculate expiration and refresh time from the received info
* and store them in object's fields`expireTime` and `refreshTime`.
* and store them in fields `expireTime` and `refreshTime`.
*
* @param tokenResponse - Response object from a token service request
* @protected
Expand Down
2 changes: 2 additions & 0 deletions test/unit/iam-token-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('iam_token_manager_v1', function() {
};

instance.tokenInfo = currentTokenInfo;
instance.accessToken = CURRENT_ACCESS_TOKEN;
instance.expireTime = Math.floor(Date.now() / 1000) + 60;
instance.refreshTime = Math.floor(Date.now() / 1000) - 1;

Expand Down Expand Up @@ -113,6 +114,7 @@ describe('iam_token_manager_v1', function() {
};

instance.tokenInfo = currentTokenInfo;
instance.accessToken = ACCESS_TOKEN;
instance.expireTime = Math.floor(Date.now() / 1000) + 60 * 60;
instance.refreshTime = Math.floor(Date.now() / 1000) + 48 * 60;

Expand Down
1 change: 1 addition & 0 deletions test/unit/jwt-token-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe('JWT Token Manager', () => {
it('should use an sdk-managed token if present and not expired', async done => {
const instance = new JwtTokenManager();
instance.tokenInfo.access_token = ACCESS_TOKEN;
instance.accessToken = ACCESS_TOKEN;
instance.expireTime = getCurrentTime() + 1000;
instance.refreshTime = getCurrentTime() + 800;
const token = await instance.getToken();
Expand Down

0 comments on commit 064b92f

Please sign in to comment.