Skip to content

Commit 9e9f279

Browse files
author
Thom Seddon
committed
camelCase all the things + switch back to explicit save*Token signature
There was still a confusing mix of under_scored naming and camelCase naming, I've now draw the line as follows: - Everything coming in from the wild, should be underscored (as per the spec) - Everything is camelCased internally Switching to the save*Token(data, callback) was shorter, but made variable names VERY important, this removes some of that overhead by reintroducing the longer save*Token(token, clientId, expires, userId, callback) style signatures.
1 parent 653de3a commit 9e9f279

File tree

10 files changed

+147
-155
lines changed

10 files changed

+147
-155
lines changed

Readme.md

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
9595
- *date* **expires**
9696
- The date when it expires
9797
- `null` to indicate the token **never expires**
98-
- *string|number* **user_id**
99-
- The user_id (saved in req.user.id)
98+
- *string|number* **userId**
99+
- The user id (saved in req.user.id)
100100

101101
#### getClient (clientId, clientSecret, callback)
102102
- *string* **clientId**
@@ -108,7 +108,7 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
108108
- The client retrieved from storage or falsey to indicate an invalid client
109109
- Saved in `req.client`
110110
- Must contain the following keys:
111-
- *string* **client_id**
111+
- *string* **clientId**
112112

113113
#### grantTypeAllowed (clientId, grantType, callback)
114114
- *string* **clientId**
@@ -119,12 +119,11 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
119119
- *boolean* **allowed**
120120
- Indicates whether the grantType is allowed for this clientId
121121

122-
#### saveAccessToken (accessToken, callback)
123-
- *object* **accessToken**
124-
- *string* **accessToken**
125-
- *string* **clientId**
126-
- *string|number* **userId**
127-
- *date* **expires**
122+
#### saveAccessToken (accessToken, clientId, expires, user, callback)
123+
- *string* **accessToken**
124+
- *string* **clientId**
125+
- *string|number* **userId**
126+
- *date* **expires**
128127
- *function* **callback (error)**
129128
- *mixed* **error**
130129
- Truthy to indicate an error
@@ -140,20 +139,19 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
140139
- *object* **authCode**
141140
- The authorization code retrieved form storage or falsey to indicate invalid code
142141
- Must contain the following keys:
143-
- *string|number* **client_id**
144-
- client_id associated with this auth code
142+
- *string|number* **clientId**
143+
- client id associated with this auth code
145144
- *date* **expires**
146145
- The date when it expires
147-
- *string|number* **user_id**
148-
- The user_id
149-
150-
#### saveAuthCode (authCode, callback)
151-
- *object* **authCode**
152-
- *string* **auth_code**
153-
- *string* **client_id**
154-
- *date* **expires**
155-
- *mixed* **user**
156-
- Whatever was passed as `user` to the codeGrant function (see example)
146+
- *string|number* **userId**
147+
- The userId
148+
149+
#### saveAuthCode (authCode, clientId, expires, user, callback)
150+
- *string* **authCode**
151+
- *string* **clientId**
152+
- *date* **expires**
153+
- *mixed* **user**
154+
- Whatever was passed as `user` to the codeGrant function (see example)
157155
- *function* **callback (error)**
158156
- *mixed* **error**
159157
- Truthy to indicate an error
@@ -175,12 +173,11 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
175173

176174
### Required for `refresh_token` grant type
177175

178-
#### saveRefreshToken (refreshToken, callback)
179-
- *object* **refreshToken**
180-
- *string* **refreshToken**
181-
- *string* **clientId**
182-
- *string|number* **userId**
183-
- *date* **expires**
176+
#### saveRefreshToken (refreshToken, clientId, expires, user, callback)
177+
- *string* **refreshToken**
178+
- *string* **clientId**
179+
- *string|number* **userId**
180+
- *date* **expires**
184181
- *function* **callback (error)**
185182
- *mixed* **error**
186183
- Truthy to indicate an error
@@ -194,13 +191,13 @@ Note: see https://github.com/nightworld/node-oauth2-server/tree/master/examples/
194191
- *object* **refreshToken**
195192
- The refresh token retrieved form storage or falsey to indicate invalid refresh token
196193
- Must contain the following keys:
197-
- *string|number* **client_id**
198-
- client_id associated with this token
194+
- *string|number* **clientId**
195+
- client id associated with this token
199196
- *date* **expires**
200197
- The date when it expires
201198
- `null` to indicate the token **never expires**
202-
- *string|number* **user_id**
203-
- The user_id
199+
- *string|number* **userId**
200+
- The userId
204201

