Skip to content

Commit 2677693

Browse files
committed
Rebase revoke-handler to oauthjs:dev
1 parent 592c809 commit 2677693

File tree

2 files changed

+163
-8
lines changed

2 files changed

+163
-8
lines changed

lib/handlers/revoke-handler.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var InvalidTokenError = require('../errors/invalid-token-error');
1010
var InvalidRequestError = require('../errors/invalid-request-error');
1111
var OAuthError = require('../errors/oauth-error');
1212
var Promise = require('bluebird');
13+
var promisify = require('promisify-any');
1314
var Request = require('../request');
1415
var Response = require('../response');
1516
var ServerError = require('../errors/server-error');
@@ -148,7 +149,7 @@ RevokeHandler.prototype.getClient = function(request, response) {
148149
throw new InvalidRequestError('Invalid parameter: `client_secret`');
149150
}
150151

151-
return Promise.try(this.model.getClient, [credentials.clientId, credentials.clientSecret])
152+
return Promise.try(promisify(this.model.getClient, 2), [credentials.clientId, credentials.clientSecret])
152153
.then(function(client) {
153154
if (!client) {
154155
throw new InvalidClientError('Invalid client: client is invalid');
@@ -223,7 +224,7 @@ RevokeHandler.prototype.getTokenFromRequest = function(request) {
223224
*/
224225

225226
RevokeHandler.prototype.getRefreshToken = function(token, client) {
226-
return Promise.try(this.model.getRefreshToken, token)
227+
return Promise.try(promisify(this.model.getRefreshToken, 1), token)
227228
.then(function(token) {
228229
if (!token) {
229230
throw new InvalidTokenError('Invalid token: refresh token is invalid');
@@ -258,7 +259,7 @@ RevokeHandler.prototype.getRefreshToken = function(token, client) {
258259
*/
259260

260261
RevokeHandler.prototype.getAccessToken = function(token, client) {
261-
return Promise.try(this.model.getAccessToken, token)
262+
return Promise.try(promisify(this.model.getAccessToken, 1), token)
262263
.then(function(accessToken) {
263264
if (!accessToken) {
264265
throw new InvalidTokenError('Invalid token: access token is invalid');
@@ -273,7 +274,7 @@ RevokeHandler.prototype.getAccessToken = function(token, client) {
273274
}
274275

275276
if (accessToken.client.id !== client.id) {
276-
throw new InvalidTokenError('Invalid token: access token is invalid');
277+
throw new InvalidTokenError('Invalid token: access token client is invalid');
277278
}
278279

279280
if (accessToken.accessTokenExpiresAt && !(accessToken.accessTokenExpiresAt instanceof Date)) {
@@ -295,7 +296,7 @@ RevokeHandler.prototype.getAccessToken = function(token, client) {
295296
*/
296297

297298
RevokeHandler.prototype.revokeToken = function(token) {
298-
return Promise.try(this.model.revokeToken, token)
299+
return Promise.try(promisify(this.model.revokeToken, 1), token)
299300
.then(function(token) {
300301
if (!token) {
301302
throw new InvalidTokenError('Invalid token: token is invalid');

test/integration/handlers/revoke-handler_test.js

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('RevokeHandler integration', function() {
234234
.then(should.fail)
235235
.catch(function() {
236236
response.body.should.eql({ error: 'server_error', error_description: 'Unhandled exception' });
237-
response.status.should.equal(503);
237+
response.status.should.equal(500);
238238
});
239239
});
240240

@@ -434,6 +434,21 @@ describe('RevokeHandler integration', function() {
434434
handler.getClient(request).should.be.an.instanceOf(Promise);
435435
});
436436

437+
it('should support callbacks', function() {
438+
var model = {
439+
getClient: function(clientId, clientSecret, callback) {
440+
callback(null, { grants: [] });
441+
},
442+
revokeToken: function() {},
443+
getRefreshToken: function() {},
444+
getAccessToken: function() {}
445+
};
446+
var handler = new RevokeHandler({ model: model });
447+
var request = new Request({ body: { client_id: 12345, client_secret: 'secret' }, headers: {}, method: {}, query: {} });
448+
449+
handler.getClient(request).should.be.an.instanceOf(Promise);
450+
});
451+
437452
it('should support non-promises', function() {
438453
var model = {
439454
getClient: function() { return { grants: [] }; },
@@ -607,11 +622,12 @@ describe('RevokeHandler integration', function() {
607622
});
608623

609624
it('should throw an error if the `client_id` does not match', function() {
610-
var client = { id: 12345 };
625+
var client = { id: 'foo' };
626+
var token = { refreshToken: 'hash', client: { id: 'baz'}, user: {}, refreshTokenExpiresAt: new Date(new Date() * 2) };
611627
var model = {
612628
getClient: function() {},
613629
revokeToken: function() {},
614-
getRefreshToken: function() { return { client: { id: 9999}, user: {} }; },
630+
getRefreshToken: function() { return token; },
615631
getAccessToken: function() {}
616632
};
617633
var handler = new RevokeHandler({ model: model });
@@ -623,6 +639,121 @@ describe('RevokeHandler integration', function() {
623639
e.message.should.equal('Invalid token: refresh token client is invalid');
624640
});
625641
});
642+
643+
it('should return a token', function() {
644+
var client = { id: 'foo' };
645+
var token = { refreshToken: 'hash', client: { id: 'foo'}, user: {}, refreshTokenExpiresAt: new Date(new Date() * 2) };
646+
var model = {
647+
getClient: function() {},
648+
revokeToken: function() {},
649+
getRefreshToken: function() { return token; },
650+
getAccessToken: function() {}
651+
};
652+
var handler = new RevokeHandler({ model: model });
653+
654+
return handler.getRefreshToken('hash', client)
655+
.then(function(token) {
656+
should.exist(token);
657+
})
658+
.catch(should.fail);
659+
});
660+
661+
it('should support callbacks', function() {
662+
var client = { id: 'foo' };
663+
var token = { refreshToken: 'hash', client: { id: 'foo'}, user: {}, refreshTokenExpiresAt: new Date(new Date() * 2) };
664+
var model = {
665+
getClient: function() {},
666+
revokeToken: function() {},
667+
getRefreshToken: function(refreshToken, callback) {
668+
callback(null, token);
669+
},
670+
getAccessToken: function() {}
671+
};
672+
var handler = new RevokeHandler({ model: model });
673+
674+
return handler.getRefreshToken('hash', client)
675+
.then(function(token) {
676+
should.exist(token);
677+
})
678+
.catch(should.fail);
679+
});
680+
});
681+
682+
describe('getAccessToken()', function() {
683+
it('should throw an error if the `accessToken` is invalid', function() {
684+
var client = {};
685+
var model = {
686+
getClient: function() {},
687+
revokeToken: function() {},
688+
getAccessToken: function() {},
689+
getRefreshToken: function() {}
690+
};
691+
var handler = new RevokeHandler({ model: model });
692+
693+
return handler.getAccessToken('hash', client)
694+
.then(should.fail)
695+
.catch(function(e) {
696+
e.should.be.an.instanceOf(InvalidTokenError);
697+
e.message.should.equal('Invalid token: access token is invalid');
698+
});
699+
});
700+
701+
it('should throw an error if the `client_id` does not match', function() {
702+
var client = { id: 'foo' };
703+
var token = { accessToken: 'hash', client: { id: 'baz'}, user: {}, accessTokenExpiresAt: new Date(new Date() * 2) };
704+
var model = {
705+
getClient: function() {},
706+
revokeToken: function() {},
707+
getAccessToken: function() { return token; },
708+
getRefreshToken: function() {}
709+
};
710+
var handler = new RevokeHandler({ model: model });
711+
712+
return handler.getAccessToken('hash', client)
713+
.then(should.fail)
714+
.catch(function(e) {
715+
e.should.be.an.instanceOf(InvalidTokenError);
716+
e.message.should.equal('Invalid token: access token client is invalid');
717+
});
718+
});
719+
720+
it('should return a token', function() {
721+
var client = { id: 'foo' };
722+
var token = { accessToken: 'hash', client: { id: 'foo'}, user: {}, accessTokenExpiresAt: new Date(new Date() * 2) };
723+
var model = {
724+
getClient: function() {},
725+
revokeToken: function() {},
726+
getAccessToken: function() { return token; },
727+
getRefreshToken: function() {}
728+
};
729+
var handler = new RevokeHandler({ model: model });
730+
731+
return handler.getAccessToken('hash', client)
732+
.then(function(token) {
733+
should.exist(token);
734+
})
735+
.catch(should.fail);
736+
});
737+
738+
it('should support callbacks', function() {
739+
var client = { id: 'foo' };
740+
var token = { accessToken: 'hash', client: { id: 'foo'}, user: {}, accessTokenExpiresAt: new Date(new Date() * 2) };
741+
var model = {
742+
getClient: function() {},
743+
revokeToken: function() {},
744+
getAccessToken: function(accessToken, callback) {
745+
callback(null, token);
746+
},
747+
getRefreshToken: function() {}
748+
};
749+
var handler = new RevokeHandler({ model: model });
750+
751+
return handler.getAccessToken('hash', client)
752+
.then(function(token) {
753+
should.exist(token);
754+
})
755+
.catch(should.fail);
756+
});
626757
});
627758

628759
describe('revokeToken()', function() {
@@ -644,6 +775,29 @@ describe('RevokeHandler integration', function() {
644775
e.message.should.equal('Invalid token: token is invalid');
645776
});
646777
});
778+
779+
it('should support callbacks', function() {
780+
var token = {};
781+
var client = {};
782+
var model = {
783+
getClient: function() {},
784+
revokeToken: function(tokenObject, callback) {
785+
callback(null, null);
786+
},
787+
getRefreshToken: function(refreshToken, callback) {
788+
callback(null, { client: {}, user: {}});
789+
},
790+
getAccessToken: function() {}
791+
};
792+
var handler = new RevokeHandler({ model: model });
793+
794+
return handler.revokeToken(token, client)
795+
.then(should.fail)
796+
.catch(function(e) {
797+
e.should.be.an.instanceOf(InvalidTokenError);
798+
e.message.should.equal('Invalid token: token is invalid');
799+
});
800+
});
647801
});
648802

649803
describe('getTokenFromRequest()', function() {

0 commit comments

Comments
 (0)