Mastodon
  • 什么是 Mastodon?
  • 使用 Mastodon
    • 注册帐户
    • 设置你的个人资料
    • 在你的账户发布内容
    • 使用网络功能
    • 处理不想看到的内容
    • 推广你和他人
    • 进行偏好设置
    • 更多设置
    • 在站点外使用 Mastodon
    • 迁移或离开账户
    • 运行你自己的站点
  • 运营 Mastodon
    • 准备你的服务器
    • 从源代码安装
    • 配置你的环境
    • 安装可选功能
      • 对象存储
      • 洋葱服务
      • 验证码
      • 单点登录
    • 配置全文搜索
    • 设置你的新实例
    • 使用管理 CLI
    • 升级到新版本
    • 备份你的服务器
    • 迁移到新机器
    • 扩大你的站点规模
    • 审核操作
    • 故障排除
      • 数据库索引损坏
    • 用户组
  • 开发 Mastodon 应用
    • API 入门指南
    • 使用公开数据
    • 获取客户端应用访问权限
    • 使用帐户登录
    • 库与实现
  • 向 Mastodon 项目做贡献
    • 技术概览
    • 设置开发环境
    • 代码结构
    • 路由
    • 漏洞赏金与责任披露
  • 遵循的标准
    • ActivityPub
    • WebFinger
    • 安全性
    • Microformats
    • OAuth
    • Bearcaps
  • REST API
    • Datetime 格式
    • 指南与最佳实践
    • OAuth 令牌
    • OAuth 作用域
    • 速率限制
  • API 方法
    • apps
      • oauth
      • emails
    • accounts
      • bookmarks
      • favourites
      • mutes
      • blocks
      • domain_blocks
      • filters
      • reports
      • follow_requests
      • endorsements
      • featured_tags
      • preferences
      • followed_tags
      • suggestions
      • tags
    • profile
    • statuses
      • media
      • polls
      • scheduled_statuses
    • timelines
      • conversations
      • lists
      • markers
      • streaming
    • grouped notifications
    • notifications
      • push
    • search
    • instance
      • trends
      • directory
      • custom_emojis
      • announcements
    • admin
      • accounts
      • canonical_email_blocks
      • dimensions
      • domain_allows
      • domain_blocks
      • email_domain_blocks
      • ip_blocks
      • measures
      • reports
      • retention
      • trends
    • proofs
    • oembed
  • API 实体
    • Account
    • AccountWarning
    • Admin::Account
    • Admin::CanonicalEmailBlock
    • Admin::Cohort
    • Admin::Dimension
    • Admin::DomainAllow
    • Admin::DomainBlock
    • Admin::EmailDomainBlock
    • Admin::Ip
    • Admin::IpBlock
    • Admin::Measure
    • Admin::Report
    • Announcement
    • Appeal
    • Application
    • Context
    • Conversation
    • CustomEmoji
    • DomainBlock
    • Error
    • ExtendedDescription
    • FamiliarFollowers
    • FeaturedTag
    • Filter
    • FilterKeyword
    • FilterResult
    • FilterStatus
    • IdentityProof
    • Instance
    • List
    • Marker
    • MediaAttachment
    • Notification
    • NotificationPolicy
    • NotificationRequest
    • Poll
    • Preferences
    • PreviewCard
    • PreviewCardAuthor
    • PrivacyPolicy
    • Reaction
    • Relationship
    • RelationshipSeveranceEvent
    • Report
    • Role
    • Rule
    • ScheduledStatus
    • Search
    • Status
    • StatusEdit
    • StatusSource
    • Suggestion
    • Tag
    • TermsOfService
    • Token
    • Translation
    • V1::Filter
    • V1::Instance
    • V1::NotificationPolicy
    • WebPushSubscription

WebFinger

将 `user@domain` mentions 转换为 Actor 账户 URI。

    • 什么是 WebFinger,为何使用它?
    • WebFinger 流程示例
    • Mastodon 对 WebFinger 的要求
    • 另请参阅

什么是 WebFinger,为何使用它?

在 Mastodon 中,账户可以托管在与你的账户相同的站点,也可以托管在一个完全不同的站点。同一个用户名可能会在不同的域名上使用。因此,一个完整的 Mastodon 用户 mentions 由用户名和域名两部分组成,格式为 @username@domain。实际上,@user@example.com 与 @user@example.org 是不同的。如果不包含域名,Mastodon 会尝试查找名为 @username 的本地用户。但是,为了通过 ActivityPub 将内容递送给某人,仅有 @username@domain mentions 是不够的 —— 必须首先将 mentions 转换为 HTTPS URI,这样才能找到远程 Actor 的收件箱和发件箱。

于是就有了 WebFinger。如 RFC 7033 所述,WebFinger 是一项规范,它定义了一种在仅知道特定服务器上的 URI 的情况下解析资源链接的方法。这使得任何人都能在无需事先知道资源确切地址的情况下查找资源的位置;例如,可以通过电子邮件或电话号码进行查找。此查找请求将被发往 /.well-known/webfinger 端点,并附带一个 resource 查询参数。Mastodon 使用的资源 URI 是 RFC 7565 中描述的 acct: URI,其包含了托管在特定域名上的账户的用户名。