205202

206203
### Optional for Refresh Token grant type
@@ -241,7 +238,7 @@ The spec does not actually require that you revoke the old token - hence this is
241238
- *null* indicates to revert to the default token generator
242239
- *object* indicates a reissue (i.e. will not be passed to saveAccessToken/saveRefreshToken)
243240
- Must contain the following keys (if object):
244-
- *string* **access_token** OR **refresh_token** dependant on type
241+
- *string* **accessToken** OR **refreshToken** dependant on type
245242

246243
## Extension Grants
247244
You can support extension/custom grants by implementing the extendedGrant method as outlined above.

examples/postgresql/model.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@ model.getAccessToken = function (bearerToken, callback) {
2727
if (err) return callback(err);
2828
client.query('SELECT access_token, client_id, expires, user_id FROM oauth_access_tokens ' +
2929
'WHERE access_token = $1', [bearerToken], function (err, result) {
30+
if (err || !result.rowCount) return callback(err);
3031
// This object will be exposed in req.oauth.token
3132
// The user_id field will be exposed in req.user (req.user = { id: "..." }) however if
3233
// an explicit user object is included (token.user, must include id) it will be exposed
3334
// in req.user instead
34-
callback(err, result.rowCount ? result.rows[0] : false);
35+
var token = result.rows[0];
36+
callback(null, {
37+
accessToken: token.access_token,
38+
clientId: token.client_id,
39+
expires: token.expires,
40+
userId: token.userId
41+
});
3542
done();
3643
});
3744
});
@@ -43,8 +50,14 @@ model.getClient = function (clientId, clientSecret, callback) {
4350
client.query('SELECT client_id, client_secret, redirect_uri FROM oauth_clients WHERE ' +
4451
'client_id = $1 AND client_secret = $2', [clientId, clientSecret],
4552
function (err, result) {
53+
if (err || !result.rowCount) return callback(err);
54+
4655
// This object will be exposed in req.oauth.client
47-
callback(err, result.rowCount ? result.rows[0] : false);
56+
var client = result.rows[0];
57+
callback(null, {
58+
clientId: client.client_id,
59+
clientSecret: client.client_secret
60+
});
4861
done();
4962
});
5063
});
@@ -97,4 +110,4 @@ model.getUser = function (username, password, callback) {
97110
done();
98111
});
99112
});
100-
};
113+
};

