在Passport.js中刷新JWT令牌

Mik*_*ike 11 javascript openid-connect passport.js auth0 onelogin

我使用的护照-openidconnect策略效果很好,但是会话的有效期很短,只有3600秒,我认为它是不会更改的。

我会使用刷新令牌来获取另一个令牌ID吗?

如果我这样做,该在哪里添加这种逻辑呢?https://github.com/passport/express-4.x-openidconnect-example/blob/master/server.js

man*_*shg 10

会话的到期时间可从身份验证提供者端进行配置。例如,假设您auth0用作身份验证提供程序,然后您可以token在应用设置中配置超时(https://auth0.com/docs/tokens/guides/access-token/set-access-token-lifetime

在此处输入图片说明

refresh token目前而言,护照本身不支持它,由我们来实施它。对于 auth0,您可以按照https://auth0.com/docs/tokens/refresh-token/current 上的流程更新令牌。我粘贴了该链接中的代码:

var request = require("request");

var options = { method: 'POST',
  url: 'https://YOUR_DOMAIN/oauth/token',
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
  form: 
   { grant_type: 'refresh_token',
     client_id: 'YOUR_CLIENT_ID',
     client_secret: 'YOUR_CLIENT_SECRET',
     refresh_token: 'YOUR_REFRESH_TOKEN' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
Run Code Online (Sandbox Code Playgroud)

或者您可以使用护照附加组件https://github.com/fiznool/passport-oauth2-refresh

var passport = require('passport'),
  , refresh = require('passport-oauth2-refresh')
  , FacebookStrategy = require('passport-facebook').Strategy;

var strategy = new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: "http://www.example.com/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
  // Make sure you store the refreshToken somewhere!
  User.findOrCreate(..., function(err, user) {
    if (err) { return done(err); }
    done(null, user);
  });
});

passport.use(strategy);
refresh.use(strategy);

var refresh = require('passport-oauth2-refresh');
refresh.requestNewAccessToken('facebook', 'some_refresh_token', function(err, accessToken, refreshToken) {
  // You have a new access token, store it in the user object,
  // or use it to make a new request.
  // `refreshToken` may or may not exist, depending on the strategy you are using.
  // You probably don't need it anyway, as according to the OAuth 2.0 spec,
  // it should be the same as the initial refresh token.

});
Run Code Online (Sandbox Code Playgroud)