gpt4 book ai didi

javascript - 在 Passportjs 中刷新 JWT token

转载 作者:可可西里 更新时间:2023-11-01 02:25:52 26 4
gpt4 key购买 nike

我正在使用 passport-openidconnect 策略,它运行良好,但 session 的到期时间很短 3600 秒,我认为它不可更改。

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

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

最佳答案

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

enter image description here

按照refresh token的说法,passport本身不支持,需要我们去实现。对于 auth0,您可以按照 https://auth0.com/docs/tokens/refresh-token/current 处的流程更新 token 。 .我粘贴了该链接中的代码:

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);
});

或者您可以使用 Passport 附加组件 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.

});

关于javascript - 在 Passportjs 中刷新 JWT token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55924774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com