将你的 DiscourseConnect(DiscourseSSO)设置为 SimpleSAMLphp,使用你的 Discourse 论坛作为 SAML 身份提供者(IDP)

Discourse 集成了 DiscourseConnect,允许您将 Discourse 变成 SSO 提供商。您可以在此文章 “将 Discourse 用作身份提供商(SSO、DiscourseConnect)” 中找到它。但是,它提供的不是标准的 SAML 或 Oauth 协议。

因此,我们需要使用 Discourse 论坛上 Paul B. 创建的 模块。这个模块允许我们将 DiscourseConnect 连接到 SimpleSAMLphp,然后使用 SimpleSAMLphp 提供标准的 SAML 登录服务协议。在此,我要为他的帮助表示感谢,这使得这一切成为可能。您也可以在这里找到该模块:

现在开始指南。

首先,我们需要配置 SimpleSAMLphp。

首先下载 SimpleSAMLphp。

然后解压。

然后将文件移动到 /var/simplesamlphp。

然后将此文件夹的所有权和权限设置为 www-data 用户和 755。

sudo chmod -R 755 /var/simplesamlphp/

您可能会问为什么路径必须是 /var/simplesamlphp,因为官方文档放在这里,如果您将其放在其他位置,则需要额外的配置。

然后配置 nginx 并将其索引指向其文件夹中的 /www。如果您使用 apache,请在官方文档中查找配置。

upstream saml-php-handler {
    server unix:/run/php/php8.0-fpm.sock;
}

server {
        listen 443 ssl;
        server_name YOUR_SAML_DOMAIN;
        index index.php;
        ssl_certificate    YOUR_CERT;
        ssl_certificate_key    YOUR_KEY;
        ssl_protocols          TLSv1.3 TLSv1.2;
        ssl_ciphers            EECDH+AESGCM:EDH+AESGCM;
        location ^~ /simplesaml {
            alias /var/simplesamlphp/www;
            location ~ ^(?<prefix>/simplesaml)(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
                include          fastcgi_params;
                fastcgi_pass     saml-php-handler;
                fastcgi_param SCRIPT_FILENAME $document_root$phpfile;
                # Must be prepended with the baseurlpath
                fastcgi_param SCRIPT_NAME /simplesaml$phpfile;
                fastcgi_param PATH_INFO $pathinfo if_not_empty;
            }
        }
}

然后更新您的源:

然后安装软件包:

注意:这里要求您根据您的 php 版本进行安装。例如,如果您使用的是 php8.0,则需要在所有“php”后面添加“8.0”。例如:php8.0-xml

安装完成后,重启 Nginx 以激活新的 php 扩展:

然后生成一个 salt:

复制此 salt。

然后打开 /var/simplesamlphp/config/config.php

'auth.adminpassword',YOUR_PASSWORD;
'secretsalt',YOUR_SALT;
'technicalcontact_name',YOUR_NAME;
'technicalcontact_email' ,YOUR_EMAIL;
'language.default',CHANGE_TO_YOUR_LANGUAGE;
'timezone',CHANGE_TO_YOUR_AREA;
'enable.saml20-idp' => true,

然后打开 https://YOUR_SAML_DOMAIN/simplesaml,理论上您的 SimpleSAMLphp 现在应该可以工作了。

然后安装 Composer。

然后您应该输入 composer 来运行。

转到 /var/simplesamlphp,编辑 composer.json,将版本更改为 dev-master。

之所以在这里这样做,是因为 discourse-simplesamlphp 模块需要“dev-master”版本,而其默认版本是数字,所以我们需要将版本更改为“dev-master”。

然后开始安装此模块。

参数“–ignore-platform-reqs”是为了防止您的 php 版本不符合要求。

现在您应该看到 composer 开始安装 swcc/simplesamlphp-module-authdiscourse。

安装完成后,输入:

以启用此插件。



然后在您的 Discourse 论坛中启用“discourse connect provider secrets”和“enable discourse connect provider”这两个选项。

在“discourse connect provider secrets”中输入您的 SAML 域名和一个 sso 密钥。

然后编辑 simplesamlphp 文件夹中的 config/authsources.php:

<?php
$config = [
    // This is a authentication source which handles admin authentication.
    'admin' => [
        'core:AdminPassword',
    ],

    // This is the authentication source using the Discourse authentication.
    'discourse-sso' => [
      'authdiscourse:Discourse',
      'url' => 'https://discourse.your-domain.org',
      'secret' => '<your-sso-secret>',
    ],

];

