chore: initial commit of ai site platform
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
121
platform/internal/agentcap/capsule.go
Normal file
121
platform/internal/agentcap/capsule.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package agentcap
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/hkdf"
|
||||
)
|
||||
|
||||
const Prefix = "AJZ1"
|
||||
|
||||
// Descriptor 智能体解密后才能看到的请求契约(前端不展示明文)。
|
||||
type Descriptor struct {
|
||||
Version string `json:"v"`
|
||||
BaseURL string `json:"base_url"`
|
||||
AppSlug string `json:"app_slug"`
|
||||
TenantHint string `json:"tenant_hint,omitempty"`
|
||||
Auth AuthSpec `json:"auth"`
|
||||
Resources []ResourceSpec `json:"resources"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AuthSpec struct {
|
||||
Type string `json:"type"` // bearer_jwt
|
||||
Header string `json:"header"`
|
||||
}
|
||||
|
||||
type ResourceSpec struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Methods []string `json:"methods"`
|
||||
Filters []string `json:"filters,omitempty"`
|
||||
Sorts []string `json:"sorts,omitempty"`
|
||||
Fields []FieldSpec `json:"fields,omitempty"`
|
||||
PrimaryKey string `json:"primary_key"`
|
||||
}
|
||||
|
||||
type FieldSpec struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func DeriveKey(masterSecret string, tenantID int64, userID int64) []byte {
|
||||
h := hkdf.New(sha256.New, []byte(masterSecret), []byte("aijianzhan-agent-v1"), []byte(fmt.Sprintf("%d:%d", tenantID, userID)))
|
||||
key := make([]byte, 32)
|
||||
_, _ = io.ReadFull(h, key)
|
||||
return key
|
||||
}
|
||||
|
||||
func Encrypt(key []byte, desc *Descriptor) (string, error) {
|
||||
raw, err := json.Marshal(desc)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ciphertext := gcm.Seal(nil, nonce, raw, nil)
|
||||
return fmt.Sprintf("%s.%s.%s",
|
||||
Prefix,
|
||||
base64.RawURLEncoding.EncodeToString(nonce),
|
||||
base64.RawURLEncoding.EncodeToString(ciphertext),
|
||||
), nil
|
||||
}
|
||||
|
||||
func Decrypt(key []byte, capsule string) (*Descriptor, error) {
|
||||
parts := strings.Split(capsule, ".")
|
||||
if len(parts) != 3 || parts[0] != Prefix {
|
||||
return nil, fmt.Errorf("invalid capsule format")
|
||||
}
|
||||
nonce, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ciphertext, err := base64.RawURLEncoding.DecodeString(parts[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plain, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decrypt failed")
|
||||
}
|
||||
var desc Descriptor
|
||||
if err := json.Unmarshal(plain, &desc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &desc, nil
|
||||
}
|
||||
|
||||
func PublicAgentKey(masterSecret string, tenantID, userID int64) string {
|
||||
key := DeriveKey(masterSecret, tenantID, userID)
|
||||
return base64.RawURLEncoding.EncodeToString(key)
|
||||
}
|
||||
|
||||
func ParseAgentKey(b64 string) ([]byte, error) {
|
||||
return base64.RawURLEncoding.DecodeString(b64)
|
||||
}
|
||||
36
platform/internal/agentcap/capsule_test.go
Normal file
36
platform/internal/agentcap/capsule_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package agentcap
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
key := DeriveKey("secret", 1, 2)
|
||||
cap, err := Encrypt(key, &Descriptor{
|
||||
Version: "1",
|
||||
BaseURL: "http://127.0.0.1:8888",
|
||||
AppSlug: "demo",
|
||||
Auth: AuthSpec{Type: "bearer_jwt", Header: "Authorization"},
|
||||
Resources: []ResourceSpec{{
|
||||
Name: "items", Path: "/api/v1/apps/demo/items",
|
||||
Methods: []string{"GET", "POST"}, PrimaryKey: "id",
|
||||
Fields: []FieldSpec{{Name: "title", Type: "string"}},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cap[:4] != Prefix {
|
||||
t.Fatalf("prefix: %s", cap)
|
||||
}
|
||||
desc, err := Decrypt(key, cap)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if desc.AppSlug != "demo" || len(desc.Resources) != 1 {
|
||||
t.Fatalf("%+v", desc)
|
||||
}
|
||||
if _, err := Decrypt([]byte("bad-key-bad-key-bad-key-bad!!!!"), cap); err == nil {
|
||||
t.Fatal("expected decrypt fail")
|
||||
}
|
||||
}
|
||||
114
platform/internal/agentcap/modpath.go
Normal file
114
platform/internal/agentcap/modpath.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package agentcap
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const ModulePathPrefix = "ajzm1_"
|
||||
|
||||
// ModulePathClaim 写入加密路径 token。根据用户/智能体 ID 生成路径,公开侧可用平台密钥解开。
|
||||
type ModulePathClaim struct {
|
||||
TenantID int64 `json:"t"`
|
||||
OwnerID int64 `json:"o"` // user_id 或 agent_id(宇恒侧用户)
|
||||
Slug string `json:"s"`
|
||||
IssuedAt int64 `json:"iat"`
|
||||
}
|
||||
|
||||
func modulePathKey(masterSecret string) []byte {
|
||||
sum := sha256.Sum256([]byte("aijianzhan-module-path-v1|" + masterSecret))
|
||||
return sum[:]
|
||||
}
|
||||
|
||||
// SealModulePath 根据用户 ID(及租户、模块 slug)加密,返回:
|
||||
// - token:单段密文(可作 URL 段)
|
||||
// - filePath:逻辑文件路径 m/{token}(不含明文用户 id / slug)
|
||||
func SealModulePath(masterSecret string, tenantID, ownerID int64, slug string) (token, filePath string, err error) {
|
||||
slug = strings.TrimSpace(slug)
|
||||
if slug == "" {
|
||||
return "", "", fmt.Errorf("slug required")
|
||||
}
|
||||
if ownerID <= 0 {
|
||||
return "", "", fmt.Errorf("owner id required")
|
||||
}
|
||||
if tenantID <= 0 {
|
||||
return "", "", fmt.Errorf("tenant id required")
|
||||
}
|
||||
claim := ModulePathClaim{
|
||||
TenantID: tenantID,
|
||||
OwnerID: ownerID,
|
||||
Slug: slug,
|
||||
IssuedAt: time.Now().UTC().Unix(),
|
||||
}
|
||||
raw, err := json.Marshal(claim)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
key := modulePathKey(masterSecret)
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
ciphertext := gcm.Seal(nil, nonce, raw, nil)
|
||||
packed := append(nonce, ciphertext...)
|
||||
token = ModulePathPrefix + base64.RawURLEncoding.EncodeToString(packed)
|
||||
filePath = "m/" + token
|
||||
return token, filePath, nil
|
||||
}
|
||||
|
||||
// OpenModulePath 解密路径 token,得到租户、用户、模块 slug。
|
||||
func OpenModulePath(masterSecret, token string) (*ModulePathClaim, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
token = strings.TrimPrefix(token, "/")
|
||||
if strings.HasPrefix(token, "m/") {
|
||||
token = strings.TrimPrefix(token, "m/")
|
||||
}
|
||||
if !strings.HasPrefix(token, ModulePathPrefix) {
|
||||
return nil, fmt.Errorf("invalid module path token")
|
||||
}
|
||||
packed, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(token, ModulePathPrefix))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid module path token encoding")
|
||||
}
|
||||
key := modulePathKey(masterSecret)
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ns := gcm.NonceSize()
|
||||
if len(packed) < ns+1 {
|
||||
return nil, fmt.Errorf("invalid module path token length")
|
||||
}
|
||||
plain, err := gcm.Open(nil, packed[:ns], packed[ns:], nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module path decrypt failed")
|
||||
}
|
||||
var claim ModulePathClaim
|
||||
if err := json.Unmarshal(plain, &claim); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if claim.TenantID <= 0 || claim.OwnerID <= 0 || claim.Slug == "" {
|
||||
return nil, fmt.Errorf("module path claim incomplete")
|
||||
}
|
||||
return &claim, nil
|
||||
}
|
||||
28
platform/internal/agentcap/modpath_test.go
Normal file
28
platform/internal/agentcap/modpath_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package agentcap
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSealOpenModulePath(t *testing.T) {
|
||||
secret := "test-secret"
|
||||
token, filePath, err := SealModulePath(secret, 1, 42, "settlement")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if filePath != "m/"+token {
|
||||
t.Fatalf("filePath=%s", filePath)
|
||||
}
|
||||
claim, err := OpenModulePath(secret, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if claim.TenantID != 1 || claim.OwnerID != 42 || claim.Slug != "settlement" {
|
||||
t.Fatalf("%+v", claim)
|
||||
}
|
||||
claim2, err := OpenModulePath(secret, filePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if claim2.Slug != "settlement" {
|
||||
t.Fatalf("%+v", claim2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user