Skip to content

Commit d8dd547

Browse files
committed
Fix middleware authentication storage
Fixes #4.
1 parent 5d7b3cb commit d8dd547

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ExpressOAuthServer.prototype.authenticate = function() {
4444
return server.authenticate(request, response);
4545
})
4646
.tap(function(token) {
47-
req.app.locals.oauth = { token: token };
47+
res.locals.oauth = { token: token };
4848
})
4949
.catch(function(e) {
5050
return handleError(e, req, res);
@@ -73,7 +73,7 @@ ExpressOAuthServer.prototype.authorize = function() {
7373
return server.authorize(request, response);
7474
})
7575
.tap(function(code) {
76-
req.app.locals.oauth = { code: code };
76+
res.locals.oauth = { code: code };
7777
})
7878
.then(function() {
7979
return handleResponse(req, res, response);
@@ -105,7 +105,7 @@ ExpressOAuthServer.prototype.token = function() {
105105
return server.token(request, response);
106106
})
107107
.tap(function(token) {
108-
req.app.locals.oauth = { token: token };
108+
res.locals.oauth = { token: token };
109109
})
110110
.then(function() {
111111
return handleResponse(req, res, response);

test/integration/index_test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,62 @@ describe('ExpressOAuthServer', function() {
7979
.expect(200)
8080
.end(done);
8181
});
82+
83+
it('should cache the authorization token', function(done) {
84+
var token = { user: {} };
85+
var model = {
86+
getAccessToken: function() {
87+
return token;
88+
}
89+
};
90+
var oauth = new ExpressOAuthServer({ model: model });
91+
92+
app.use(oauth.authenticate());
93+
94+
app.use(function(req, res, next) {
95+
res.locals.oauth.token.should.equal(token);
96+
97+
next();
98+
});
99+
100+
request(app.listen())
101+
.get('/')
102+
.set('Authorization', 'Bearer foobar')
103+
.end(done);
104+
});
82105
});
83106

84107
describe('authorize()', function() {
108+
it('should cache the authorization code', function(done) {
109+
var code = { authorizationCode: 123 };
110+
var model = {
111+
getAccessToken: function() {
112+
return { user: {} };
113+
},
114+
getClient: function() {
115+
return { grants: ['authorization_code'], redirectUris: ['http://example.com'] };
116+
},
117+
saveAuthorizationCode: function() {
118+
return code;
119+
}
120+
};
121+
var oauth = new ExpressOAuthServer({ model: model });
122+
123+
app.use(oauth.authorize());
124+
125+
app.use(function(req, res, next) {
126+
res.locals.oauth.code.should.equal(code);
127+
128+
next();
129+
});
130+
131+
request(app.listen())
132+
.post('/?state=foobiz')
133+
.set('Authorization', 'Bearer foobar')
134+
.send({ client_id: 12345, response_type: 'code' })
135+
.end(done);
136+
});
137+
85138
it('should return a `location` header with the error', function(done) {
86139
var model = {
87140
getAccessToken: function() {
@@ -143,6 +196,36 @@ describe('ExpressOAuthServer', function() {
143196
});
144197

145198
describe('token()', function() {
199+
it('should cache the authorization token', function(done) {
200+
var token = { accessToken: 'foobar', client: {}, user: {} };
201+
var model = {
202+
getClient: function() {
203+
return { grants: ['password'] };
204+
},
205+
getUser: function() {
206+
return {};
207+
},
208+
saveToken: function() {
209+
return token;
210+
}
211+
};
212+
var oauth = new ExpressOAuthServer({ model: model });
213+
214+
app.use(oauth.token());
215+
216+
app.use(function(req, res, next) {
217+
res.locals.oauth.token.should.equal(token);
218+
219+
next();
220+
});
221+
222+
request(app.listen())
223+
.post('/')
224+
.send('client_id=foo&client_secret=bar&grant_type=password&username=qux&password=biz')
225+
.expect({ access_token: 'foobar', token_type: 'bearer' })
226+
.end(done);
227+
});
228+
146229
it('should return an `access_token`', function(done) {
147230
var model = {
148231
getClient: function() {

0 commit comments

Comments
 (0)