lib/authCodeGrant.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function AuthCodeGrant(config, req, res, next, check) {
5353
runner(fns, this, function (err) {
5454
if (err && res.oauthRedirect) {
5555
// Custom redirect error handler
56-
return res.redirect(self.client.redirect_uri + '?error=' + err.error +
56+
return res.redirect(self.client.redirectUri + '?error=' + err.error +
5757
'&error_description=' + err.error_description + '&code=' + err.code);
5858
}
5959

@@ -85,8 +85,8 @@ function checkParams (done) {
8585
}
8686

8787
// Redirect URI
88-
this.redirectURI = this.req.body.redirect_uri;
89-
if (!this.redirectURI) {
88+
this.redirectUri = this.req.body.redirect_uri;
89+
if (!this.redirectUri) {
9090
return done(error('invalid_request',
9191
'Invalid or missing redirect_uri parameter'));
9292
}
@@ -107,7 +107,7 @@ function checkClient (done) {
107107

108108
if (!client) {
109109
return done(error('invalid_client', 'Invalid client credentials'));
110-
} else if (client.redirect_uri !== self.redirectURI) {
110+
} else if (client.redirectUri !== self.redirectUri) {
111111
return done(error('invalid_request', 'redirect_uri does not match'));
112112
}
113113

@@ -165,14 +165,8 @@ function saveAuthCode (done) {
165165
var expires = new Date();
166166
expires.setSeconds(expires.getSeconds() + this.config.authCodeLifetime);
167167

168-
var data = {
169-
auth_code: this.authCode,
170-
client_id: this.client.client_id,
171-
expires: expires,
172-
user: this.user
173-
};
174-
175-
this.model.saveAuthCode(data, function (err) {
168+
this.model.saveAuthCode(this.authCode, this.client.clientId, expires,
169+
this.user, function (err) {
176170
if (err) return done(error('server_error', false, err));
177171
done();
178172
});
@@ -185,5 +179,5 @@ function saveAuthCode (done) {
185179
* @this OAuth
186180
*/
187181
function redirect (done) {
188-
this.res.redirect(this.client.redirect_uri + '?code=' + this.authCode);
182+
this.res.redirect(this.client.redirectUri + '?code=' + this.authCode);
189183
}

lib/authorise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function checkToken (done) {
123123

124124
// Expose params
125125
self.req.oauth = { bearerToken: token };
126-
self.req.user = token.user ? token.user : { id: token.user_id };
126+
self.req.user = token.user ? token.user : { id: token.userId };
127127

128128
done();
129129
});

lib/grant.js

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ function extractCredentials (done) {
7979
// Extract credentials
8080
// http://tools.ietf.org/html/rfc6749#section-3.2.1
8181
this.client = credsFromBasic(this.req) || credsFromBody(this.req);
82-
if (!this.client.client_id ||
83-
!this.client.client_id.match(this.config.regex.clientId)) {
82+
if (!this.client.clientId ||
83+
!this.client.clientId.match(this.config.regex.clientId)) {
8484
return done(error('invalid_client',
8585
'Invalid or missing client_id parameter'));
86-
} else if (!this.client.client_secret) {
86+
} else if (!this.client.clientSecret) {
8787
return done(error('invalid_client', 'Missing client_secret parameter'));
8888
}
8989

@@ -97,8 +97,8 @@ function extractCredentials (done) {
9797
* @param {String} secret client_secret
9898
*/
9999
function Client (id, secret) {
100-
this.client_id = id;
101-
this.client_secret = secret;
100+
this.clientId = id;
101+
this.clientSecret = secret;
102102
}
103103

104104
/**
@@ -141,7 +141,7 @@ function credsFromBody (req) {
141141
* @this OAuth
142142
*/
143143
function checkClient (done) {
144-
this.model.getClient(this.client.client_id, this.client.client_secret,
144+
this.model.getClient(this.client.clientId, this.client.clientSecret,
145145
function (err, client) {
146146
if (err) return done(error('server_error', false, err));
147147

@@ -193,16 +193,16 @@ function useAuthCodeGrant (done) {
193193
this.model.getAuthCode(code, function (err, authCode) {
194194
if (err) return done(error('server_error', false, err));
195195

196-
if (!authCode || authCode.client_id !== self.client.client_id) {
196+
if (!authCode || authCode.clientId !== self.client.clientId) {
197197
return done(error('invalid_grant', 'Invalid code'));
198198
} else if (authCode.expires < self.now) {
199199
return done(error('invalid_grant', 'Code has expired'));
200200
}
201201

202-
self.user = authCode.user || { id: authCode.user_id };
202+
self.user = authCode.user || { id: authCode.userId };
203203
if (!self.user.id) {
204204
return done(error('server_error', false,
205-
'No user/user_id parameter returned from getauthCode'));
205+
'No user/userId parameter returned from getauthCode'));
206206
}
207207

208208
done();
@@ -251,16 +251,16 @@ function useRefreshTokenGrant (done) {
251251
this.model.getRefreshToken(token, function (err, refreshToken) {
252252
if (err) return done(error('server_error', false, err));
253253

254-
if (!refreshToken || refreshToken.client_id !== self.client.client_id) {
254+
if (!refreshToken || refreshToken.clientId !== self.client.clientId) {
255255
return done(error('invalid_grant', 'Invalid refresh token'));
256256
} else if (refreshToken.expires !== null &&
257257
refreshToken.expires < self.now) {
258258
return done(error('invalid_grant', 'Refresh token has expired'));
259259
}
260260

261-
if (!refreshToken.user_id) {
261+
if (!refreshToken.userId) {
262262
return done(error('server_error', false,
263-
'No user/user_id parameter returned from getRefreshToken'));
263+
'No user/userId parameter returned from getRefreshToken'));
264264
}
265265

266266
if (self.model.revokeRefreshToken) {
@@ -270,7 +270,7 @@ function useRefreshTokenGrant (done) {
270270
});
271271
}
272272

273-
self.user = refreshToken.user || { id: refreshToken.user_id };
273+
self.user = refreshToken.user || { id: refreshToken.userId };
274274
done();
275275
});
276276
}
@@ -308,7 +308,7 @@ function useExtendedGrant (done) {
308308
* @this OAuth
309309
*/
310310
function checkGrantTypeAllowed (done) {
311-
this.model.grantTypeAllowed(this.client.client_id, this.grantType,
311+
this.model.grantTypeAllowed(this.client.clientId, this.grantType,
312312
function (err, allowed) {
313313
if (err) return done(error('server_error', false, err));
314314

@@ -329,7 +329,7 @@ function checkGrantTypeAllowed (done) {
329329
*/
330330
function generateAccessToken (done) {
331331
var self = this;
332-
token(this, 'access_token', function (err, token) {
332+
token(this, 'accessToken', function (err, token) {
333333
self.accessToken = token;
334334
done(err);
335335
});
@@ -345,8 +345,8 @@ function saveAccessToken (done) {
345345
var accessToken = this.accessToken;
346346

347347
// Object idicates a reissue
348-
if (typeof accessToken === 'object' && accessToken.access_token) {
349-
this.accessToken = accessToken.access_token;
348+
if (typeof accessToken === 'object' && accessToken.accessToken) {
349+
this.accessToken = accessToken.accessToken;
350350
return done();
351351
}
352352

@@ -356,14 +356,8 @@ function saveAccessToken (done) {
356356
expires.setSeconds(expires.getSeconds() + this.config.accessTokenLifetime);
357357
}
358358

359-
var data = {
360-
access_token: accessToken,
361-
client_id: this.client.client_id,
362-
expires: expires,
363-
user: this.user
364-
};
365-
366-
this.model.saveAccessToken(data, function (err) {
359+
this.model.saveAccessToken(accessToken, this.client.clientId, expires,
360+
this.user, function (err) {
367361
if (err) return done(error('server_error', false, err));
368362
done();
369363
});
@@ -379,7 +373,7 @@ function generateRefreshToken (done) {
379373
if (this.config.grants.indexOf('refresh_token') === -1) return done();
380374

381375
var self = this;
382-
token(this, 'refresh_token', function (err, token) {
376+
token(this, 'refreshToken', function (err, token) {
383377
self.refreshToken = token;
384378
done(err);
385379
});
@@ -397,8 +391,8 @@ function saveRefreshToken (done) {
397391
if (!refreshToken) return done();
398392

399393
// Object idicates a reissue
400-
if (typeof refreshToken === 'object' && refreshToken.refresh_token) {
401-
this.refreshToken = refreshToken.refresh_token;
394+
if (typeof refreshToken === 'object' && refreshToken.refreshToken) {
395+
this.refreshToken = refreshToken.refreshToken;
402396
return done();
403397
}
404398

@@ -408,14 +402,8 @@ function saveRefreshToken (done) {
408402
expires.setSeconds(expires.getSeconds() + this.config.refreshTokenLifetime);
409403
}
410404

411-
var data = {
412-
refresh_token: refreshToken,
413-
client_id: this.client.client_id,
414-
expires: expires,
415-
user: this.user
416-
};
417-
418-
this.model.saveRefreshToken(data, function (err) {
405+
this.model.saveRefreshToken(refreshToken, this.client.clientId, expires,
406+
this.user, function (err) {
419407
if (err) return done(error('server_error', false, err));
420408
done();
421409
});

0 commit comments

Comments
 (0)