关于 spacetimedb-auth
spacetimedb-auth 是用于在 SpacetimeDB 模块中处理身份、声明、访问控制和授权策略的技能,涵盖连接拒绝、声明验证、reducer 检查及实验性 RLS。
主要功能
- 01身份与 JWT 声明指引
- 02拒绝未认证连接
- 03验证签发者与受众
- 04reducer 授权检查
- 05实验性 RLS 指引
- 06认证参考索引
适用场景
- 01设计经过认证的 SpacetimeDB 模块
- 02授权前验证 JWT 声明
- 03拒绝未认证客户端
- 04应用行级可见性规则
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ danmossa/ spacetimedb-skills --skill spacetimedb-auth安装后在 Claude Code 中输入/spacetimedb-auth确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/core-concepts-authentication-auth0.mdcore-concepts-authentication-clerk.mdcore-concepts-authentication-spacetimeauth-configuring-a-project.mdcore-concepts-authentication-spacetimeauth-creating-a-project.mdcore-concepts-authentication-spacetimeauth-react-integration.mdcore-concepts-authentication-spacetimeauth-testing.mdcore-concepts-authentication-spacetimeauth.mdcore-concepts-authentication-usage.mdcore-concepts-authentication.mdhow-to-reject-client-connections.mdhow-to-rls.md详细介绍
将此技能用于身份、声明、访问控制和授权策略。
核心身份验证模式
- SpacetimeDB 从 JWT 的
sub和iss声明中派生Identity。使用身份实现稳定的所有权;使用 JWT 声明进行提供方、受众、角色和策略决策。 - 已验证身份的模块应在
client_connected/clientConnected中拒绝未经身份验证的客户端,防止其订阅或调用 reducers。 - 在信任 subject 或自定义声明之前,务必验证签发者和受众。声明名称和预期值因部署而异。
- 将
senderAuth/SenderAuth/sender_auth()视为 JWT 详情的来源,而非仅依赖客户端提供的身份。 - 在 reducers 中使用角色或自定义声明辅助函数进行授权检查;对于预期的拒绝,向发送方返回错误。
- RLS 仍处于实验阶段。其规则是 SQL 筛选条件,使用
:sender表示调用方身份,返回单个表中的完整行;同一表的多条规则按 OR 逻辑组合。模块所有者会绕过 RLS。
TypeScript 连接拒绝模式:
import { SenderError } from "spacetimedb/server";
const OIDC_CLIENT_IDS = ["client_..."];
export const onConnect = spacetimedb.clientConnected((ctx) => {
const jwt = ctx.senderAuth.jwt;
if (jwt == null) throw new SenderError("Unauthorized: JWT is required");
if (jwt.issuer !== "https://auth.spacetimedb.com/oidc") {
throw new SenderError("Unauthorized: invalid issuer");
}
if (!jwt.audience.some((aud) => OIDC_CLIENT_IDS.includes(aud))) {
throw new SenderError("Unauthorized: invalid audience");
}
});
参考索引
| 需求 | 打开 |
|---|---|
| 身份验证概览 | references/core-concepts-authentication.md |
| SpacetimeAuth | references/core-concepts-authentication-spacetimeauth*.md |
| 第三方提供方 | references/core-concepts-authentication-auth0.md、references/core-concepts-authentication-clerk.md |
| 模块代码中的声明 | references/core-concepts-authentication-usage.md |
| 行级安全与连接拒绝 | references/how-to-rls.md、references/how-to-reject-client-connections.md |
指引
- 将身份验证中的身份检查与表可见性和行级授权选择分开处理。
- 声明名称、签发者和受众因部署而异;除非用户的项目中已有示例值,否则不要硬编码示例值。
- 如果任务涉及端点级的 HTTP 令牌机制,请转至 spacetimedb-http。
评论
支持 Markdown