Skip to content

Commit c476e4c

Browse files
authored
Merge pull request #389 from runeb/fix/validate-scope-before-revoke
fix(refresh-token): validate scope before revoking token #390
2 parents d4dc7c7 + 38e9520 commit c476e4c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/grant-types/refresh-token-grant-type.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ class RefreshTokenGrantType extends AbstractGrantType {
5454

5555
let token;
5656
token = await this.getRefreshToken(request, client);
57-
token = await this.revokeToken(token);
5857

58+
// Validate scope before revoking token to prevent destroying tokens on scope validation errors
5959
const scope = this.getScope(request, token);
6060

61+
token = await this.revokeToken(token);
62+
6163
return this.saveToken(token.user, client, scope);
6264
}
6365

test/integration/grant-types/refresh-token-grant-type_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const InvalidArgumentError = require('../../../lib/errors/invalid-argument-error');
88
const InvalidGrantError = require('../../../lib/errors/invalid-grant-error');
99
const InvalidRequestError = require('../../../lib/errors/invalid-request-error');
10+
const InvalidScopeError = require('../../../lib/errors/invalid-scope-error');
1011
const RefreshTokenGrantType = require('../../../lib/grant-types/refresh-token-grant-type');
1112
const Request = require('../../../lib/request');
1213
const ServerError = require('../../../lib/errors/server-error');
@@ -182,6 +183,34 @@ describe('RefreshTokenGrantType integration', function() {
182183

183184
grantType.handle(request, client).should.be.an.instanceOf(Promise);
184185
});
186+
187+
it('should throw an error if extra `scope` is requested', async function() {
188+
const client = { id: 123 };
189+
const token = {
190+
accessToken: 'foo',
191+
client: { id: 123 },
192+
user: { name: 'foo' },
193+
refreshTokenExpiresAt: new Date(new Date() * 2)
194+
};
195+
const model = {
196+
getRefreshToken: async function() {
197+
return token;
198+
},
199+
revokeToken: () => should.fail(),
200+
saveToken: () => should.fail()
201+
};
202+
const grantType = new RefreshTokenGrantType({ accessTokenLifetime: 123, model });
203+
const request = new Request({ body: { refresh_token: 'foobar', scope: 'read' }, headers: {}, method: {}, query: {} });
204+
205+
try {
206+
await grantType.handle(request, client);
207+
208+
should.fail();
209+
} catch (e) {
210+
e.should.be.an.instanceOf(InvalidScopeError);
211+
e.message.should.equal('Invalid scope: Unable to add extra scopes');
212+
}
213+
});
185214
});
186215

187216
describe('getRefreshToken()', function() {

0 commit comments

Comments
 (0)