Skip to content

Commit 1eea733

Browse files
committed
support for short lived refresh token in implicit flow
1 parent 2c9e891 commit 1eea733

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/authCodeGrant.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ var tokenFns = [
4444
checkClient,
4545
checkUserApproved,
4646
generateAccessToken,
47+
generateRefreshToken,
4748
saveAccessToken,
49+
saveRefreshToken,
4850
redirect
4951
];
5052

@@ -197,6 +199,19 @@ function generateAccessToken (done) {
197199
});
198200
}
199201

202+
/* Generate refresh token
203+
*
204+
* @param {Function} done
205+
* @this OAuth
206+
*/
207+
function generateRefreshToken (done) {
208+
var self = this;
209+
token(this, 'refreshToken', function (err, token) {
210+
self.refreshToken = token;
211+
done(err);
212+
});
213+
}
214+
200215
/**
201216
* Check client against model
202217
*
@@ -242,6 +257,34 @@ function saveAccessToken (done) {
242257
});
243258
}
244259

260+
/**
261+
* Save refresh token with model
262+
*
263+
* @param {Function} done
264+
* @this OAuth
265+
*/
266+
function saveRefreshToken (done) {
267+
var refreshToken = this.refreshToken;
268+
269+
// Object idicates a reissue
270+
if (typeof refreshToken === 'object' && refreshToken.refreshToken) {
271+
this.refreshToken = refreshToken.refreshToken;
272+
return done();
273+
}
274+
275+
var expires = null;
276+
if (this.config.refreshTokenLifetime !== null) {
277+
expires = new Date();
278+
expires.setSeconds(expires.getSeconds() + this.config.implicitRefreshTokenLifetime);
279+
}
280+
281+
this.model.saveRefreshToken(refreshToken, this.client.clientId, expires,
282+
this.user, function (err) {
283+
if (err) return done(error('server_error', false, err));
284+
done();
285+
});
286+
}
287+
245288
/**
246289
* Check client against model
247290
*
@@ -255,7 +298,7 @@ function redirect (done) {
255298
if(this.responseType === 'code') {
256299
url = this.client.redirectUri + '?code=' + this.authCode;
257300
} else {
258-
url = this.client.redirectUri + '?access_token=' + this.accessToken +'&token_type=bearer';
301+
url = this.client.redirectUri + '?access_token=' + this.accessToken + '&refresh_token=' + this.refreshToken + "&expires_in=" + this.config.accessTokenLifetime +'&token_type=bearer';
259302
}
260303

261304
if(typeof this.req.body.state !== 'undefined') {

lib/oauth2server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ function OAuth2Server (config) {
4949
config.implicitEnabled : false;
5050
this.refreshTokenLifetime = config.refreshTokenLifetime !== undefined ?
5151
config.refreshTokenLifetime : 1209600;
52+
this.implicitRefreshTokenLifetime =
53+
config.implicitRefreshTokenLifetime !== undefined ?
54+
config.implicitRefreshTokenLifetime : 86400;
5255
this.authCodeLifetime = config.authCodeLifetime || 30;
5356

5457
this.regex = {

0 commit comments

Comments
 (0)