Skip to content

Commit a53c893

Browse files
author
Thom Seddon
committed
Add optional generateToken method to model
Allows you to override the default token generation mechanism
1 parent db28ef9 commit a53c893

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

Readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ The module requires a model object through which some aspects or storage, retrie
5555
The last parameter of all methods is a callback of which the first parameter is always used to indicate an error.
5656
A model must provide the following methods:
5757

58+
### Required
59+
5860
### getAccessToken(bearerToken, callback)
5961
- `bearerToken` `String` The bearer token (access token) that has been provided
6062
- `callback` `Function` callback(error, accessToken)
@@ -102,13 +104,22 @@ A model must provide the following methods:
102104
- `error` `Mixed` Truthy to indicate an error
103105
- `user` `Object|Boolean` The user retrieved from storage or falsey to indicate an invalid user (saved in req.user)
104106

107+
108+
### Optional
109+
105110
### extendedGrant(req, callback)
106111
- `req` `Object` The raw request
107112
- `callback` `Function` callback(error, supported, user)
108113
- `error` `Mixed` Truthy to indicate an error
109114
- `supported` `Boolean` Whether the grant type is supported
110115
- `user` `Object|Boolean` The user retrieved from storage or falsey to indicate an invalid user (saved in req.user), must at least have an id
111116

117+
### generateToken(type, callback)
118+
- `type` `String` Token type, one of 'accessToken' or 'refreshToken'
119+
- `callback` `Function` callback(error, token)
120+
- `error` `Mixed` Truthy to indicate an error
121+
- `token` `String` The access token
122+
112123
## Extension Grants
113124
You can support extension/custom grants by implementing the extendedGrant method as outlined above.
114125
Any requests that begin with http(s):// (as [defined in the spec](http://tools.ietf.org/html/rfc6749#section-4.5)) will be passed to it for you to handle.

lib/token.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ token.grantAccessToken = function (req, res, next) {
200200

201201
// Are we issuing refresh tokens?
202202
if (oauth.grants.indexOf('refresh_token') >= 0) {
203-
token.generateToken(createRefreshToken);
203+
token.generateToken.call(oauth, 'refreshToken', createRefreshToken);
204204
} else {
205205
token.issueToken.call(oauth, req, res, next);
206206
}
207207
});
208208
};
209209

210-
token.generateToken(createAccessToken);
210+
token.generateToken.call(oauth, 'accessToken', createAccessToken);
211211
};
212212

213213
/**
@@ -236,7 +236,9 @@ token.issueToken = function (req, res, next) {
236236
* @param {Function} next Connect next
237237
* @return {String} Random 40 char token
238238
*/
239-
token.generateToken = function (callback) {
239+
token.generateToken = function (type, callback) {
240+
if (this.model.generateToken) return this.model.generateToken(type, callback);
241+
240242
crypto.randomBytes(256, function (ex, buffer) {
241243
if (ex) return callback(new OAuth2Error('server_error'));
242244

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-oauth2-server",
33
"description": "Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with express in node.js",
4-
"version": "1.1.1",
4+
"version": "1.2.0",
55
"keywords": [
66
"oauth",
77
"oauth2"

test/oauth2server.token.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,43 @@ describe('OAuth2Server.token()', function() {
302302
});
303303
});
304304

305+
describe('generate access token', function () {
306+
it('should allow override via model', function (done) {
307+
var app = bootstrap({
308+
model: {
309+
getClient: function (id, secret, callback) {
310+
callback(false, { client_id: id });
311+
},
312+
grantTypeAllowed: function (id, secret, callback) {
313+
callback(false, true);
314+
},
315+
getUser: function (uname, pword, callback) {
316+
callback(false, { id: 1 });
317+
},
318+
generateToken: function (type, callback) {
319+
callback(false, 'thommy');
320+
},
321+
saveAccessToken: function (accessToken, clientId, userId, expires, callback) {
322+
try {
323+
accessToken.should.equal('thommy');
324+
callback();
325+
} catch (e) {
326+
return callback(e);
327+
}
328+
}
329+
},
330+
grants: ['password']
331+
});
332+
333+
request(app)
334+
.post('/oauth/token')
335+
.set('Content-Type', 'application/x-www-form-urlencoded')
336+
.send(validBody)
337+
.expect(/thommy/, 200, done);
338+
339+
});
340+
});
341+
305342
describe('saving access token', function () {
306343
it('should pass valid params to model.saveAccessToken', function (done) {
307344
var app = bootstrap({
@@ -463,4 +500,4 @@ describe('OAuth2Server.token()', function() {
463500
});
464501
});
465502

466-
});
503+
});

0 commit comments

Comments
 (0)