Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a different path for refresh tokens #387

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Simple OAuth2 grant classes accept an object with the following params.
* `auth` - required object with the following properties:
* `tokenHost` - Base URL used to obtain access tokens. Required
* `tokenPath` - URL path to obtain access tokens (See [url resolution notes](#url-resolution)). Defaults to **/oauth/token**
* `refreshPath` - URL path to refresh access tokens (See [url resolution notes](#url-resolution)). Defaults to `auth.tokenPath`
* `revokePath` - URL path to revoke access tokens (See [url resolution notes](#url-resolution)). Defaults to **/oauth/revoke**
* `authorizeHost` - Base URL used to request an *authorization code*. Defaults to `auth.tokenHost` value
* `authorizePath` - URL path to request an *authorization code* (See [url resolution notes](#url-resolution)). Defaults to **/oauth/authorize**
Expand Down
2 changes: 1 addition & 1 deletion lib/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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.tokenPath, parameters.toObject(), httpOptions);
const response = await this.#client.request(this.#config.auth.refreshPath, parameters.toObject(), httpOptions);

return new AccessToken(this.#config, this.#client, response);
}
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const clientSchema = Joi.object().keys({
const authSchema = Joi.object().keys({
tokenHost: Joi.string().required().uri({ scheme: ['http', 'https'] }),
tokenPath: Joi.string().default('/oauth/token'),
refreshPath: Joi.string().default(Joi.ref('tokenPath')),
revokePath: Joi.string().default('/oauth/revoke'),
authorizeHost: Joi.string().uri({ scheme: ['http', 'https'] }).default(Joi.ref('tokenHost')),
authorizePath: Joi.string().default('/oauth/authorize'),
Expand Down
28 changes: 28 additions & 0 deletions test/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ test.serial('@refresh => creates a new access token with a custom token path', a
t.true(has(refreshAccessToken.token, 'access_token'));
});

test.serial('@refresh => creates a new access token with a custom refresh path', async (t) => {
const config = createModuleConfig({
auth: {
refreshPath: '/the-custom/refresh-path',
},
});

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.tokenSuccessWithCustomPath('/the-custom/refresh-path', scopeOptions, refreshParams);

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

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

test.serial('@refresh => creates a new access token with custom (inline) http options', async (t) => {
const config = createModuleConfig();

Expand Down