获取密钥
登录账户后创建密钥,密钥只会显示一次。随后通过环境变量或密钥存储将它提供给程序。程序只读取密钥,不能签发密钥。
如果程序可以签发密钥,泄露密钥的持有者就能任意创建新密钥,而这些密钥可能在原密钥撤销后继续有效。签发凭据应由用户本人操作,不能交给凭据自身。
SDK 可以查看已有密钥并撤销不应继续使用的密钥。这足以满足运行中程序审计和切断访问权限的合理需求。
const { keys } = await api.keys.list();
// [{ id, name, prefix, scopes, lastUsedAt, expiresAt, isActive, createdAt }]
// Which key is this process running on? Match on the prefix you deployed with it —
// never on the key itself, which is not in the list and should not be compared.
const mine = keys.find((k) => k.prefix === process.env.IAGENIFY_KEY_PREFIX);
// And stop one, by the id that only list() carries.
await api.keys.revoke(someKey.id);import os
keys = api.keys.list()["keys"]
# [{"id", "name", "prefix", "scopes", "lastUsedAt", "expiresAt", "isActive", "createdAt"}]
# Which key is this process running on? Match on the prefix you deployed with it —
# never on the key itself, which is not in the list and should not be compared.
mine = next((k for k in keys if k["prefix"] == os.environ["IAGENIFY_KEY_PREFIX"]), None)
# And stop one, by the id that only list() carries.
api.keys.revoke(some_key["id"])丢失的密钥只能替换,不能找回。撤销立即生效:下一次请求时,其哈希值就不再被接受。
SDK 如何使用密钥
首次调用时,密钥通过 POST 发送到 /api/auth/sessions/exchange,同时发送运行环境、框架、设备 ID 和检测到的资源。返回的 JWT 和刷新令牌会被缓存,供后续调用使用。
// What the exchange sends
{ apiKey, storageId?, storageType: "filesystem" | "localStorage" | "memory",
framework: "node" | "next.js" | "gatsby" | "vite" | "browser",
appName, machineId, environment: { os, arch, hostname, runtime }, resources }
// What every later request carries
Authorization: Bearer <jwt>
X-IaGenify-Machine-Id: <sha256 of hostname|os|arch|runtime>
X-IaGenify-Resources: {"cpu":{...},"memory":{...},"disk":[...]}
X-IaGenify-Version: 0.2.2# What the exchange sends
{"apiKey": ..., "storageId": ..., "storageType": "filesystem",
"framework": "python", "appName": ..., "machineId": ...,
"environment": {"os": ..., "arch": ..., "hostname": ..., "runtime": ...}, "resources": ...}
# What every later request carries
# Authorization: Bearer <jwt>
# X-IaGenify-Machine-Id: <sha256 of hostname|os|arch|runtime>
# X-IaGenify-Resources: {"cpu":{...},"memory":{...},"disk":[...]}JWT 过期后,由刷新令牌进行更新。刷新失败时会再次用密钥交换会话。收到 401 时,SDK 仅丢弃 JWT 并重试一次,避免签名变更中断长期运行的进程。
去重依据是 apiKey 与 appName 的组合,并且跨客户端实例生效。React StrictMode 的双重渲染只会触发一次交换;两次交换会创建两个会话。
更换密钥
缓存的会话包含创建它的密钥指纹。向 SDK 传入另一把密钥后,不会复用原来的缓存会话。
更换密钥后,下一次调用立即使用新的身份,中间不会使用旧会话。无需手动清理任何内容。
一台设备使用多把密钥
会话文件由路径区分。请为每个账户使用独立路径,否则它们会相互覆盖。
const tenantA = new IaGenify({
apiKey: keyA,
sessionPath: "/var/lib/app/sessions/tenant-a",
appName: "billing-worker",
});
const tenantB = new IaGenify({
apiKey: keyB,
sessionPath: "/var/lib/app/sessions/tenant-b",
appName: "billing-worker",
});tenant_a = IaGenify(
api_key=key_a,
session_path="/var/lib/app/sessions/tenant-a",
app_name="billing-worker",
)
tenant_b = IaGenify(
api_key=key_b,
session_path="/var/lib/app/sessions/tenant-b",
app_name="billing-worker",
)不通过 SDK 调用 API
API 也接受原始密钥,但必须通过 Authorization 标头传递。通过 Cookie 和 ?api_key= 查询参数传递密钥的方式已被移除,因为 URL 中的密钥会出现在日志、来源标头和浏览器历史记录中。
curl https://api.iagenify.com/api/auth/me \
-H "Authorization: ApiKey ia_your_key_here"密钥存放位置
- 保存在服务端环境变量中。
- 绝不要放在客户端代码中。浏览器代码包里的密钥就是公开的密钥。
- 浏览器应用应在自己的服务器上调用 SDK,再把结果传给浏览器。
- 在 Node 中,会话保存在
~/.iagenify/session,所在目录的创建权限为0700。 - 在 CI 中,为每条流水线使用独立且有名称的密钥,避免撤销一把密钥影响其他流水线。