Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ApiKeyService

API Key 管理服务,用于创建和管理下游 API 访问密钥。API Key 可用于程序化访问 RustBill gRPC 接口,通过 ApiKeyAuthLayer 中间件认证,注入 DownstreamUser

RPC 列表

RPC描述权限
CreateApiKey创建新的 API Key(密钥仅返回一次)admin 或已授权 customer
ListApiKeys列出指定客户的 API Key 列表admin 或已授权 customer
RevokeApiKey吊销(禁用)API Keyadmin 或 API Key 所属客户

CreateApiKey

描述

创建新的 API Key。返回的 api_key 字段仅在创建时返回一次,之后无法再次获取,客户端应立即安全保存。

Request — CreateApiKeyRequest

字段类型编号必填描述
namestring1Key 名称(便于识别)
customer_idstring2关联客户 ID(admin 需指定,customer 用户自动设置)

Response — CreateApiKeyResponse

字段类型编号描述
api_keystring1完整 API Key(仅此一次返回,需立即保存)
key_prefixstring2Key 前缀(用于识别,如 "rk_live_a1b2c3"
idstring3API Key 记录 ID(UUID)

错误码

错误说明
InvalidArgument名称不能为空
NotFound指定客户不存在
PermissionDenied当前用户无权为此客户创建 Key(customer 需 can_create_api_keys=true

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"name":"生产环境 API Key","customer_id":"0192a123-..."}' \
  localhost:50051 rustbill.downstream.ApiKeyService/CreateApiKey
const resp = await api.createApiKey({
  name: "生产环境 API Key",
  customer_id: "0192a123-..."
});
// resp.api_key → "rk_live_a1b2c3d4e5f6..." (仅此一次)
// resp.key_prefix → "rk_live_a1b2c3"
// resp.id → "0192b456-..."
// 立即安全存储 api_key,之后无法再获取

ListApiKeys

描述

列出指定客户的所有 API Key(不包含完整密钥值,仅含前缀和元数据)。

Request — ListApiKeysRequest

字段类型编号必填描述
customer_idstring1客户 ID(UUID)

Response — ListApiKeysResponse

字段类型编号描述
keysrepeated ApiKeyInfo1API Key 信息列表

ApiKeyInfo

字段类型编号描述
idstring1Key 记录 ID(UUID)
key_prefixstring2Key 前缀(如 "rk_live_a1b2c3"
namestring3Key 名称
enabledbool4是否启用
created_atstring5创建时间(RFC3339)
last_used_atstring6最近使用时间(RFC3339)
expires_atstring7过期时间(RFC3339)

错误码

错误说明
PermissionDenied非管理员且无此客户的 API Key 管理权限

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"customer_id":"0192a123-..."}' \
  localhost:50051 rustbill.downstream.ApiKeyService/ListApiKeys
const resp = await api.listApiKeys({
  customer_id: "0192a123-..."
});
// resp.keys → [{ id, key_prefix, name, enabled, created_at, last_used_at, expires_at }]

RevokeApiKey

描述

吊销(禁用)指定的 API Key。吊销后该 Key 无法再用于认证。

Request — RevokeApiKeyRequest

字段类型编号必填描述
idstring1API Key 记录 ID(UUID)

Response — RevokeApiKeyResponse

无字段。

错误码

错误说明
NotFoundAPI Key 不存在
PermissionDenied非管理员且非该 Key 所属客户

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"id":"0192b456-...}' \
  localhost:50051 rustbill.downstream.ApiKeyService/RevokeApiKey
const resp = await api.revokeApiKey({
  id: "0192b456-..."
});

API Key 使用方式

API Key 在 HTTP 请求中作为 Bearer token 携带:

Authorization: Bearer rk_live_a1b2c3d4e5f6...

认证成功后,ApiKeyAuthLayer 中间件在 request extensions 中注入 DownstreamUser

DownstreamUser {
    api_key_id: Uuid,
    customer_id: Uuid,
    permissions: Vec<String>
}

服务端通过 require_downstream() 守卫访问下游用户信息,并根据 customer_id 自动限定数据范围。

权限对照

操作AdminCustomer(can_create_api_keys=true)Customer(can_create_api_keys=false)
CreateApiKey可创建任意客户仅创建自身客户禁止
ListApiKeys可查看任意客户仅查看自身客户禁止
RevokeApiKey可吊销任意 Key仅吊销自身 Key禁止

安全注意事项

  • api_key 完整值仅在 CreateApiKey 响应中返回一次,服务端不存储明文密钥(仅存哈希)
  • API Key 应存储在安全的环境中(如环境变量、密钥管理服务)
  • 建议定期轮换 API Key(创建新 Key + 吊销旧 Key)
  • 吊销操作将 enabled 设为 false,不物理删除记录