mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-18 23:01:23 +00:00
Grok Build access token 带有数字 tier claim(0=free、1=supergrok、5=heavy、6=lite), 原先只解 email/sub/team_id,refresh 还会把旧档位抄回去,订阅失效后一直显示 Heavy。 - 解码 JWT tier,并归一化 display / header 别名(含 free-tier、SuperGrok Lite) - 仅从 access token 读取档位;新 JWT 覆盖已存凭证,AT 无 claim 时保留旧值 - 用量与 free-cache 判定以当前 AT 为准,账单月额度仅作降级
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package xai
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// MapJWTSubscriptionTier maps prod_auth.SubscriptionTier numeric JWT claims
|
|
// to stable snake_case keys used by Grok Build / Mixpanel.
|
|
func MapJWTSubscriptionTier(tier uint64) string {
|
|
switch tier {
|
|
case 0:
|
|
return "free"
|
|
case 1:
|
|
return "supergrok"
|
|
case 2:
|
|
return "x_basic"
|
|
case 3:
|
|
return "x_premium"
|
|
case 4:
|
|
return "x_premium_plus"
|
|
case 5:
|
|
return "supergrok_heavy"
|
|
case 6:
|
|
return "supergrok_lite"
|
|
case 7:
|
|
return "supergrok_plus"
|
|
default:
|
|
return strconv.FormatUint(tier, 10)
|
|
}
|
|
}
|
|
|
|
// NormalizeSubscriptionTier canonicalizes display names, /user strings, and
|
|
// JWT-derived keys onto the same snake_case identifiers.
|
|
func NormalizeSubscriptionTier(raw string) string {
|
|
t := strings.ToLower(strings.TrimSpace(raw))
|
|
t = strings.ReplaceAll(t, "-", "_")
|
|
t = strings.Join(strings.Fields(t), "_")
|
|
switch t {
|
|
case "free", "grok_free", "grokfree", "free_tier", "freetier", "grok_basic", "grokbasic":
|
|
return "free"
|
|
case "supergrok", "grokpro":
|
|
return "supergrok"
|
|
case "supergrok_lite", "supergroklite":
|
|
return "supergrok_lite"
|
|
case "supergrok_heavy", "supergrokheavy", "supergrokpro":
|
|
return "supergrok_heavy"
|
|
case "supergrok_plus", "supergrokplus":
|
|
return "supergrok_plus"
|
|
case "x_basic", "xbasic", "basic":
|
|
return "x_basic"
|
|
case "x_premium", "xpremium":
|
|
return "x_premium"
|
|
case "x_premium_plus", "xpremiumplus", "x_premium+":
|
|
return "x_premium_plus"
|
|
default:
|
|
return t
|
|
}
|
|
}
|
|
|
|
// SubscriptionTierFromJWT decodes an access token payload (no signature check)
|
|
// and maps the numeric or string `tier` claim.
|
|
func SubscriptionTierFromJWT(jwt string) string {
|
|
claims := DecodeJWTClaims(jwt)
|
|
if claims == nil {
|
|
return ""
|
|
}
|
|
raw, ok := claims["tier"]
|
|
if !ok || raw == nil {
|
|
return ""
|
|
}
|
|
switch v := raw.(type) {
|
|
case float64:
|
|
if v < 0 {
|
|
return ""
|
|
}
|
|
return MapJWTSubscriptionTier(uint64(v))
|
|
case json.Number:
|
|
n, err := v.Int64()
|
|
if err != nil || n < 0 {
|
|
return NormalizeSubscriptionTier(v.String())
|
|
}
|
|
return MapJWTSubscriptionTier(uint64(n))
|
|
case string:
|
|
trimmed := strings.TrimSpace(v)
|
|
if trimmed == "" {
|
|
return ""
|
|
}
|
|
if n, err := strconv.ParseUint(trimmed, 10, 64); err == nil {
|
|
return MapJWTSubscriptionTier(n)
|
|
}
|
|
return NormalizeSubscriptionTier(trimmed)
|
|
default:
|
|
return ""
|
|
}
|
|
}
|