ExpressJS 护照 OIDC 回调未被调用

编程入门 行业动态 更新时间:2024-10-06 12:35:22

ExpressJS <a href=https://www.elefans.com/category/jswz/34/1771386.html style=护照 OIDC 回调未被调用"/>

ExpressJS 护照 OIDC 回调未被调用

我是身份验证系统或 OIDC 的新手。我目前正在尝试将名为 CILogon () 的 OIDC 工具集成到我正在构建的网站中。基本上,它可以通过 http://localhost:3000/auth 将用户重定向到第三方登录页面,登录成功后,它将用户重定向到 http://localhost:3000/home。

现在我想通过回调获取登录用户信息,但是回调函数好像没有被调用(例如我不能console.log profile对象:

passport.use(
  new OIDCStrategy(
    oidcConfig,
    (issuer, sub, profile, accessToken, refreshToken, done) => {
      // This callback will be called after successful authentication

      console.log("profile: ", profile); // does not print

      return done(null, profile);
    }
  )
);

下面是完整的代码。

const express = require("express");
const app = express();
const cors = require("cors");

// auth
const config = require("./auth/config"); // saved on server only
const session = require("express-session");
const crypto = require("crypto");
const passport = require("passport");
const OIDCStrategy = require("passport-openidconnect").Strategy;

// Body parser to parse incoming and outgoing requests
app.use(express.json());
app.use(cors());
app.listen(3000, () => console.log("server is up and running"));

// OIDC auth
const oidcConfig = {
  issuer: "",
  clientID: "xx123",
  clientSecret: "XXABC123",
  callbackURL: "http://localhost:3000/home",
  authorizationURL: "",
  tokenURL: " ",
  userInfoURL: " ",
};

// Generate a random session secret key
const secretKey = crypto.randomBytes(64).toString("hex");
app.use(
  session({
    secret: secretKey,
    resave: false,
    saveUninitialized: false,
  })
);

app.use(passport.initialize()); // Initialize Passport middleware
app.use(passport.session()); // Enable session support for Passport

passport.use(
  new OIDCStrategy(
    oidcConfig,
    (issuer, sub, profile, accessToken, refreshToken, done) => {

      console.log("profile: ", profile); // do not print

      console.log("here...");
      return done(null, profile);
    }
  )
);

app.get("/auth", passport.authenticate("openidconnect", { scope: "profile" })); // Initiate authentication

app.get(
  "/auth/callback",
  passport.authenticate("openidconnect", {
    successRedirect: "/home", // Redirect URL after successful authentication
    failureRedirect: "/login", // Redirect URL after failed authentication
  })
); // Callback URL for handling the OIDC provider's response

app.get("/home", async (req, res) => {
  console.log("home");
  const html = `<h1>Welcome to the home page!</h1>`; // Example HTML
  res.send(html);
});

app.get("/profile", (req, res) => {
  // Access the authenticated user's information from 'req.user'
  // Render the user's profile page
});

app.get("/logout", (req, res) => {
  // Log the user out and redirect to a logout page
});

回答如下:

找到了答案——看来我需要将“/auth/callback”更改为“/home”,因为我将回调 URL 注册为“/home”。

更多推荐

ExpressJS 护照 OIDC 回调未被调用

本文发布于:2024-05-30 10:17:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:护照   回调   未被   ExpressJS   OIDC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!