跳到主内容

Joplin Server

Joplin 是一款开源笔记应用,Joplin Server 是其自托管同步服务器。从版本 3.4.1 开始,Joplin Server 可以通过 SAML 2.0 身份提供者 (IdP) 登录用户。以 Casdoor 作为 IdP,用户使用其 Casdoor 账户登录 Joplin Server 以及与之同步的 Joplin 应用。

Joplin Server 使用 samlify 和 xml-crypto 验证 SAML 响应。xml-crypto 不支持 XML canonicalization 1.1(Casdoor 默认使用该标准进行签名),因此 Casdoor 中的应用必须开启 Enable SAML C14N10。请使用最新的 Casdoor 版本:旧版本对这类响应的签名方式会导致 Joplin Server 拒绝,报错 FAILED_TO_VERIFY_SIGNATURE 或出现 XML schema 错误。

示例中使用 https://joplin.example.com 表示 Joplin Server,使用 https://door.example.com 表示 Casdoor。

在 Casdoor 中创建应用​

  1. 在 Casdoor 管理控制台中,打开用户所属的组织并添加一个应用,或打开一个现有应用。

  2. 将 Joplin Server 的实体 ID 及其断言消费者服务 (ACS) URL 添加到 Redirect URLs。实体 ID 是你将在下文编写的服务提供商 (SP) 文件中的 entityID(示例中为 Joplin)。ACS URL 是 <APP_BASE_URL>/api/saml:

    Joplin
    https://joplin.example.com/api/saml
  3. 在 SAML 选项卡中:

    • 开启 Enable SAML C14N10,并将 SAML C14N10 prefix 留空。

    • 开启 Enable SAML POST binding。Joplin Server 使用 HTTP-POST 绑定向 Casdoor 发送请求,因此元数据必须列出 Casdoor 的 HTTP-POST 端点。

    • 在 SAML attributes 中添加两行。Joplin Server 仅读取这两个属性,且名称区分大小写:Casdoor 默认发送的 Email 和 DisplayName 属性不计入在内。

      NameName formatValue
      emailBasic$user.email
      displayNameBasic$user.displayName

      每个用户在 Casdoor 中都需要有一个电子邮件地址。Joplin Server 通过该地址识别用户。

  4. 保存,然后点击 Copy SAML metadata URL。当 Enable SAML POST binding 开启时,URL 以 enablePostBinding=true 结尾:

    https://door.example.com/api/saml/metadata?application=admin/<application-name>&enablePostBinding=true
  5. 打开该 URL 并将 XML 保存为 idp.xml。Joplin Server 从文件而非 URL 读取 IdP 元数据。

准备 Joplin Server​

  1. 编写 SP 文件 sp.xml。将 entityID 设置为你添加到 Redirect URLs 的实体 ID,将 Location 设置为 ACS URL:

    <?xml version="1.0"?>
    <md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="Joplin">
    <md:SPSSODescriptor AuthnRequestsSigned="false" WantAssertionsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
    <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</md:NameIDFormat>
    <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://joplin.example.com/api/saml" index="1" />
    </md:SPSSODescriptor>
    </md:EntityDescriptor>
  2. 将 idp.xml 和 sp.xml 放置在 Joplin Server 可以读取的位置。如果使用 Docker,请挂载一个目录,例如在 docker-compose.yml 中设置 ./saml:/saml:ro。

  3. 设置 Joplin Server 的环境变量,例如其 .env 文件中:

    APP_BASE_URL=https://joplin.example.com
    API_BASE_URL=https://joplin.example.com
    SAML_ENABLED=true
    SAML_IDP_CONFIG_FILE=/saml/idp.xml
    SAML_SP_CONFIG_FILE=/saml/sp.xml
    SAML_ORGANIZATION_DISPLAY_NAME=Casdoor
    DELETE_EXPIRED_SESSIONS_SCHEDULE=
    VariableDescription
    APP_BASE_URL, API_BASE_URLSAML needs both set to the same URL.
    SAML_ENABLEDTurns on SAML sign-in.
    SAML_IDP_CONFIG_FILEThe path of the IdP metadata from Casdoor.
    SAML_SP_CONFIG_FILEThe path of the SP file.
    SAML_ORGANIZATION_DISPLAY_NAMEOptional. The name that the sign-in page shows above the SAML button.
    DELETE_EXPIRED_SESSIONS_SCHEDULEJoplin Server 默认每 6 小时清理一次会话,之后 SAML 用户需要手动重新登录。设置为空值可禁用此功能。

    若要仅允许 SAML 登录,还需设置 LOCAL_AUTH_ENABLED=false。

  4. 重启 Joplin Server。

验证结果​

  1. 在隐私浏览器窗口中打开 https://joplin.example.com/login,然后点击 Login using your organisation account。
  2. 登录 Casdoor。Joplin Server 将打开其主页并显示已登录的 Casdoor 用户。如果该用户在 Joplin Server 中尚不存在,则在首次登录时使用来自 Casdoor 的电子邮件和显示名称创建新用户。
  3. 在 Joplin 应用中,选择 Joplin Server (SAML) 作为同步目标,并输入 https://joplin.example.com。应用将在浏览器中打开登录页面。登录 Casdoor 后,Joplin Server 会显示一个代码,你在应用中输入此代码即可开始同步。

故障排除​

SymptomCause and fix
Joplin Server logs FAILED_TO_VERIFY_SIGNATURE: POST /api/samlUpdate Casdoor to the latest version. If it persists, download the metadata again: Joplin Server must have the certificate of the application's current Cert.
Joplin Server logs canonicalization algorithm 'http://www.w3.org/2006/12/xml-c14n11' is not supportedTurn on Enable SAML C14N10 on the application.
Joplin Server logs ERR_EXCEPTION_VALIDATE_XML, about xs:string without a namespace declarationUpdate Casdoor to the latest version. Older versions drop the xs namespace from the response when they sign it.
email must be a string or displayName must be a stringAdd the email and displayName rows to SAML attributes, with these exact names, and check that Disable SAML attributes is off.
Could not login using email "..."Joplin Server already has a local account with a password for this email, and doesn't let SAML sign in to it. Delete or rename the local account in Joplin Server.
The browser opens a Joplin Server URL that ends with [object Object]The IdP metadata has no HTTP-POST endpoint. Turn on Enable SAML POST binding, and download the metadata again with enablePostBinding=true.
Casdoor shows Issuer URI: Joplin doesn't exist in the allowed Redirect URI listAdd the entityID of sp.xml to Redirect URLs.
Casdoor shows AssertionConsumerServiceURL: ... doesn't match the SAML reply URL or the allowed Redirect URI listAdd <APP_BASE_URL>/api/saml to Redirect URLs.

另请参阅​