OpenID Connect 身份认证
OpenID Connect (OIDC) 可在 BookStack 中作为主要身份认证方式使用。 这将取代默认的电子邮件与密码身份认证机制。 BookStack 支持简单级别的自动发现,以简化端点和密钥管理。
启用后,BookStack 将尝试根据 BookStack 用户上存储的 "External Authentication ID" 值, 将 OIDC 用户与已有的 BookStack 用户进行匹配。 如果无法完成匹配,BookStack 将自动注册该用户, 以提供无缝的访问体验。该用户将获得应用设置中 "Default user role after registration" 选项所指定的默认角色。
视频指南
我们提供了一系列视频,可针对不同的身份平台演示设置 BookStack OpenID Connect 身份认证集成所需的具体步骤:
要求与限制
以下是关于 BookStack OIDC 实现需要留意的一些事项:
- 目前仅支持将 RS256 用作令牌签名算法,不支持令牌加密。此限制同时适用于 ID 令牌和 userinfo 响应。
- 发现机制涵盖从
<issuer>/.well-known/openid-configuration端点获取身份认证与令牌端点,以及解析 JWKS URI 上的任何密钥。- 不支持颁发者发现(Issuer discovery)。
- 支持 RP 发起注销(RP-initiated logout),前提是已启用;不支持其他注销机制。
BookStack 配置
要设置基于 OIDC 的身份认证,请在你的 .env 文件中添加或修改以下变量:
# Set OIDC to be the authentication method
AUTH_METHOD=oidc
# Control if BookStack automatically initiates login via your OIDC system
# if it's the only authentication method. Prevents the need for the
# user to click the "Login with x" button on the login page.
# Setting this to true enables auto-initiation.
AUTH_AUTO_INITIATE=false
# Set the display name to be shown on the login button.
# (Login with <name>)
OIDC_NAME=SSO
# Name of the claims(s) to use for the user's display name.
# Can have multiple attributes listed, separated with a '|' in which
# case those values will be joined with a space.
# Example: OIDC_DISPLAY_NAME_CLAIMS=given_name|family_name
OIDC_DISPLAY_NAME_CLAIMS=name
# OAuth Client ID to access the identity provider
OIDC_CLIENT_ID=abc123
# OAuth Client Secret to access the identity provider
OIDC_CLIENT_SECRET=def456
# Issuer URL
# Must start with 'https://'
OIDC_ISSUER=https://instance.authsystem.example.com
# The "end session" (RP-initiated logout) URL to call during BookStack logout.
# By default this is false which disables RP-initiated logout.
# Setting to "true" will enable logout if found as supported by auto-discovery.
# Otherwise, this can be set as a specific URL endpoint.
OIDC_END_SESSION_ENDPOINT=false
# Enable auto-discovery of endpoints and token keys.
# As per the standard, expects the service to serve a
# `<issuer>/.well-known/openid-configuration` endpoint.
OIDC_ISSUER_DISCOVER=true
############################################################
## NOTE: The below are only needed if not using the above ##
## auto-discovery option. ##
############################################################
# Path to identity provider token signing public RSA key
OIDC_PUBLIC_KEY=file:///keys/idp-public-key.pem
# Full URL to the OIDC authorize endpoint
OIDC_AUTH_ENDPOINT=https://instance.authsystem.example.com/v1/authorize
# Full URL to the OIDC token endpoint
OIDC_TOKEN_ENDPOINT=https://instance.authsystem.example.com/v1/token
# Full URL to the OIDC userinfo endpoint
# Won't be used if all required claims are provided in the ID token.
OIDC_USERINFO_ENDPOINT=https://instance.authsystem.example.com/v1/userinfo
BookStack 中的用户将通过 sub 声明与 OIDC 提供的账户关联。
如果该 ID 的值在身份提供商处发生变更,管理员可在 BookStack 中
通过更改用户资料页面上的 "External Authentication ID" 字段来更新该值。
身份认证系统配置
使用 OIDC 时,通常无需为 BookStack 专门配置太多内容。 多数情况下,你只需确保回调/重定向 URI 按如下方式设置:
- 登录回调/重定向 URI:
https://example.com/oidc/callback - 注销回调/重定向 URI:
https://example.comhttps://example.com/loginhttps://example.com/login?prevent_auto_init=true- 实际只会使用其中一个 URL,具体取决于所设置的配置。部分系统允许改用通配符,例如
https://example.com/*。
将 https://example.com 更改为你 BookStack 实例的基础 URL。
自 v24.02 起,BookStack 将在身份认证过程中使用 Proof Key for Code Exchange (PKCE)。 如果你的身份认证系统提供相关选项,应将 PKCE 设为必需,以提升安全性。
为已有用户切换到 OIDC
将 AUTH_METHOD 从 standard 切换为 oidc 时,BookStack 不会
将 OIDC 用户账户与电子邮件地址匹配的已有 BookStack 用户关联,
因为已有 BookStack 用户的 "External Authentication ID" 值
与 OIDC 系统提供的唯一用户 ID 并不匹配。
你可以在 AUTH_METHOD=standard 期间使用管理员账户登录 BookStack 来解决这一问题。
登录后,将 AUTH_METHOD 更改为 oidc。
更改身份认证方法后,在 BookStack 中查看用户账户时,
姓名和电子邮件输入框下方会显示 "External Authentication ID" 文本字段。
你可以在此处输入 OIDC 提供商给出的唯一用户 ID。
保存后,BookStack 在下次登录时会使用该值来匹配 OIDC 与 BookStack 用户账户。
如果需要批量更新账户,也可以直接更新 BookStack 数据库中
users 表的 external_auth_id 字段。
调试
为帮助你设置或配置 BookStack 以使用你的 OIDC 系统,
以下 .env 选项有助于获取更详细的信息:
# Dump out the details fetched from the identity provider.
# Only set this option to true if debugging since it will block logins
# and potentially show private details.
OIDC_DUMP_USER_DETAILS=false
此外,遇到的 BookStack 错误详情可参阅我们的 通用调试文档。
使用不同的 ID 声明
默认情况下,BookStack 将使用 sub 声明作为唯一标识符,
以在 BookStack 与身份提供商之间匹配用户。
对于绝大多数使用场景而言,这是合适的,因为该声明属于
OIDC 标准的一部分。
在某些特定场景下,你可能希望使用其他声明作为唯一标识符。
可在你的 .env 中设置 OIDC_EXTERNAL_ID_CLAIM 选项来实现,如下所示,
其中该选项的值即声明的名称:
# Configure a custom ID Token claim to be used as the
# "External Authentication ID" within BookStack.
OIDC_EXTERNAL_ID_CLAIM=upn
请注意,如果对已有的 BookStack OIDC 用户更改此设置, 但未同时更改其 "External Authentication ID" 值, 则可能导致后续登录出现问题,因为其在 BookStack 中存储的 ID 可能不再匹配。
组同步
BookStack 能够将 OIDC 用户组与 BookStack 角色进行同步。
默认情况下,它会将 OIDC 组名与 BookStack 角色显示名称进行匹配,且忽略大小写。
启用 OIDC 身份认证后,编辑角色时可以看到 'External Authentication IDs' 字段,
可通过该字段覆盖上述默认行为。
如果填写了该字段,将使用其中的名称,并忽略 BookStack 角色显示名称。
可以使用逗号分隔多个名称进行匹配。
如果需要使用字面逗号字符进行映射,可用反斜杠转义逗号 (\,)。
在将 OIDC 组与角色名称或 'External Authentication IDs' 值进行匹配时, BookStack 会将 OIDC 组名统一转换为小写,并将空格替换为连字符。 例如,要匹配名为 "United Kingdom" 的 OIDC 组, 可使用 "united-kingdom" 作为 'External Authentication IDs' 值。
此功能要求 OIDC 服务器在 ID 令牌中提供一个包含组名数组的声明。
你需要使用 OIDC_GROUPS_CLAIM 指定该属性,以告知 BookStack 从哪个声明中获取组。
该值可使用点号表示法访问 ID 令牌 JSON 数据中的嵌套属性,
示例见下方。
请注意,你可以使用 OIDC_DUMP_USER_DETAILS 选项(如上方
调试章节所示)导出身份认证系统提供的声明值,
以帮助理解身份认证系统实际提供的内容。
以下是启用组同步需要添加到你的 .env 文件中的设置:
# Enable OIDC group sync.
OIDC_USER_TO_GROUPS=true
# Set the attribute from which BookStack will read groups names from.
OIDC_GROUPS_CLAIM=groups
# Additional scopes to send with the authentication request.
# By default BookStack only sends the 'openid', 'profile' & 'email' scopes.
# Many platforms require specific scopes to be requested for group data.
# Multiple scopes can be added via comma separation.
OIDC_ADDITIONAL_SCOPES=groups
# Remove the user from roles that don't match OIDC groups upon login.
# Note: While this is enabled the "Default Registration Role", editable within the
# BookStack settings view, will be considered a matched role and assigned to the user.
OIDC_REMOVE_FROM_GROUPS=true
嵌套组声明示例
下面展示的是 ID 令牌 JSON 数据的简化示例,其中组数据位于嵌套属性内,
同时给出了针对该结构所使用的 OIDC_GROUPS_CLAIM 值,用于检测所提供的 "Editor" 和 "Admin" 角色。
OIDC_GROUPS_CLAIM=resource_access.bookstack.roles
{
...
"resource_access": {
"bookstack": {
"roles": [
"Editor",
"Admin"
]
}
},
"email": "email@example.com"
...
}
谢谢你的反馈!