- payload 新增 bc(编译截止 Unix 秒)/bcm(deny|degrade),结构校验拒绝负值与非法动作 - SDK 支持 WithBuildTime/WithBuildTimeUnix 与 ldflags BuildTime 注入;未提供构建时间时 fail-closed - degrade 模式进入 OUTDATED 状态,授权查询零值但进程继续运行;修复心跳/Proxy 续租覆盖该状态的隐患 - 缓存恢复路径同样执行截止判定;无截止的存量 license 行为不变 - 补充 license/sdk 单元测试与状态迁移回归用例
154 lines
4.1 KiB
Go
154 lines
4.1 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"`
|
||
BuildCutoff int64 `json:"build_cutoff,omitempty"` // 0=不限制
|
||
BuildCutoffAction string `json:"build_cutoff_action,omitempty"` // deny|degrade
|
||
}
|
||
|
||
// 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 StateOutdated:
|
||
return false, ReasonStateOutdated
|
||
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
|
||
info := LicenseInfo{
|
||
JTI: p.JTI, TenantID: p.TID, TenantName: p.TName,
|
||
ProductKey: p.Prod, LicenseType: p.LT,
|
||
ExpiresAt: p.EXP, NotBefore: p.NBF,
|
||
BuildCutoff: p.BC,
|
||
}
|
||
if p.BC > 0 {
|
||
info.BuildCutoffAction = p.EffectiveBuildCutoffAction()
|
||
}
|
||
return info
|
||
}
|
||
|
||
// tierRank 返回档位序号(未知档位返回 -1,视为低于一切已知档位)。
|
||
func tierRank(tier string, order []string) int {
|
||
for i, t := range order {
|
||
if t == tier {
|
||
return i
|
||
}
|
||
}
|
||
return -1
|
||
}
|