您需要将这部分添加到合适的位置:

    'discourse-sso' => [
      'authdiscourse:Discourse',
      'url' => 'YOUR_DISCOURSE_DOMAIN',
      'secret' => 'DISCOURSE_SSO_SECRET',
    ],

以允许 SimpleSAMLphp 使用 discourse-sso 进行身份验证。

最后,打开 https://YOUR_SAML_DOMAIN/simplesaml/module.php/core/authenticate.php?as=discourse-sso

或者

打开 https://YOUR_SAML_DOMAIN/simplesaml/ 然后点击 authentication 然后点击 discourse-sso 进行测试。

如果返回了正确的信息,则表示您已成功配置 SimpleSAMLphp 连接到您的 Discourse。

现在您需要配置您的 IDP 设置。

首先,您需要生成一个 IDP 证书,使用:

如果您在根目录生成它,您可以在那里看到您的新证书。

然后将您的证书移动到您想要放置的位置,然后将其所有权和权限设置为 www-data 用户和 755。

chmod -R 755 /YOUR_CERT_PATH

最后,在 metadata/saml20-idp-hosted.php 中编辑您的证书路径和您的身份验证源。

<?php
$metadata['__DYNAMIC:1__'] = [
    /*
     * The hostname for this IdP. This makes it possible to run multiple
     * IdPs from the same configuration. '__DEFAULT__' means that this one
     * should be used by default.
     */
    'host' => '__DEFAULT__',

    /*
     * The private key and certificate to use when signing responses.
     * These are stored in the cert-directory.
     */
    'privatekey' => '/YOUR_KEY_PATH',
    'certificate' => '/YOUR_CERT_PATH',
    /*
     * The authentication source which should be used to authenticate the
     * user. This must match one of the entries in config/authsources.php.
     * NOTICE: YOU NEED TO CHANGE THE AUTH METHOD TO "discourse-sso"
     */
    'auth' => 'discourse-sso',
];

现在您的 SimpleSAMLphp 使用 Discourse 作为用户数据库后端,作为一个 SAML IDP,提供标准的 SAML 登录协议。

IDP ID(URI):https://YOUR_SAML_DOMAIN/simplesaml/saml2/idp/metadata.php

URL Target(用于发送 SP 身份要求):https://YOUR_SAML_DOMAIN/simplesaml/saml2/idp/SSOService.php

X509 Cert:https://YOUR_SAML_DOMAIN/simplesaml/module.php/saml/idp/certs.php/idp.crt

或者对于那些允许您直接上传 IDP 元数据文件的 SP,您可以下载 IDP 元数据:https://YOUR_SAML_DOMAIN/simplesaml/saml2/idp/metadata.php

全部完成。一切现在都应该可以工作了。

尽情享受吧~

接下来我可能会介绍如何将由 Discourse 驱动的 SimpleSAMLphp 作为 NextCloud 的 SAML 登录方式。

任何误解的地方都可以回复此帖子或通过 admin@rail.moe 联系我。

11 个赞

感谢您的指南!我确实对进行这次切换很感兴趣!我不太明白如何进行设置,但我有机会时会尝试一下。

嘿,最近有人尝试过这个吗?我正在运行 discourse 3.3 和 simpleSAMLphp 2.2.1,但无法使其正常工作。我现在处于测试阶段。链接不再有效,但我发现 simplesamls 管理面板中有一个测试选项。但是,我在这里遇到了这个异常:

SimpleSAML\\Error\\Error: UNHANDLEDEXCEPTION
Backtrace:
3 src/SimpleSAML/Error/ExceptionHandler.php:35 (SimpleSAML\\Error\\ExceptionHandler::customExceptionHandler)
2 vendor/symfony/error-handler/ErrorHandler.php:535 (Symfony\\Component\\ErrorHandler\\ErrorHandler::handleException)
1 vendor/symfony/error-handler/ErrorHandler.php:627 (Symfony\\Component\\ErrorHandler\\ErrorHandler::handleFatalError)
0 [builtin] (N/A)
Caused by: Symfony\\Component\\ErrorHandler\\Error\\FatalError: Compile Error: Declaration of SimpleSAML\\Module\\authdiscourse\\Auth\\Source\\Discourse::authenticate(\u0026$state) must be compatible with SimpleSAML\\Auth\\Source::authenticate(array \u0026$state): void
Backtrace:
0 modules/authdiscourse/lib/Auth/Source/Discourse.php:73 (N/A)

我多次检查了所有步骤,但该模块已经 4 年没有更新了,我担心它不再兼容。还有人还在使用这个吗?

那么 DiscourseConnect 使用什么协议?我曾尝试使用 oAuth2 客户端,但意识到参数并不完全相同 :frowning:

1 个赞