跳到主内容

概述

Casdoor基于OAuth构建,并使用令牌进行用户身份验证和授权。

访问令牌和ID令牌​

登录返回两个不同的 JSON Web Tokens (JWTs):

  • 访问令牌用于调用 API。其 tokenType 声明值为 access-token。
  • ID 令牌告知您的应用程序谁进行了登录。其 tokenType 声明值为 id-token。

两种令牌都包含相同的用户声明,采用相同的 Token format(令牌格式),并使用相同的密钥进行签名。它们在以下声明上有所不同:

ClaimAccess tokenID token
tokenTypeaccess-tokenid-token
jtiIts own IDIts own ID
audThe client ID, or the resource of RFC 8707, or the audience of a token exchangeAlways the client ID
at_hash, c_hashNot presentPresent in the cases that OpenID Connect defines

对于共享应用,两种令牌的 aud 均为 <client-id>-org-<organization>,即该组织下应用的客户端 ID。由于这两种令牌是独立的,因此不能互相替代使用,这符合 RFC 8725 第 3.12 节的要求。请将访问令牌发送给 API,仅使用 ID 令牌来读取用户的身份信息。

/api/login 端点在其响应的 data3 字段中返回 ID 令牌。

令牌字段​

Casdoor 将每个发出的令牌存储为一条记录,包含以下字段:

字段描述
owner, nameThe ID of the token record
createdTimeWhen Casdoor issued the token
applicationThe application that the token was issued to
organization, userThe user of the token
codeThe authorization code that the token was issued for
accessToken, refreshToken, idToken三种令牌。Casdoor 还会存储每个令牌的哈希值,分别位于 accessTokenHash、refreshTokenHash 和 idTokenHash 中
expiresInThe lifetime of the access token, in seconds. 0 means that Casdoor has revoked the token
scopeThe granted scope
tokenTypeBearer, or DPoP for a token bound with DPoP
grantTypeThe grant that issued the token, when it matters for refreshing
resourceThe resource or the token exchange audience that the token is bound to
sessionIdThe Casdoor browser session in which the user signed in, if any

令牌生命周期与失效​

当用户登录时,Casdoor 会颁发一个访问令牌和一个刷新令牌。访问令牌用于 API 身份验证;刷新令牌用于在无需重新身份验证的情况下获取新的访问令牌。

要撤销令牌,Casdoor 会将其 expiresIn 设置为 0。此时令牌内省端点将返回 "active": false,刷新端点会以 invalid_grant 和 refresh token is expired 拒绝刷新令牌请求。

当 Casdoor 撤销令牌时​

EventTokens that Casdoor revokes
The user signs out with SSO logout and logoutAll=trueAll tokens of the user
The user signs out of one session, or an administrator ends a session on the Sessions pageThe tokens that were issued in that session
The user's password is changed through /api/set-password, by the user, by an administrator, or with the forgot-password flowAll tokens of the user, in all applications. Applications must sign the user in again
A new sign-in to an application with Enable exclusive signin on and Max sessions at most 1The earlier tokens of the user for that application. See Exclusive sign-in
The user is forbidden to sign in, or deletedAll tokens of the user
RP-initiated logout with id_token_hintThe token of the hint

未通过浏览器会话签发的令牌(例如客户端凭证授予类型的令牌)不受会话结束的影响。由访问令牌创建的 Casdoor 会话会在该令牌被撤销或过期时结束。

自动清理过期令牌​

Casdoor 会自动删除已过期一段时间的令牌记录,以防止 token 表无限增长。当令牌的过期时间超过其所属组织的保留期限后,该令牌将被移除,保留期限从令牌自身的过期时间开始计算。

在组织的 Advanced(高级)选项卡中的 Token retention days(令牌保留天数)设置保留期限。留空或设为 0 表示默认值为 30 天。已不存在的组织的令牌使用 30 天的默认值。

清理任务的运行时机:

  • 启动时在后台执行一次,以免延迟服务器启动
  • 每天午夜按服务器时间执行

清理操作每批最多删除 1,000 个令牌,且仅影响已超过保留期限的已过期令牌。活动令牌绝不会受到影响。

令牌格式选项​

在颁发JWT时,可从四种格式中进行选择:

  • JWT
  • JWT-Empty
  • JWT-Custom
  • JWT-标准

令牌格式选项的行为如下:

  • JWT:在令牌有效载荷中包含所有用户字段
  • JWT-Empty:仅包含一组简短且固定的 User 字段:owner、name、id、displayName、avatar、email、email_verified 和 phone
  • JWT-自定义:包含您选择的自定义用户令牌字段(在令牌字段中选择属性)
  • JWT标准:以符合OIDC的格式包含标准OIDC声明(电子邮件、电话、性别、地址)

在应用的 OIDC/OAuth 选项卡中设置 Token format(令牌格式)。空值表示 JWT。新创建的应用会使用其组织的 Default token format(默认令牌格式)以及来自组织 Advanced(高级)选项卡的 Default token fields(默认令牌字段)作为其 Token fields(令牌字段)。更改这些默认值不会影响现有应用。客户端通过动态客户端注册创建的应用也会获得相同的默认值。

信息

**只有 JWT-Standard 会生成符合 OIDC 规范的 address 声明。**在 JWT、JWT-Empty 和 JWT-Custom 格式中,address 字段是原始的 []string 数组(用户的 Address 属性),不符合 OIDC 规范。如果你的应用把 address 声明当作对象解析,请使用 JWT-Standard 令牌格式。

群组格式​

应用程序 OIDC/OAuth 选项卡上的 Token group format 决定了 groups 声明如何在 /api/userinfo 的响应以及携带该声明的令牌格式中对用户的群组进行命名:JWT 始终包含,JWT-Custom 仅在 Token fields 中选中 Groups 时包含。JWT-Empty 和 JWT-Standard 令牌不包含 groups 声明:

