package server import ( "context" "errors" "net/http" "time" "git.breakpilot.com/platform/tenant-registry/internal/keycloak" "git.breakpilot.com/platform/tenant-registry/internal/store" ) type entitlementCheckReq struct { TenantID string `json:"tenant_id"` Product string `json:"product"` Addon string `json:"addon,omitempty"` // UserID (optional): the Keycloak user acting. A member blocked in the // tenant is not allowed (portal#74), whatever the tenant is entitled to. UserID string `json:"user_id,omitempty"` } // EntitlementCheck is the answer products cache for ≤ 60 s // (PRODUCT_INTEGRATION_SPEC 2.0 §4.4). ReadOnly mirrors the ladder: warning // keeps full access, restricted/halted/frozen block writes. type EntitlementCheck struct { Allowed bool `json:"allowed"` ReadOnly bool `json:"read_only"` Status string `json:"status,omitempty"` Limits map[string]interface{} `json:"limits,omitempty"` ExpiresAt *time.Time `json:"expires_at,omitempty"` Reason string `json:"reason,omitempty"` } // checkEntitlement is POST /v1/internal/entitlements/check. func (s *Server) checkEntitlement(w http.ResponseWriter, r *http.Request) { var in entitlementCheckReq if !decodeJSON(w, r, &in) { return } if in.TenantID == "" || in.Product == "" { writeError(w, http.StatusBadRequest, "invalid_input", "tenant_id and product are required") return } ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) defer cancel() res, err := s.evaluateEntitlement(ctx, in) if err != nil { writeError(w, http.StatusInternalServerError, "internal", err.Error()) return } if in.UserID != "" && res.Allowed { blocked, berr := s.memberBlocked(ctx, in.UserID, in.TenantID) switch { case errors.Is(berr, keycloak.ErrUserNotFound): res = EntitlementCheck{Status: res.Status, Reason: "user_unknown"} case berr != nil: s.Log.Warn("member block check failed; allowing", "user_id", in.UserID, "tenant_id", in.TenantID, "err", berr) case blocked: res = EntitlementCheck{Status: res.Status, Reason: "member_blocked"} } } writeJSON(w, http.StatusOK, res) } func (s *Server) evaluateEntitlement(ctx context.Context, in entitlementCheckReq) (EntitlementCheck, error) { prod, err := s.product(ctx, in.Product) if err != nil { if errors.Is(err, store.ErrNotFound) { return EntitlementCheck{Allowed: false, Reason: "unknown_product"}, nil } return EntitlementCheck{}, err } if in.Addon != "" && !prod.HasAddon(in.Addon) { return EntitlementCheck{Allowed: false, Reason: "unknown_addon"}, nil } t, err := s.Store.GetTenant(ctx, in.TenantID) if err != nil { if errors.Is(err, store.ErrNotFound) { return EntitlementCheck{Allowed: false, Reason: "tenant_not_found"}, nil } return EntitlementCheck{}, err } out := EntitlementCheck{Status: t.Status} switch t.Status { case store.StatusArchived: out.Reason = "tenant_archived" return out, nil case store.StatusHalted: out.Reason = "tenant_halted" return out, nil case store.StatusRestricted, store.StatusFrozen: out.ReadOnly = true } tps, err := s.Store.ListTenantProducts(ctx, t.ID) if err != nil { return EntitlementCheck{}, err } now := time.Now().UTC() for _, tp := range tps { if tp.Product != prod.Key { continue } if !tp.Enabled { out.Reason = "entitlement_disabled" return out, nil } if tp.ExpiresAt != nil && tp.ExpiresAt.Before(now) { out.Reason = "entitlement_expired" out.ExpiresAt = tp.ExpiresAt return out, nil } if in.Addon != "" { key := addonLimitKey(prod, in.Addon) if v, ok := tp.Config[key]; !ok || toFloat(v) <= 0 { out.Reason = "addon_not_purchased" return out, nil } } out.Allowed = true out.Limits = tp.Config out.ExpiresAt = tp.ExpiresAt return out, nil } out.Reason = "not_entitled" return out, nil } func addonLimitKey(p *store.Product, addon string) string { for _, a := range p.Addons { if a.ID == addon { if a.LimitKey != "" { return a.LimitKey } return addon } } return addon } func toFloat(v interface{}) float64 { switch n := v.(type) { case float64: return n case int: return float64(n) case int64: return float64(n) case bool: if n { return 1 } } return 0 }