簡體   English   中英

在Passportjs中刷新JWT令牌

[英]Refreshing JWT token in Passportjs

我使用的是passport-openidconnect策略,雖然運行良好,但會話到期時間短了3600秒,我認為它不會改變。

我會使用刷新令牌獲取另一個令牌ID嗎?

如果我在哪里,我會在這樣的東西中添加那個邏輯? https://github.com/passport/express-4.x-openidconnect-example/blob/master/server.js

會話到期可以從身份驗證提供程序端配置。 例如,假設您使用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);
});

或者您可以使用附加護照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.

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM