mcpField
S
spacetimedb-auth

spacetimedb-auth

github.com/danmossa安全2164 次安装19 周前更新
代码仓库

关于 spacetimedb-auth

spacetimedb-auth 是用于在 SpacetimeDB 模块中处理身份、声明、访问控制和授权策略的技能,涵盖连接拒绝、声明验证、reducer 检查及实验性 RLS。

主要功能

  1. 01身份与 JWT 声明指引
  2. 02拒绝未认证连接
  3. 03验证签发者与受众
  4. 04reducer 授权检查
  5. 05实验性 RLS 指引
  6. 06认证参考索引

适用场景

  1. 01设计经过认证的 SpacetimeDB 模块
  2. 02授权前验证 JWT 声明
  3. 03拒绝未认证客户端
  4. 04应用行级可见性规则

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/danmossa/spacetimedb-skills --skill spacetimedb-auth

安装后在 Claude Code 中输入/spacetimedb-auth确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
core-concepts-authentication-auth0.md
core-concepts-authentication-clerk.md
core-concepts-authentication-spacetimeauth-configuring-a-project.md
core-concepts-authentication-spacetimeauth-creating-a-project.md
core-concepts-authentication-spacetimeauth-react-integration.md
core-concepts-authentication-spacetimeauth-testing.md
core-concepts-authentication-spacetimeauth.md
core-concepts-authentication-usage.md
core-concepts-authentication.md
how-to-reject-client-connections.md
how-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
SpacetimeAuthreferences/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