我是 Node.js 的新手。我正在使用 express、passport、passport-oauth2 策略对我的 React 应用程序进行身份验证。
以下是我的节点服务 index.js 中的passport.js 策略配置
passport.use(
new OAuth2Strategy(
{
authorizationURL: 'https://idp-portal-URL', // IDP portal url
tokenURL: 'https://token.oauth2', // token URL
callbackURL: 'https://localhost:8060/auth', // **REACT application call back URL**
clientID: clientCreds.clientID,
clientSecret: clientCreds.clientSecret,
scope: 'openid profile',
},
function(accessken, refreshToken, user, profile, done) {
const ca = user.id_token;
return done(null, user);
},
),
);
然后在路由下面进行身份验证:这是我从 React 应用程序调用的路由,例如:'http://localhost:8080/login'
app.get(
'/login',
passport.authenticate('oauth2', { failureRedirect: '/failed' }),
function(req, res, next) {
next();
const config = {
method: 'post',
url: `https://idp-a/authorization.oauth2?token=${req.user.access_token}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: {
client_id: clientCreds.clientID,
client_secret: clientCreds.clientSecret
},
};
axios(config)
.then(function(response) {
res.redirect(`https://localhost:8060/#/auth?ssoParams=${queryParam}`);
})
.catch(function(error) {
console.log(error);
});
// Successful authentication, redirect home.
},
);
使用上面的代码,用户能够成功地进行身份验证并重定向回我的反应应用程序 URL,该 URL 配置为回调 URL,并在查询参数中附加身份验证代码。像下面这样: “https://localhost:8060/auth?code=***************”
但在此之前,在节点服务本身中,我想使用查询参数中收到的身份验证代码检索访问令牌。
如何通过进行 API 调用来使用身份验证代码检索访问令牌,然后使用访问代码重定向回我的 React 应用程序。我在回调函数中写的 axios 调用没有执行,控制台没有打印或没有错误。
请在这里提供帮助,感谢您的帮助。