OptionValue in groups
ID (org/group)<organization>/<group>. This is the default
Path (org/parent/group)从顶级群组开始的完整路径,例如 <organization>/<parent-group>/<group>
Name (group)<group>

Path (org/parent/group) 取代了 conf/app.conf 中已不再存在的 useGroupPathInToken 选项。

用 Token fields 限制字段​

应用上的 Token fields 设置(用于为 JWT-Custom 格式挑选自定义用户属性)同时也是 /userinfo 接口的白名单。

  • Token fields 为空时不启用白名单:令牌载荷和 /userinfo 响应都包含默认的全部字段。这是默认行为。
  • Token fields 列出了一个或多个字段时,只返回这些字段。不在列表中的用户属性,在 JWT-Custom 令牌载荷和 /userinfo 响应中都会被省略。

白名单按底层的用户属性名匹配。例如,要通过 /userinfo 返回显示名、邮箱和头像,把 Name、Email 和 Avatar 加到 Token fields。字段仍受申请的 OAuth scope 约束,例如只有授予了 email scope 才返回 Email,只有 address scope 才返回 Location(即 address 声明),只有 phone scope 才返回 Phone。无论白名单如何,sub 和 aud 声明总会返回。

注意事项

配置 Token fields 会限制 /userinfo,而不只是令牌。如果你之前依赖 /userinfo 返回所有字段,在这里添加条目后,该响应就会开始被过滤。要保持完整响应,就让 Token fields 留空。

OIDC 地址声明​

OIDC 规范 把 address 声明定义为包含以下字段的 JSON 对象:

FieldDescription
格式化完整邮寄地址,已格式化以便显示
街道地址街道地址组成部分(可能包含门牌号、街道名称、邮政信箱信箱等)
所在地城市或所在地
地区州、省、地级市或地区
邮政编码邮编或邮政编码
国家国家名称

Casdoor如何将用户数据映射到地址声明​

Casdoor在两个独立的用户字段中存储地址信息:

  • 位置(字符串):一个通用的位置字符串(例如,“纽约”). 请求 address scope 时,/api/userinfo 接口返回的纯字符串 address 值就取自这个字段。
  • 地址(字符串数组):地址行字符串数组(例如,["123 Main St", "Anytown, NY 12345", "USA"]). JWT-Standard 令牌中符合 OIDC 规范的 address 声明以这个字段为来源。

JWT-标准地址声明​

请求 address scope 并使用 JWT-Standard 格式时,Casdoor 会在令牌中返回如下对象:

{
"address": {
"street_address": "123 Main St\nAnytown, NY 12345\nUSA",
"formatted": "",
"locality": "",
"region": "",
"postal_code": "",
"country": ""
}
}

street_address 字段是用户 Address 数组中各项用换行符连接后的结果。其余的 OIDC 地址子字段(formatted、locality、region、postal_code、country)目前为空,因为 Casdoor 把整个地址以自由格式的多行文本存在 Address 数组中,而不是按组成部分分字段存储。

非标准格式(JWT、JWT-Empty、JWT-自定义)​

在 JWT、JWT-Empty 和 JWT-Custom 令牌格式中,address 字段是用户对象原始的 Address 属性,即一个字符串组成的 JSON 数组,而不是 OIDC 地址对象:

{
"address": ["123 Main St", "Anytown, NY 12345", "USA"]
}

按照 OIDC 规范期望 address 声明是 JSON 对象的 OIDC 客户端会解析失败。如果需要令牌中有符合规范的 address 声明,请切换到 JWT-Standard。

JWT-Custom

标准OIDC声明​

所有JWT令牌格式均在有效负载中包含这些标准OpenID Connect声明:

  • sub:主体标识(唯一的用户 ID)
  • email:用户的邮箱地址
  • email_verified - 布尔值,指示电子邮件地址是否经过验证:用户通过 Casdoor 的代码或魔法链接确认了邮箱,或者创建该用户的 OAuth 提供商为其提供了验证担保
  • name或preferred_username- 用户的显示名称
  • picture或avatar- 用户头像URL

有了 email_verified 声明,把 Casdoor 当作身份提供商的外部应用可以直接从令牌判断邮箱是否已验证,无需再调 API。

当用户通过 OAuth 提供商注册时,Casdoor 会从该提供商获取电子邮件地址的验证状态。例如,Google 会在其 email_verified 声明中报告此状态,而自定义 OAuth 提供商可以映射 emailVerified 字段。GitHub、GitLab、Gitee、Facebook、LinkedIn、DingTalk、Lark 和 Twitter 等提供商会将它们返回的所有电子邮件地址视为已验证。

JWT 格式还包含用户的 lastSigninTime 和 lastSigninIp 字段。在密码授予类型的令牌中,这些字段描述的是上一次登录信息,因为 Casdoor 是在签发令牌之后才记录本次登录的。当前登录的时间对应于 iat 声明。

自定义令牌属性​

使用JWT-自定义格式,定义自定义属性及其数据类型。每个属性都包含一个类型字段,用于控制值如何包含在JWT中:

  • 数组:即使属性值仅包含一个元素,也始终以数组形式返回。这可确保与期望角色、组或权限等字段采用数组类型的OIDC客户端兼容。
  • 字符串:属性值将作为单个字符串返回(如果存在多个值,则返回第一个元素)。

空属性会自动从标记中省略,以保持有效负载的简洁。在为角色、组或权限配置属性时,建议使用数组类型,以更好地符合OIDC规范,并与Rancher和Keycloak等系统兼容。