- 加密租约缓存(AES-256-GCM + HKDF(指纹)):重启恢复 GRACE,401/410 自动重新激活 - WithGracePolicy(KeepBooleans)、WithLicenseBytes、WithHeartbeatInterval、WithHTTPClient、WithAutoActivate、WithOnEntitlementsChange、Deactivate - 心跳/激活 server_time 参与可信时间校正 - 新增缓存/降级/时钟/重激活/回调测试
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
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)。
|
||
// WithGracePolicy(GraceKeepBooleans) 时,EXPIRED 后仍放行 payload 中为 true 的布尔 Feature。
|
||
func (c *Client) Enabled(fid string) bool {
|
||
if ok, _ := c.Check(fid); ok {
|
||
return true
|
||
}
|
||
if c.opts.gracePolicy != GraceKeepBooleans || c.State() != StateExpired {
|
||
return false
|
||
}
|
||
s := c.snap.Load()
|
||
if s == nil || s.payload == nil {
|
||
return false
|
||
}
|
||
ent, ok := s.payload.Ent[fid]
|
||
return ok && ent.AsBool()
|
||
}
|
||
|
||
// 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
|
||
}
|