Skip to content

Commit

Permalink
feat(iam-authenticator): expose refresh token with a getter - `getRef…
Browse files Browse the repository at this point in the history
…reshToken()` (#122)

The `IamAuthenticator` class now makes the refresh token available to users through
a public method, `getRefreshToken`. This provides support to services that require
the refresh token as a part of their authentication scheme.
  • Loading branch information
dpopp07 authored Feb 19, 2021
1 parent 170918a commit d3c4611
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
10 changes: 10 additions & 0 deletions auth/authenticators/iam-authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,14 @@ export class IamAuthenticator extends TokenRequestBasedAuthenticator {
// update properties in token manager
this.tokenManager.setScope(scope);
}

/**
* Return the most recently stored refresh token.
*
* @public
* @returns {string}
*/
public getRefreshToken(): string {
return this.tokenManager.getRefreshToken();
}
}
28 changes: 28 additions & 0 deletions auth/token-managers/iam-token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface Options extends JwtTokenManagerOptions {
*/
export class IamTokenManager extends JwtTokenManager {
protected requiredOptions = ['apikey'];
protected refreshToken: string;
private apikey: string;
private clientId: string;
private clientSecret: string;
Expand Down Expand Up @@ -131,6 +132,33 @@ export class IamTokenManager extends JwtTokenManager {
}
}

/**
* Return the most recently stored refresh token.
*
* @public
* @returns {string}
*/
public getRefreshToken(): string {
return this.refreshToken;
}

/**
* Extend this method from the parent class to extract the refresh token from
* the request and save it.
*
* @param tokenResponse - Response object from JWT service request
* @protected
* @returns {void}
*/
protected saveTokenInfo(tokenResponse): void {
super.saveTokenInfo(tokenResponse);

const responseBody = tokenResponse.result || {};
if (responseBody.refresh_token) {
this.refreshToken = responseBody.refresh_token;
}
}

/**
* Request an IAM token using an API key.
*
Expand Down
16 changes: 15 additions & 1 deletion test/unit/iam-token-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ RequestWrapper.mockImplementation(() => {

const ACCESS_TOKEN = '9012';
const CURRENT_ACCESS_TOKEN = '1234';
const REFRESH_TOKEN = '3456';
const IAM_RESPONSE = {
result: {
access_token: ACCESS_TOKEN,
refresh_token: '3456',
refresh_token: REFRESH_TOKEN,
token_type: 'Bearer',
expires_in: 3600,
expiration: Math.floor(Date.now() / 1000) + 3600,
Expand Down Expand Up @@ -354,4 +355,17 @@ describe('iam_token_manager_v1', function() {
expect(scope).toBeUndefined();
done();
});

it('should save and expose the refresh token', async () => {
const instance = new IamTokenManager({
apikey: 'abcd-1234',
});

mockSendRequest.mockImplementation(parameters => Promise.resolve(IAM_RESPONSE));

await instance.getToken();

expect(instance.refreshToken).toBe(REFRESH_TOKEN);
expect(instance.getRefreshToken()).toBe(REFRESH_TOKEN);
});
});

0 comments on commit d3c4611

Please sign in to comment.