From 81bdf29199ce22e677598a608afe53d4008b3780 Mon Sep 17 00:00:00 2001 From: Thom Seddon Date: Wed, 31 Dec 2014 12:58:49 +0000 Subject: [PATCH] Expose user and client to generateToken. Closes #120 Closes #124 Closes #132 --- lib/grant.js | 18 ++++++++++++++++++ test/grant.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/grant.js b/lib/grant.js index 2ebbcd817..7ceb6c640 100644 --- a/lib/grant.js +++ b/lib/grant.js @@ -31,6 +31,7 @@ var fns = [ checkClient, checkGrantTypeAllowed, checkGrantType, + exposeParams, generateAccessToken, saveAccessToken, generateRefreshToken, @@ -342,6 +343,23 @@ function checkGrantTypeAllowed (done) { }); } +/** + * Expose user and client params + * + * @param {Function} done + * @this OAuth + */ +function exposeParams (done) { + this.req.oauth = this.req.oauth || {}; + this.req.oauth.client = { + id: this.client.clientId, + secret: this.client.clientSecret + }; + this.req.user = this.user; + + done(); +} + /** * Generate an access token * diff --git a/test/grant.js b/test/grant.js index b6a323b61..2d5820de0 100644 --- a/test/grant.js +++ b/test/grant.js @@ -252,6 +252,40 @@ describe('Grant', function() { }); + it('should include client and user in request', function (done) { + var app = bootstrap({ + model: { + getClient: function (id, secret, callback) { + callback(false, true); + }, + grantTypeAllowed: function (clientId, grantType, callback) { + callback(false, true); + }, + getUser: function (uname, pword, callback) { + callback(false, { id: 1 }); + }, + generateToken: function (type, req, callback) { + req.oauth.client.id.should.equal('thom'); + req.oauth.client.secret.should.equal('nightworld'); + req.user.id.should.equal(1); + callback(false, 'thommy'); + }, + saveAccessToken: function (token, clientId, expires, user, cb) { + token.should.equal('thommy'); + cb(); + } + }, + grants: ['password'] + }); + + request(app) + .post('/oauth/token') + .set('Content-Type', 'application/x-www-form-urlencoded') + .send(validBody) + .expect(200, /thommy/, done); + + }); + it('should reissue if model returns object', function (done) { var app = bootstrap({ model: {