- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属: license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓) - sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc) - license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书 - 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require - CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
This commit is contained in:
32 files changed
+3940
No files matched your search
+133
@@ -0,0 +1,133 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"maps"
|
||||
|
||||
"git.wxccs.org/EntitleForge/EntitleForge-SDK-go/license"
|
||||
)
|
||||
|
||||
// LicenseInfo 是只读授权摘要。
|
||||
type LicenseInfo struct {
|
||||
JTI string `json:"jti"`
|
||||
TenantID string `json:"tenant_id"`
|
||||
TenantName string `json:"tenant_name,omitempty"`
|
||||
ProductKey string `json:"product_key"`
|
||||
LicenseType string `json:"license_type"`
|
||||
ExpiresAt int64 `json:"expires_at"` // 0=永久
|
||||
NotBefore int64 `json:"not_before"`
|
||||
}
|
||||
|
||||
// usable 判断当前状态是否允许正常查询(ACTIVE/GRACE)。
|
||||
func (c *Client) usable() bool {
|
||||
switch c.State() {
|
||||
case StateActive, StateGrace:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Enabled 布尔开关查询;未授权或状态不可用一律 false(docs/design/05-entitlement-model.md §4 规则 1)。
|
||||
func (c *Client) Enabled(fid string) bool {
|
||||
ok, _ := c.Check(fid)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Quantity 数量配额查询;未授权返回 0。
|
||||
func (c *Client) Quantity(fid string) int64 {
|
||||
s := c.snap.Load()
|
||||
if s == nil || !c.usable() || s.payload == nil {
|
||||
return 0
|
||||
}
|
||||
return s.payload.Ent[fid].AsInt()
|
||||
}
|
||||
|
||||
// Tier 返回档位;无档位授权返回空串。
|
||||
func (c *Client) Tier() string {
|
||||
s := c.snap.Load()
|
||||
if s == nil || !c.usable() || s.payload == nil {
|
||||
return ""
|
||||
}
|
||||
return s.payload.Ent["tier"].AsString()
|
||||
}
|
||||
|
||||
// TierAtLeast 档位序比较(档位序由 WithTierOrder 注入或用默认序)。
|
||||
func (c *Client) TierAtLeast(min string) bool {
|
||||
cur := c.Tier()
|
||||
if cur == "" {
|
||||
return false
|
||||
}
|
||||
return tierRank(cur, c.opts.tierOrder) >= tierRank(min, c.opts.tierOrder)
|
||||
}
|
||||
|
||||
// Allows 约束集合查询。
|
||||
func (c *Client) Allows(fid, value string) bool {
|
||||
s := c.snap.Load()
|
||||
if s == nil || !c.usable() || s.payload == nil {
|
||||
return false
|
||||
}
|
||||
return s.payload.Ent[fid].Allows(value)
|
||||
}
|
||||
|
||||
// Entitlements 返回授权集合拷贝(含全部状态下的真实值,供管理端点展示)。
|
||||
func (c *Client) Entitlements() map[string]license.Entitlement {
|
||||
s := c.snap.Load()
|
||||
if s == nil || s.payload == nil {
|
||||
return map[string]license.Entitlement{}
|
||||
}
|
||||
out := make(map[string]license.Entitlement, len(s.payload.Ent))
|
||||
maps.Copy(out, s.payload.Ent)
|
||||
return out
|
||||
}
|
||||
|
||||
// Check 一次性谓词:授权完整且状态可用时 OK,否则 false + 精确原因。
|
||||
func (c *Client) Check(fid string) (bool, Reason) {
|
||||
s := c.snap.Load()
|
||||
if s == nil || s.payload == nil {
|
||||
return false, ReasonStateUnverified
|
||||
}
|
||||
switch st := s.state; st {
|
||||
case StateGrace:
|
||||
if _, ok := s.payload.Ent[fid]; !ok {
|
||||
return false, ReasonNotEntitled
|
||||
}
|
||||
return true, ReasonStateGrace
|
||||
case StateExpired:
|
||||
return false, ReasonStateExpired
|
||||
case StateRevoked:
|
||||
return false, ReasonStateRevoked
|
||||
case StateInvalid:
|
||||
return false, ReasonStateInvalid
|
||||
case StateUnverified:
|
||||
return false, ReasonStateUnverified
|
||||
case StateActive:
|
||||
if _, ok := s.payload.Ent[fid]; !ok {
|
||||
return false, ReasonNotEntitled
|
||||
}
|
||||
return true, ReasonOK
|
||||
}
|
||||
return false, ReasonStateInvalid
|
||||
}
|
||||
|
||||
// Info 返回只读授权摘要(无授权时零值)。
|
||||
func (c *Client) Info() LicenseInfo {
|
||||
s := c.snap.Load()
|
||||
if s == nil || s.payload == nil {
|
||||
return LicenseInfo{}
|
||||
}
|
||||
p := s.payload
|
||||
return LicenseInfo{
|
||||
JTI: p.JTI, TenantID: p.TID, TenantName: p.TName,
|
||||
ProductKey: p.Prod, LicenseType: p.LT,
|
||||
ExpiresAt: p.EXP, NotBefore: p.NBF,
|
||||
}
|
||||
}
|
||||
|
||||
// tierRank 返回档位序号(未知档位返回 -1,视为低于一切已知档位)。
|
||||
func tierRank(tier string, order []string) int {
|
||||
for i, t := range order {
|
||||
if t == tier {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
Reference in new issue
Block a user