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 }