feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

View File

@@ -0,0 +1,43 @@
{
"openapi": "3.0.3",
"info": {"title": "License Platform Client API", "version": "1.0.0", "description": "Client-facing endpoints. Admin session is not a client credential. See protocol.md for canonical signature and trust-root rules."},
"servers": [{"url": "https://license.example.com", "description": "Replace with deployed HTTPS origin"}],
"paths": {
"/api/v1/activation/instances/{instance_id}": {"post": {
"operationId": "activateInstance", "summary": "Activate pre-provisioned instance",
"parameters": [{"$ref": "#/components/parameters/InstanceID"}, {"$ref": "#/components/parameters/IdempotencyKey"}],
"requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["license_key", "secret"], "properties": {"license_key": {"type": "string"}, "secret": {"type": "string", "format": "password"}}}}}},
"responses": {"200": {"description": "Activation certificate; verify data.certificate before accepting", "content": {"application/json": {"schema": {"type": "object", "properties": {"data": {"type": "object", "properties": {"instance_id": {"type": "string"}, "status": {"type": "string", "enum": ["activated"]}, "certificate": {"$ref": "#/components/schemas/Certificate"}}}, "idempotent_replay": {"type": "boolean"}}}}}}, "default": {"$ref": "#/components/responses/Error"}}
}},
"/api/v1/instances/{instance_id}/heartbeat": {"post": {
"operationId": "heartbeat", "summary": "Refresh signed runtime lease",
"parameters": [{"$ref": "#/components/parameters/InstanceID"}, {"$ref": "#/components/parameters/IdempotencyKey"}],
"requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["secret", "client_time"], "properties": {"secret": {"type": "string", "format": "password"}, "client_time": {"type": "string", "format": "date-time"}}}}}},
"responses": {"200": {"description": "Signed lease; HTTP 200 does not imply active state", "content": {"application/json": {"schema": {"type": "object", "properties": {"data": {"$ref": "#/components/schemas/Lease"}}}}}}, "429": {"description": "Rate limited; respect Retry-After", "headers": {"Retry-After": {"schema": {"type": "string"}}}, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}, "default": {"$ref": "#/components/responses/Error"}}
}},
"/api/v1/customers/{customer_id}/releases/{id}/download": {"post": {
"operationId": "downloadRelease", "summary": "Download entitled release; repeat credentials for Range retries",
"parameters": [{"name": "customer_id", "in": "path", "required": true, "schema": {"type": "integer", "minimum": 1}}, {"name": "id", "in": "path", "required": true, "schema": {"type": "integer", "minimum": 1}}, {"name": "Range", "in": "header", "schema": {"type": "string"}}],
"requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["instance_id", "secret"], "properties": {"instance_id": {"type": "string"}, "secret": {"type": "string", "format": "password"}}}}}},
"responses": {"200": {"description": "Full file (through configured download gateway)", "content": {"application/octet-stream": {"schema": {"type": "string", "format": "binary"}}}}, "206": {"description": "Partial file", "headers": {"Content-Range": {"schema": {"type": "string"}}}, "content": {"application/octet-stream": {"schema": {"type": "string", "format": "binary"}}}}, "416": {"description": "Unsatisfiable Range"}, "default": {"$ref": "#/components/responses/Error"}}
}},
"/api/v1/activation/keyring": {"get": {
"operationId": "discoverKeyring", "summary": "Discovery only, NOT a trust-root bootstrap",
"responses": {"200": {"description": "Configured verification keys", "content": {"application/json": {"schema": {"type": "object", "properties": {"data": {"type": "object", "properties": {"current_key_id": {"type": "string"}, "keys": {"type": "array", "items": {"type": "object", "properties": {"key_id": {"type": "string"}, "public_key_pem": {"type": "string"}, "revoked": {"type": "boolean"}}}}}}}}}}}, "default": {"$ref": "#/components/responses/Error"}}
}}
},
"components": {
"parameters": {
"InstanceID": {"name": "instance_id", "in": "path", "required": true, "schema": {"type": "string"}},
"IdempotencyKey": {"name": "Idempotency-Key", "in": "header", "required": true, "schema": {"type": "string"}, "description": "Same key and unchanged body for retries of one operation; new key for a new operation"}
},
"responses": {"Error": {"description": "See error.code and HTTP status; never blindly retry conflicts", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}},
"schemas": {
"Error": {"type": "object", "properties": {"error": {"type": "object", "properties": {"code": {"type": "string"}, "message": {"type": "string"}}}}},
"Certificate": {"type": "object", "required": ["version", "payload", "signature", "canonical_sha256"], "properties": {"version": {"type": "string", "enum": ["license-v2"]}, "payload": {"$ref": "#/components/schemas/CertificatePayload"}, "signature": {"type": "string", "description": "ASN.1 DER ECDSA, unpadded standard Base64"}, "canonical_sha256": {"type": "string"}}},
"CertificatePayload": {"type": "object", "additionalProperties": false, "required": ["version", "instance_id", "license_id", "customer_id", "product_id", "starts_at", "expires_at", "key_id"], "properties": {"version": {"type": "string", "enum": ["license-v2"]}, "instance_id": {"type": "string"}, "license_id": {"type": "integer", "minimum": 1}, "customer_id": {"type": "integer", "minimum": 1}, "product_id": {"type": "integer", "minimum": 1}, "starts_at": {"type": "string", "format": "date-time"}, "expires_at": {"type": "string", "format": "date-time"}, "key_id": {"type": "string"}}},
"Lease": {"type": "object", "required": ["payload", "signature", "canonical_sha256", "algorithm", "next_heartbeat_seconds"], "properties": {"payload": {"$ref": "#/components/schemas/LeasePayload"}, "signature": {"type": "string", "description": "ASN.1 DER ECDSA, unpadded standard Base64"}, "canonical_sha256": {"type": "string"}, "algorithm": {"type": "string", "enum": ["ECDSA-P256-SHA256"]}, "next_heartbeat_seconds": {"type": "integer", "minimum": 60}}},
"LeasePayload": {"type": "object", "additionalProperties": false, "required": ["version", "instance_id", "state", "server_time", "entitlement_expires_at", "lease_expires_at", "key_id"], "properties": {"version": {"type": "string", "enum": ["lease-v2"]}, "instance_id": {"type": "string"}, "state": {"type": "string", "enum": ["active", "frozen", "revoked", "expired", "not_started", "not_activated"]}, "server_time": {"type": "string", "format": "date-time"}, "entitlement_expires_at": {"type": "string", "format": "date-time"}, "lease_expires_at": {"type": "string", "format": "date-time"}, "key_id": {"type": "string"}}}
}
}
}