由于 Mastodon 严重依赖 mentions 来定位其他账户,因此要与 Mastodon 完全互操作,WebFinger 是必需的。 用户通常可以通过搜索直接的 HTTPS URI(如果知道对应的 URI 的话)或 username@domain 地址来加载账户,但 Mastodon 的内部逻辑几乎完全依赖于 acct: URI 或 username@domain 表示形式。如果一个 ActivityPub 实现不支持 WebFinger,那么搜索任何对象或账户都会失败,因为其作者无法被转换为本地数据库中的用户。

WebFinger 流程示例

假设我们想要查找托管在 mastodon.social 网站上的用户 @Gargron。

只需向该域名的 /.well-known/webfinger 端点发起请求,并将 resource 查询参数设置为 acct: URI 即可。

{
  "subject": "acct:Gargron@mastodon.social",
  "aliases": [
    "https://mastodon.social/@Gargron",
    "https://mastodon.social/users/Gargron"
  ],
  "links": [
    {
      "rel": "http://webfinger.net/rel/profile-page",
      "type": "text/html",
      "href": "https://mastodon.social/@Gargron"
    },
    {
      "rel": "self",
      "type": "application/activity+json",
      "href": "https://mastodon.social/users/Gargron"
    },
    {
      "rel": "http://ostatus.org/schema/1.0/subscribe",
      "template": "https://mastodon.social/authorize_interaction?uri={uri}"
    }
  ]
}

https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social

您可以解析此 JSON 响应,以查找具有您所需类型的链接。对于 ActivityPub 的 id,我们特别关注查找 application/activity+json 类型。

这样,我们就将 @Gargron@mastodon.social 转换为了 https://mastodon.social/users/Gargron,现在我们可以在需要时将此 URI 作为 id,通过 ActivityPub 进行交互。

{
"id": "https://social.example/activities/1",
"type": "Create",
"actor": "https://social.example/actors/1",
"object": {
    "id": "https://social.example/objects/1",
    "type": "Note",
    "content": "Hello, Gargron!"
},
"to": "https://mastodon.social/users/Gargron"
}

示例活动

请注意,在上面的示例中,social.example 并未使用与 Mastodon 相同的 URI 结构。因此,我们无法仅根据用户名和域名猜测出行为体的 id。但是,如果 social.example 支持 WebFinger,那么我们可以通过请求 https://social.example/.well-known/webfinger?resource=acct:username@social.example 并解析响应中类型为 application/ld+json; profile="https://www.w3.org/ns/activitystreams" 或 application/activity+json 的链接来获取此 id。此链接还应具有 rel="self" 的链接关系。

Mastodon 对 WebFinger 的要求

处理格式为 username@domain 或 @username@domain 的账户时,Mastodon 将执行以下操作:

  • 使用该用户名和域名构造一个 acct: URI
  • 对该 resource 发起 WebFinger 请求

利用该 WebFinger 响应,Mastodon 将检查以下内容:

  • subject 存在
  • links 数组包含一个链接,其 rel 为 self 且 type 为 application/ld+json; profile="https://www.w3.org/ns/activitystreams" 或 application/activity+json
    • 此链接的 href 解析为一个 ActivityPub Actor

利用该 ActivityPub 行为体表示(行为体表示可能在没有初始 WebFinger 请求的情况下被直接提供),Mastodon 将执行以下操作:

  • 获取 preferredUsername 和行为体所在服务器的主机名
  • 使用该用户名和域名构造一个 acct: URI
  • 对该 resource 发起 WebFinger 请求

如果 subject 与 resource 匹配,则流程在此停止。否则,如果 subject 包含不同的规范账户 URI,则 Mastodon 将对该规范账户 URI 执行额外的 WebFinger 请求,以确保这个新的 resource 链接到同一个 ActivityPub Actor,并检查相同的条件。

换句话说,以下情况是有效的:

  • 向 example.com 请求资源 acct:alice@example.com,返回一个指向域名 example.com 上 Actor 的链接,其 preferredUsername 为 alice,并且 subject 与请求的 resource acct:alice@example.com 匹配。
  • 向 example.com 请求资源 acct:alice@example.com,返回一个指向域名 ap.example.com 上 Actor 的链接,其 preferredUsername 为 alice。
    • …然后,向 ap.example.com 请求资源 acct:alice@ap.example.com,返回的 subject 为 acct:alice@example.com,并链接到同一个行为体。

另请参阅

app/services/activitypub/fetch_remote_actor_service.rb
app/services/resolve_account_service.rb
app/lib/webfinger.rb

翻译状态: 本文是英文页面 WebFinger 的翻译,最后翻译时间:2025-04-21,点击这里可以查看翻译后页面的改动。

最后更新于 April 21, 2025 · 改进此页面
也可在此找到: English

赞助商

Dotcom-Monitor LoadView Stephen Tures Swayable SponsorMotion

加入Mastodon · 博客 ·

查看源代码 · CC BY-SA 4.0 · 版权信息