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,154 @@
package httpapi
import (
"embed"
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
//go:embed integration_assets/*
var integrationAssets embed.FS
const defaultIntegrationBaseURL = "https://license.yihuiyong.cn"
var integrationLanguages = map[string]string{
"python": "Python 3.11+",
"go": "Go 1.22+",
"java": "Java 17+",
"dotnet": ".NET 8+",
"nodejs": "Node.js 20+",
}
var integrationModes = map[string]string{
"desktop": "桌面客户端",
"service": "常驻服务",
"container": "容器部署",
}
type integrationKitResponse struct {
ProtocolVersion string `json:"protocol_version"`
PlatformBaseURL string `json:"platform_base_url"`
Language string `json:"language"`
LanguageLabel string `json:"language_label"`
Mode string `json:"mode"`
ModeLabel string `json:"mode_label"`
Endpoints []kitAPI `json:"endpoints"`
SecurityRules []string `json:"security_rules"`
Prompt string `json:"prompt"`
TestVector kitVector `json:"payload_example"`
Protocol string `json:"protocol"`
}
type kitAPI struct {
Method string `json:"method"`
Path string `json:"path"`
Purpose string `json:"purpose"`
}
type kitVector struct {
Version string `json:"version"`
InstanceID string `json:"instance_id"`
State string `json:"state"`
ServerTime string `json:"server_time"`
EntitlementExpiresAt string `json:"entitlement_expires_at"`
LeaseExpiresAt string `json:"lease_expires_at"`
}
func integrationKitInput(c *gin.Context) (string, string, bool) {
language, mode := c.DefaultQuery("language", "python"), c.DefaultQuery("mode", "service")
if _, ok := integrationLanguages[language]; !ok {
apiError(c, http.StatusBadRequest, "invalid_request", "不支持的客户端语言")
return "", "", false
}
if _, ok := integrationModes[mode]; !ok {
apiError(c, http.StatusBadRequest, "invalid_request", "不支持的接入模式")
return "", "", false
}
return language, mode, true
}
func buildIntegrationKit(language, mode string) integrationKitResponse {
label, modeLabel := integrationLanguages[language], integrationModes[mode]
protocol, _ := integrationAsset("protocol.md")
return integrationKitResponse{
ProtocolVersion: "license-v2 / lease-v2",
PlatformBaseURL: defaultIntegrationBaseURL,
Language: language,
LanguageLabel: label,
Mode: mode,
ModeLabel: modeLabel,
Endpoints: []kitAPI{
{Method: "POST", Path: "/api/v1/activation/instances/{instance_id}", Purpose: "首次激活,提交 license_key、secret 和 Idempotency-Key"},
{Method: "POST", Path: "/api/v1/instances/{instance_id}/heartbeat", Purpose: "刷新已签名运行租约,提交 secret、client_time 和 Idempotency-Key"},
{Method: "POST", Path: "/api/v1/customers/{customer_id}/releases/{release_id}/download", Purpose: "以实例凭据下载当前有权访问的发布包"},
},
SecurityRules: []string{
"仅信任随软件或受控部署渠道固定分发的 P-256 公钥;不得将 keyring 发现接口作为信任根。",
"secret 只可放在系统密钥库、Vault、Docker/Kubernetes Secret 或受限加密文件中;不得进入 URL、日志、镜像、源码或浏览器 LocalStorage。",
"每次网络重试复用同一 Idempotency-Key;新的业务操作必须生成新的键。",
"验签后核对 version、key_id、instance_id、state、授权到期和租约到期;仅 active 且未到期时可执行业务。",
"保存服务端时间与本地时间高水位,拒绝显著回拨;正常心跳最短 60 秒,租约最长 14 天。",
"403/409 进入不可用或人工处理状态;429 遵循 Retry-After;不得无限自动重试。",
},
Protocol: string(protocol),
Prompt: integrationPrompt(label, modeLabel) + "\n\n" + languageRequirements(language) + "\n\n必须阅读接入包 protocol.md 与 openapi.json。签名规范化不是 JSON:固定顺序 key=value、LF 连接、UTF-8、无末尾换行;签名是 DER ECDSA 的无填充标准 Base64。先通过包内随机测试公钥的 license-v2 和 lease-v2 验签向量,再连接部署环境。测试公钥不得在生产中信任。",
TestVector: kitVector{
Version: "lease-v2", InstanceID: "11111111-2222-3333-4444-555555555555", State: "active",
ServerTime: "2026-10-09T03:00:00Z", EntitlementExpiresAt: "2027-10-09T03:00:00Z", LeaseExpiresAt: "2026-10-23T03:00:00Z",
},
}
}
func integrationAsset(name string) ([]byte, error) {
content, err := integrationAssets.ReadFile("integration_assets/" + name)
if err != nil {
return nil, err
}
return []byte(strings.ReplaceAll(string(content), "https://license.example.com", defaultIntegrationBaseURL)), nil
}
func integrationPrompt(language, mode string) string {
return fmt.Sprintf(`你正在为 %s %s 接入授权平台。实现可复用的 LicenseClient,不要调用管理员接口,也不要把实例 secret 暴露给浏览器。
默认授权平台根地址:%s。将它作为 SDK 的默认值,同时允许部署通过 LICENSE_API_BASE_URL 覆盖;配置为根地址,不要附加 /api/v1,不要在业务代码中散落硬编码 URL。
实现激活 POST /api/v1/activation/instances/{instance_id},提交 license_key、secret 和 Idempotency-Key;实现心跳 POST /api/v1/instances/{instance_id}/heartbeat,提交 secret、RFC3339 UTC client_time 和 Idempotency-Key。
必须使用受控渠道预置的 ECDSA P-256 公钥验证 license-v2 / lease-v2 信封,验证 key_id、version、instance_id、state、entitlement_expires_at、lease_expires_at。仅 active 且授权/租约均未过期时允许业务运行。持久化并原子更新已接受租约及本地、服务端时间高水位。secret 不得写入 URL、日志、异常、镜像或源码;403/409 停止自动重试,429 遵循 Retry-After。
请交付:LicenseClient、受保护状态存储接口、激活与刷新租约实现、验签单元测试、网络/时钟回拨/过期租约负向测试,以及业务侧 canRun 调用示例。`, language, mode, defaultIntegrationBaseURL)
}
func (a *BusinessAPI) integrationKit(c *gin.Context) {
language, mode, ok := integrationKitInput(c)
if !ok {
return
}
c.JSON(http.StatusOK, gin.H{"data": buildIntegrationKit(language, mode)})
}
func (a *BusinessAPI) downloadIntegrationKit(c *gin.Context) {
language, mode, ok := integrationKitInput(c)
if !ok {
return
}
kit := buildIntegrationKit(language, mode)
var b strings.Builder
fmt.Fprintf(&b, "# 授权平台客户端接入包\n\n协议版本:%s\n\n默认授权平台根地址:%s\n\n", kit.ProtocolVersion, kit.PlatformBaseURL)
fmt.Fprintf(&b, "目标:%s,%s\n\n## 工程提示词\n\n%s\n\n## 接口\n\n", kit.LanguageLabel, kit.ModeLabel, kit.Prompt)
for _, endpoint := range kit.Endpoints {
fmt.Fprintf(&b, "- `%s %s`:%s\n", endpoint.Method, endpoint.Path, endpoint.Purpose)
}
b.WriteString("\n## 安全检查\n\n")
for _, rule := range kit.SecurityRules {
fmt.Fprintf(&b, "- %s\n", rule)
}
fmt.Fprintf(&b, "\n## 租约字段示例(不是签名测试向量)\n\n```json\n{\n \"version\": \"%s\",\n \"instance_id\": \"%s\",\n \"state\": \"%s\",\n \"server_time\": \"%s\",\n \"entitlement_expires_at\": \"%s\",\n \"lease_expires_at\": \"%s\"\n}\n```\n", kit.TestVector.Version, kit.TestVector.InstanceID, kit.TestVector.State, kit.TestVector.ServerTime, kit.TestVector.EntitlementExpiresAt, kit.TestVector.LeaseExpiresAt)
fmt.Fprintf(&b, "\n\n%s\n", kit.Protocol)
c.Header("Cache-Control", "no-store")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=license-platform-%s-%s-integration.md", language, mode))
c.Data(http.StatusOK, "text/markdown; charset=utf-8", []byte(b.String()))
}