【发布时间】:2019-06-18 10:52:51
【问题描述】:
当在 node.js 应用程序中使用 passport 作为 Oauth 2.0 流(例如 Facebook、Twitter 等)的身份验证中间件时,我想知道存储访问令牌和刷新的常见/最佳实践是什么应用程序中的令牌。我不需要将用户帐户存储在应用程序中,我只需要访问令牌即可调用 API。
例如,如果我想通过 OAuth 2.0 身份验证提供程序对用户进行身份验证,以获取用于基于 oauth 的 API 的访问令牌,我可以使用以下 passport strategy:
passport.use(new OAuth2Strategy({
authorizationURL: 'https://www.example.com/oauth2/authorize',
tokenURL: 'https://www.example.com/oauth2/token',
clientID: EXAMPLE_CLIENT_ID,
clientSecret: EXAMPLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/example/callback"
},
function(accessToken, refreshToken, profile, cb) {
// handle user profile and tokens
// [...]
}
));
如何以及在何处以安全的方式存储令牌? 可以将令牌附加到用户个人资料吗?像这样:
function(accessToken, refreshToken, profile, cb) {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
process.nextTick(() => return cb(null, profile))
}
【问题讨论】:
标签: javascript node.js oauth-2.0 passport.js access-token