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,106 @@
package httpapi
import (
"archive/zip"
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/gin-gonic/gin"
)
func languageRequirements(language string) string {
return map[string]string{
"python": "Python:使用 cryptography 的 ECDSA(SHA256) 和 httpx;base64 补齐填充后解码 DER;pytest 覆盖验签与状态机。密钥库通过可注入接口实现,禁止以硬编码加密密钥充当保护。",
"go": "Go:使用 crypto/ecdsa.VerifyASN1、crypto/x509、sha256 和 base64.RawStdEncoding;所有网络调用支持 context、超时及取消;sync.Mutex 与跨进程文件锁保护状态;go test 覆盖行为。",
"java": "Java:使用 JCA SHA256withECDSA、KeyFactory EC、X509EncodedKeySpec 与 HttpClient;Base64 解码 DER;受控调度器与原子文件替换;JUnit 覆盖行为。",
"dotnet": ".NET:使用 ECDsa.VerifyData 并显式选择 DSASignatureFormat.Rfc3279DerSequence,不使用默认 P1363;HttpClient、CancellationToken、受控 IHostedService 与可注入受保护存储;xUnit 覆盖行为。",
"nodejs": "Node.js:使用 node:crypto 的 verify('sha256', canonicalBytes, publicKey, DER),参照 client/verify.mjs;使用安全整数解析 ID;node:test 覆盖行为;不得将服务端 SDK 打包进浏览器。",
}[language]
}
func signedKitVectors() ([]byte, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
publicDER, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return nil, err
}
sign := func(payload map[string]any, canonical string) (map[string]any, error) {
digest := sha256.Sum256([]byte(canonical))
signature, err := ecdsa.SignASN1(rand.Reader, key, digest[:])
if err != nil {
return nil, err
}
return map[string]any{"payload": payload, "signature": base64.RawStdEncoding.EncodeToString(signature), "canonical_sha256": hex.EncodeToString(digest[:]), "canonical": canonical}, nil
}
instance := "11111111-2222-3333-4444-555555555555"
licensePayload := map[string]any{"version": "license-v2", "instance_id": instance, "license_id": 1, "customer_id": 2, "product_id": 3, "starts_at": "2026-10-09T03:00:00Z", "expires_at": "2027-10-09T03:00:00Z", "key_id": "integration-test-only"}
licenseCanonical := "version=license-v2\ninstance_id=" + instance + "\nlicense_id=1\ncustomer_id=2\nproduct_id=3\nstarts_at=2026-10-09T03:00:00Z\nexpires_at=2027-10-09T03:00:00Z\nkey_id=integration-test-only"
leasePayload := map[string]any{"version": "lease-v2", "instance_id": instance, "state": "active", "server_time": "2026-10-09T03:00:00Z", "entitlement_expires_at": "2027-10-09T03:00:00Z", "lease_expires_at": "2026-10-23T03:00:00Z", "key_id": "integration-test-only"}
leaseCanonical := "version=lease-v2\ninstance_id=" + instance + "\nstate=active\nserver_time=2026-10-09T03:00:00Z\nentitlement_expires_at=2027-10-09T03:00:00Z\nlease_expires_at=2026-10-23T03:00:00Z\nkey_id=integration-test-only"
license, err := sign(licensePayload, licenseCanonical)
if err != nil {
return nil, err
}
lease, err := sign(leasePayload, leaseCanonical)
if err != nil {
return nil, err
}
return json.MarshalIndent(map[string]any{"warning": "TEST ONLY: never trust this key in production; use frozen now=2026-10-09T03:00:00Z for state tests", "pinned_keys": []map[string]any{{"key_id": "integration-test-only", "public_key_pem": string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: publicDER})), "revoked": false}}, "license": license, "lease": lease}, "", " ")
}
func (a *BusinessAPI) downloadIntegrationArchive(c *gin.Context) {
language, mode, ok := integrationKitInput(c)
if !ok {
return
}
vectors, err := signedKitVectors()
if err != nil {
apiError(c, 500, "internal_error", "测试资料生成失败")
return
}
kit := buildIntegrationKit(language, mode)
files := map[string][]byte{
"README.md": []byte("# 客户端接入包\n\n阅读 protocol.md 与 openapi.json,将 prompt.md 提供给实现工程师。test-vectors.json 使用随机测试密钥,绝不能导入生产。此包提供文档和实现提示词,不是已完成生产验收的 SDK。\n"),
"prompt.md": []byte(kit.Prompt),
"test-vectors.json": vectors,
}
for _, name := range []string{"protocol.md", "openapi.json"} {
content, err := integrationAsset(name)
if err != nil {
apiError(c, 500, "internal_error", "接入资料不可用")
return
}
files[name] = content
}
var buffer bytes.Buffer
archive := zip.NewWriter(&buffer)
for _, name := range []string{"README.md", "protocol.md", "openapi.json", "prompt.md", "test-vectors.json"} {
file, err := archive.Create(name)
if err == nil {
_, err = file.Write(files[name])
}
if err != nil {
apiError(c, 500, "internal_error", "接入包生成失败")
return
}
}
if err := archive.Close(); err != nil {
apiError(c, 500, "internal_error", "接入包生成失败")
return
}
c.Header("Cache-Control", "no-store")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=license-platform-%s-%s.zip", language, mode))
c.Data(200, "application/zip", buffer.Bytes())
}