View File

@@ -0,0 +1,75 @@
# 客户端接入协议 1.0
本资料对应当前平台实现,不是独立完成生产验收的 SDK。运行 API 基地址由部署方提供,例如 https://license.example.com。必须使用 HTTPS,并验证服务端证书。管理员接口仅用于交付实例凭据,不应封装到客户端中。
## 交付与激活
管理员通过 POST /api/v1/licenses/{id}/instances 预置实例并交付 instance_id、license_key 和一次性 secret。secret 无法从后台恢复;不得跨安装复制共享凭据。
POST /api/v1/activation/instances/{instance_id}
头部:Content-Type: application/json,Idempotency-Key: 唯一业务操作标识。
请求:{"license_key":"部署方交付值","secret":"部署方交付值"}
成功 200:{"data":{"instance_id":"...","status":"activated","certificate":{"version":"license-v2","payload":{},"signature":"...","canonical_sha256":"..."}},"idempotent_replay":false}
同一次激活的网络重试复用请求内容和 Idempotency-Key。只持久化验签且绑定预期实例、产品、客户及有效期的证书。激活证书不等于运行租约,激活后仍需心跳。
## 心跳与运行租约
POST /api/v1/instances/{instance_id}/heartbeat
请求:{"secret":"部署方交付值","client_time":"2026-10-09T03:00:00Z"},要求 Idempotency-Key。
成功 200:{"data":{"payload":{"version":"lease-v2","instance_id":"...","state":"active","server_time":"...","entitlement_expires_at":"...","lease_expires_at":"...","key_id":"..."},"signature":"...","canonical_sha256":"...","algorithm":"ECDSA-P256-SHA256","next_heartbeat_seconds":60}}
服务端允许的租约状态:active、frozen、revoked、expired、not_started、not_activated。非 active 的已签名租约必须关闭业务运行;HTTP 200 本身不代表授权有效。
新心跳操作生成新键。网络重试复用原键及原 client_time,不得修改请求时间后继续使用原键。成功 active 心跳最短间隔 60 秒;根据 next_heartbeat_seconds 调度并加少量正向抖动。
## 验签规范
算法 ECDSA P-256 + SHA-256。公钥为 PEM SubjectPublicKeyInfo。signature 是 ASN.1 DER 的 ECDSA 签名,再进行标准 Base64 编码,不带 = 填充,不是 Base64URL,也不是固定 64 字节 r||s。
拒绝未知版本、缺少或额外字段、非法类型、包含 CR/LF/NUL 的字段、未知或撤销的 key_id、公钥不是 P-256,以及绑定到其他实例的信封。
按以下固定顺序输出 key=value,用单个 LF 连接,无末尾 LF,以 UTF-8 编码。时间使用收到的原始签名字符串,不能在验签前格式化或重新序列化。
license-v2:version、instance_id、license_id、customer_id、product_id、starts_at、expires_at、key_id。
lease-v2:version、instance_id、state、server_time、entitlement_expires_at、lease_expires_at、key_id。
ID 使用正十进制整数。Node.js 实现限制为安全整数。若部署数据超出安全整数范围,必须先约定解析方案。
canonical_sha256 是规范化字节的 SHA-256 十六进制;它仅是辅助校验,不能代替公钥验签。激活应验证 data.certificate,心跳应验证 data。
服务端未配置 keyring 时会生成 legacy v1;本接入方案拒绝 v1。部署前由运维确认 keyring 已配置并交付可信公钥与 key_id。GET /api/v1/activation/keyring、/activation/public-key 只是发现接口,不能自建信任根。
## 时间与状态存储
参照 client/verify.mjs:now 不得低于上次 local_high_water,server_time 不得低于 server_high_water;当前本地时间与服务端时间差不得超过 5 分钟。租约结束必须不晚于授权结束,且不超过服务端签发时间 + 14 天。两个到期时间都必须严格晚于 now 才能运行。
验签、范围检查后,先在受保护存储中原子保存实例 ID、租约、enabled、服务端与本地时间高水位,再启用业务。离线每次授权检查都应检查本地时间回拨,并持久化更新本地高水位。应用重新启动不得清除高水位绕过限制。
离线冻结和撤销不能立即让旧租约失效,最长残留授权可达 14 天。网络失败不得延长已接受的租约;有有效租约可在策略允许范围内继续,无有效租约必须 fail closed。一般受控存储不能抵抗完全控制主机的攻击者和磁盘快照回滚,不能宣称硬件绑定或防克隆。
## 重试与错误
错误结构为 {"error":{"code":"...","message":"..."}}。根据状态、code 和操作区分错误,不记录 secret 或原始请求正文。
400 修复参数;401/403 需要重新交付凭据或人工检查,不做无限重试;409 是冲突,需要检查 code 和实例状态,不自动重激活。429 在至少等待 Retry-After 后有界重试;无该头时指数退避。超时、连接错误和 5xx 有界退避,同一操作维持相同请求与键。心跳拒绝不能替代已签名撤销事件;在未收到可验证的非 active 租约时,已有租约的处理由离线策略决定,不得延长。
## 发布包下载
POST /api/v1/customers/{customer_id}/releases/{id}/download,正文 {"instance_id":"...","secret":"..."}。每个请求都提交凭据。支持 Range 续传,处理 200/206/416 和 Content-Range。使用受控的发布元数据校验完整包 size_bytes 和 SHA-256 后才安装。未经校验的部分文件不得覆盖运行版本。Nginx 内部文件路径不可作为客户端公共下载 URL。
## 模式约束
桌面:系统密钥库与单实例状态锁。服务:受限服务账户、超时、取消和受控后台任务。容器:稳定持久卷及 Secret 注入;重建不能生成新身份,多个副本不得共享同一实例凭据冒充独立席位。Kubernetes 集群授权需要额外的 License Agent 和席位策略,不在当前 SDK 范围内。
SPA 应通过同源 BFF 调用,不得在浏览器长期保存实例 secret。移动端、CI 短命实例和完全离线设备需要专门身份与交付策略,不能直接承诺当前协议覆盖。
## 交付验收
覆盖正常验签、篡改字段/签名、未知/撤销密钥、v1/额外字段、实例不匹配、过期和超过 14 天、时钟回拨、非 active、幂等重试、429、掉线与原子存储失败。测试向量只使用随机测试密钥,绝不能导入生产信任配置。