Skip to content

Commit 99968b5

Browse files
author
Michael Salinger
committed
Pass options to server.authorize, server.authenticate, and server.token
1 parent d8dd547 commit 99968b5

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function ExpressOAuthServer(options) {
3232
* (See: https://tools.ietf.org/html/rfc6749#section-7)
3333
*/
3434

35-
ExpressOAuthServer.prototype.authenticate = function() {
35+
ExpressOAuthServer.prototype.authenticate = function(options) {
3636
var server = this.server;
3737

3838
return function(req, res, next) {
@@ -41,7 +41,7 @@ ExpressOAuthServer.prototype.authenticate = function() {
4141

4242
return Promise.bind(this)
4343
.then(function() {
44-
return server.authenticate(request, response);
44+
return server.authenticate(request, response, options);
4545
})
4646
.tap(function(token) {
4747
res.locals.oauth = { token: token };
@@ -61,7 +61,7 @@ ExpressOAuthServer.prototype.authenticate = function() {
6161
* (See: https://tools.ietf.org/html/rfc6749#section-3.1)
6262
*/
6363

64-
ExpressOAuthServer.prototype.authorize = function() {
64+
ExpressOAuthServer.prototype.authorize = function(options) {
6565
var server = this.server;
6666

6767
return function(req, res, next) {
@@ -70,7 +70,7 @@ ExpressOAuthServer.prototype.authorize = function() {
7070

7171
return Promise.bind(this)
7272
.then(function() {
73-
return server.authorize(request, response);
73+
return server.authorize(request, response, options);
7474
})
7575
.tap(function(code) {
7676
res.locals.oauth = { code: code };
@@ -93,7 +93,7 @@ ExpressOAuthServer.prototype.authorize = function() {
9393
* (See: https://tools.ietf.org/html/rfc6749#section-3.2)
9494
*/
9595

96-
ExpressOAuthServer.prototype.token = function() {
96+
ExpressOAuthServer.prototype.token = function(options) {
9797
var server = this.server;
9898

9999
return function(req, res, next) {
@@ -102,7 +102,7 @@ ExpressOAuthServer.prototype.token = function() {
102102

103103
return Promise.bind(this)
104104
.then(function() {
105-
return server.token(request, response);
105+
return server.token(request, response, options);
106106
})
107107
.tap(function(token) {
108108
res.locals.oauth = { token: token };

test/unit/index_test.js

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Response = require('oauth2-server').Response;
99
var express = require('express');
1010
var request = require('supertest');
1111
var sinon = require('sinon');
12+
var should = require('should');
1213

1314
/**
1415
* Test `ExpressOAuthServer`.
@@ -33,9 +34,31 @@ describe('ExpressOAuthServer', function() {
3334
.get('/')
3435
.end(function() {
3536
oauth.server.authenticate.callCount.should.equal(1);
36-
oauth.server.authenticate.firstCall.args.should.have.length(2);
37+
oauth.server.authenticate.firstCall.args.should.have.length(3);
3738
oauth.server.authenticate.firstCall.args[0].should.be.an.instanceOf(Request);
3839
oauth.server.authenticate.firstCall.args[1].should.be.an.instanceOf(Response);
40+
should.not.exist(oauth.server.authenticate.firstCall.args[2])
41+
oauth.server.authenticate.restore();
42+
43+
done();
44+
});
45+
});
46+
47+
it('should call `authenticate()` with options', function(done) {
48+
var oauth = new ExpressOAuthServer({ model: {} });
49+
50+
sinon.stub(oauth.server, 'authenticate').returns({});
51+
52+
app.use(oauth.authenticate({options: true}));
53+
54+
request(app.listen())
55+
.get('/')
56+
.end(function() {
57+
oauth.server.authenticate.callCount.should.equal(1);
58+
oauth.server.authenticate.firstCall.args.should.have.length(3);
59+
oauth.server.authenticate.firstCall.args[0].should.be.an.instanceOf(Request);
60+
oauth.server.authenticate.firstCall.args[1].should.be.an.instanceOf(Response);
61+
oauth.server.authenticate.firstCall.args[2].should.eql({options: true});
3962
oauth.server.authenticate.restore();
4063

4164
done();
@@ -55,9 +78,31 @@ describe('ExpressOAuthServer', function() {
5578
.get('/')
5679
.end(function() {
5780
oauth.server.authorize.callCount.should.equal(1);
58-
oauth.server.authorize.firstCall.args.should.have.length(2);
81+
oauth.server.authorize.firstCall.args.should.have.length(3);
5982
oauth.server.authorize.firstCall.args[0].should.be.an.instanceOf(Request);
6083
oauth.server.authorize.firstCall.args[1].should.be.an.instanceOf(Response);
84+
should.not.exist(oauth.server.authorize.firstCall.args[2]);
85+
oauth.server.authorize.restore();
86+
87+
done();
88+
});
89+
});
90+
91+
it('should call `authorize()` with options', function(done) {
92+
var oauth = new ExpressOAuthServer({ model: {} });
93+
94+
sinon.stub(oauth.server, 'authorize').returns({});
95+
96+
app.use(oauth.authorize({options: true}));
97+
98+
request(app.listen())
99+
.get('/')
100+
.end(function() {
101+
oauth.server.authorize.callCount.should.equal(1);
102+
oauth.server.authorize.firstCall.args.should.have.length(3);
103+
oauth.server.authorize.firstCall.args[0].should.be.an.instanceOf(Request);
104+
oauth.server.authorize.firstCall.args[1].should.be.an.instanceOf(Response);
105+
oauth.server.authorize.firstCall.args[2].should.eql({options: true});
61106
oauth.server.authorize.restore();
62107

63108
done();
@@ -77,9 +122,31 @@ describe('ExpressOAuthServer', function() {
77122
.get('/')
78123
.end(function() {
79124
oauth.server.token.callCount.should.equal(1);
80-
oauth.server.token.firstCall.args.should.have.length(2);
125+
oauth.server.token.firstCall.args.should.have.length(3);
126+
oauth.server.token.firstCall.args[0].should.be.an.instanceOf(Request);
127+
oauth.server.token.firstCall.args[1].should.be.an.instanceOf(Response);
128+
should.not.exist(oauth.server.token.firstCall.args[2]);
129+
oauth.server.token.restore();
130+
131+
done();
132+
});
133+
});
134+
135+
it('should call `token()` with options', function(done) {
136+
var oauth = new ExpressOAuthServer({ model: {} });
137+
138+
sinon.stub(oauth.server, 'token').returns({});
139+
140+
app.use(oauth.token({options: true}));
141+
142+
request(app.listen())
143+
.get('/')
144+
.end(function() {
145+
oauth.server.token.callCount.should.equal(1);
146+
oauth.server.token.firstCall.args.should.have.length(3);
81147
oauth.server.token.firstCall.args[0].should.be.an.instanceOf(Request);
82148
oauth.server.token.firstCall.args[1].should.be.an.instanceOf(Response);
149+
oauth.server.token.firstCall.args[2].should.eql({options: true});
83150
oauth.server.token.restore();
84151

85152
done();

0 commit comments

Comments
 (0)