Skip to content

Commit

Permalink
Preserve old refresh token if new token does not contain refresh token (
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jun 30, 2024
1 parent 8ca734c commit 176ef7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module.exports = class AccessToken {
const parameters = GrantTypeParams.forGrantType(REFRESH_TOKEN_PROPERTY_NAME, this.#config.options, refreshParams);
const response = await this.#client.request(this.#config.auth.refreshPath, parameters.toObject(), httpOptions);

if (response[REFRESH_TOKEN_PROPERTY_NAME] === undefined) {
response.refresh_token = this.refresh_token;
}
return new AccessToken(this.#config, this.#client, response);
}

Expand Down
9 changes: 9 additions & 0 deletions test/_authorization-server-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ function createAuthorizationServer(authorizationServerUrl) {
});
}

function tokenSuccessWithoutRefreshToken(scopeOptions, params) {
return nock(authorizationServerUrl, scopeOptions)
.post('/oauth/token', params)
.reply(200, { ...accessToken, refresh_token: undefined }, {
'Content-Type': 'application/json',
});
}

return {
tokenError,
tokenAuthorizationError,
Expand All @@ -98,6 +106,7 @@ function createAuthorizationServer(authorizationServerUrl) {
tokenSuccessWithNonJSONContent,
tokenSuccessWithCustomPath,
tokenSuccess,
tokenSuccessWithoutRefreshToken,
};
}

Expand Down
24 changes: 24 additions & 0 deletions test/access-token-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,27 @@ test.serial('@refresh => creates a new access token with custom (inline) http op
scope.done();
t.true(has(refreshAccessToken.token, 'access_token'));
});

test.serial('@refresh => creates a new access token with keeping the old refresh token if refresh did not provide a new refresh token', async (t) => {
const config = createModuleConfig();

const accessTokenResponse = chance.accessToken({
expireMode: 'expires_in',
});

const client = new Client(config);

const refreshParams = {
grant_type: 'refresh_token',
refresh_token: accessTokenResponse.refresh_token,
};

const server = createAuthorizationServer('https://authorization-server.org:443');
const scope = server.tokenSuccessWithoutRefreshToken(scopeOptions, refreshParams);

const accessToken = new AccessToken(config, client, accessTokenResponse);
const refreshAccessToken = await accessToken.refresh();

scope.done();
t.true(has(refreshAccessToken.token, 'refresh_token'));
});

0 comments on commit 176ef7b

Please sign in to comment.