理解 Downcity City

token 模型

admin_secret_key 和 user_token 如何配合。

Downcity 使用 admin_secret_key 管理 Federation,并使用 user_token 让终端 client 调用 service。

可信后端你的后端使用 Admin City,并持有 admin_secret_key创建 city / 申请 user_token
Downcity校验 admin_secret_key,签发 user_token,并在内部保存 provider key。
终端 client前端、扩展或 App 只拿 user_token + city_id,使用 User City 调 service。拿不到 admin_secret_key

user_token

user_token 是给终端用户或产品 client 使用的调用凭证。它由 Admin City 基于 city_id + user_id 向 Federation 申请。

  • city_id
  • user_id
  • metadata
  • ttl

client 只携带 user_tokencity_id,不接触 provider key,也不接触 admin_secret_key

admin_secret_key

DOWNCITY_FEDERATION_ADMIN_SECRET_KEY 用于管理 Federation:创建 city、管理 env、为某个 city 下的用户签发 user_token。Federation 首次启动时会自动生成并写入 Federation env。

const client = new City({
  role: "admin",
  federation_url: "https://base.example.com",
  admin_secret_key: process.env.DOWNCITY_FEDERATION_ADMIN_SECRET_KEY,
});

const city = await client.cities.create({
  name: "Chrome Extension",
});

多产品复用

多个 client 可以归属同一个 city。签发 token 时明确写入 city_id

const user = await client.cities.tokens.apply({
  city_id: city.city_id,
  user_id: "user_123",
  metadata: {
    plan: "pro",
  },
  ttl: "7d",
});

console.log(user.user_token);