Fix oAuth to properly renew tokens

This commit is contained in:
Ben Hardill
2018-04-28 21:43:53 +01:00
parent 1a7c56af6f
commit 2bcca3f6ce
2 changed files with 10 additions and 26 deletions

View File

@@ -35,8 +35,8 @@ var AccessTokenSchema = new Schema({
scope: [ { type: String }],
expires: { type: Date, default: function(){
var today = new Date();
//var length = 60 * 24 * 90; // Length (in minutes) of our access token
var length = 60 * 24 * 365 * 100;
var length = 60 * 24 * 90; // Length (in minutes) of our access token
//var length = 60 * 24 * 365 * 100;
return new Date(today.getTime() + length*60000);
} },
active: { type: Boolean, get: function(value) {

View File

@@ -44,7 +44,8 @@ server.exchange(oauth2orize.exchange.code({
if (token) {
OAuth.RefreshToken.findOne({application:application, user: grant.user},function(error, refreshToken){
if (refreshToken){
done(null,token.token, refreshToken.token,{token_type: 'standard'});
var expires = Math.round((token.expires - (new Date().getTime()))/1000);
done(null,token.token, refreshToken.token,{token_type: 'Bearer', expires_in: expires});
} else {
// Shouldn't get here unless there is an error as there
// should be a refresh token if there is an access token
@@ -60,10 +61,11 @@ server.exchange(oauth2orize.exchange.code({
});
token.save(function(error){
var expires = Math.round((token.expires - (new Date().getTime()))/1000);
//delete old refreshToken or reuse?
OAuth.RefreshToken.findOne({application:application, user: grant.user},function(error, refreshToken){
if (refreshToken) {
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'standard' });
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope});
} else if (!error) {
var refreshToken = new OAuth.RefreshToken({
user: grant.user,
@@ -71,7 +73,7 @@ server.exchange(oauth2orize.exchange.code({
});
refreshToken.save(function(error){
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'standard' });
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope });
});
} else {
done(error);
@@ -83,25 +85,6 @@ server.exchange(oauth2orize.exchange.code({
}
});
//console.log("exchange user ", grant.user);
// var token = new OAuth.AccessToken({
// application: grant.application,
// user: grant.user,
// grant: grant,
// scope: grant.scope
// });
// token.save(function(error) {
// var refreshToken = new OAuth.RefreshToken({
// user: grant.user,
// application: grant.application
// });
// refreshToken.save(function(error){
// done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'standard' });
// });
// });
} else {
done(error, false);
}
@@ -120,12 +103,13 @@ server.exchange(oauth2orize.exchange.refreshToken({
application: refresh.application,
user: refresh.user,
grant: grant,
scope: scope
scope: grant.scope
});
newToken.save(function(error){
var expires = Math.round((newToken.expires - (new Date().getTime()))/1000);
if (!error) {
done(null, newToken.token);
done(null, newToken.token, refresh.token, {token_type: 'Bearer', expires_in: expires, scope: newToken.scope});
} else {
done(error,false);
}