如何使用 @node-saml/node-saml 制作 SAML SP?

问题描述 投票:0回答:1

我正在开发 SAML SP。 我使用 @node-saml/node-saml 库。 虽然我制作了以下源,但 SAML 身份验证不起作用。 你能告诉我如何修复或制作 SAML SP 的良好参考源吗?

vc.ts

export const SAML = async () => {
  const express = require("express");
  const bodyParser = require("body-parser");
  const { SAML } = require("@node-saml/node-saml");

  const app = express();
  const port = 3001;

  app.use(bodyParser.urlencoded({ extended: true }));

  const spOptions = {
    issuer: "saml_XXX",
    cert: "MII...XXX",
    assert_endpoint: "https://5XXX",
    audience: "https://5XXX",
  };

  const sp = new SAML(spOptions);

  // SAMLrequest
  app.get("/login", (req: any, res: { redirect: (arg0: any) => void }) => {
    const request = sp.createLoginRequest();
    res.redirect(request);
  });

  // SAML Assertion
  app.post(
    "/assert",
    (
      req: { body: { SAMLResponse: any } },
      res: {
        status: (arg0: number) => {
          (): any;
          new (): any;
          send: { (arg0: any): any; new (): any };
        };
        redirect: (arg0: string) => any;
        send: (arg0: string) => void;
      }
    ) => {
      const response = req.body.SAMLResponse;
      sp.parseLoginResponse(
        response,
        (err: any, profile: any, loggedOut: any) => {
          if (err) {
            console.log("err:", err);
            return res.status(500).send(err);
          }

          console.log(profile);

          if (loggedOut) {
            return res.redirect("/");
          }

          res.send("Login successful!");
          return "OK!";
        }
      );
    }
  );

  return app;
};

saml-request.ts

import { error } from "console";
import { SAML } from "../../../lib/vc";

const createSamlRequest = async (req: any, res: any) => {
  const app = await SAML();
  app(req, res);
  try {
    console.log("success");
  } catch {
    console.log("failed", error);
  }
};

export default createSamlRequest;

抱歉我的英语和技能很差。 谢谢你的好意。

我可能有 spOptions 内容...如果您知道,请告诉我如何在 Azure Entra ID 中设置 SAML 应用程序。 (我已经阅读了下面的文档。但是我无法为断言消费者服务 URL 和实体 Id 做出正确的设置。现在我设置了单个应用程序基本 url,该 url 适用于断言消费者服务 URL 的此源。

文档 https://learn.microsoft.com/en-us/power-pages/security/authentication/saml2-settings-azure-ad

node.js typescript azure saml microsoft-entra-id
1个回答
0
投票

首先在 vc.ts 文件中,您要导出一个返回 Express 应用程序的函数。我建议直接导出应用程序实例而不是函数。

import express from "express";
import bodyParser from "body-parser";
import { SAML } from "@node-saml/node-saml";

const app = express();
const port = 3001;

app.use(bodyParser.urlencoded({ extended: true }));

const spOptions = {
  issuer: "saml_XXX",
  cert: "MII...XXX",
  assert_endpoint: "https://5XXX",
  audience: "https://5XXX",
};

const sp = new SAML(spOptions);

// SAMLrequest
app.get("/login", (req, res) => {
  const request = sp.createLoginRequest();
  res.redirect(request);
});

// SAML Assertion
app.post("/assert", (req, res) => {
  const response = req.body.SAMLResponse;
  sp.parseLoginResponse(
    response,
    (err, profile, loggedOut) => {
      if (err) {
        console.log("err:", err);
        return res.status(500).send(err);
      }

      console.log(profile);

      if (loggedOut) {
        return res.redirect("/");
      }

      res.send("Login successful!");
    }
  );
});

export default app;

并在您的 saml-request.ts 文件中,使用应用程序实例而不是直接使用导出的函数。

import { error } from "console";
import app from "../../../lib/vc";

const createSamlRequest = async (req: any, res: any) => {
  app(req, res);
  try {
    console.log("success");
  } catch (err) {
    console.log("failed", err);
  }
};

export default createSamlRequest;

© www.soinside.com 2019 - 2024. All rights reserved.