On this page
Parse, expire, and write OAuth token entries in ~/.claudewheel/tokens.json.
#claudewheel.tokens
#claudewheel.tokens
Parse, expire, and write OAuth token entries in ~/.claudewheel/tokens.json.
#parse_entry
def parse_entry(entry: object) -> str | NoneExtract the token string from a tokens.json entry.
Supports both formats: a bare string, or a dict like {"token": ..., "created": ..., "expires_at": ...}. Returns None if the entry is empty, absent, or unrecognized.
#TokenExpiry
Computed token lifetime: creation date, expiry date, days remaining.
#compute_expiry
def compute_expiry(entry: object, tokens_mtime: float, today: date | None=None) -> TokenExpiryCompute a token entry's creation date, expiry date, and remaining days.
Precedence: explicit "expires_at" ISO date; else "created" + TOKEN_TTL_DAYS; else (legacy bare-string entry) the tokens.json file mtime + TOKEN_TTL_DAYS. Unparseable or absent dict fields yield (None, None, TOKEN_TTL_DAYS), matching the historical health-check behavior of assuming a fresh token.
#add_token
def add_token(name: str, token: str, *, tier: str | None=None, subscription: str | None=None) -> NoneAdd or update a profile's OAuth token in tokens.json.
Writes token, created (today), and expires_at (created + TOKEN_TTL_DAYS). Optionally stores rateLimitTier and subscriptionType when provided. The file always ends up with 0600 permissions (it holds secrets). Writes atomically via tmp-file rename.
Raises OSError if an existing tokens.json cannot be parsed -- a corrupt file is never silently overwritten (callers handle OSError).
#store_tier
def store_tier(name: str, *, tier: str | None=None, subscription: str | None=None) -> NoneStore rate-limit tier metadata in tokens.json for a profile.
Creates or updates the entry. If the profile already has a token entry, the tier fields are merged into it. If not, a tier-only entry (no token) is created -- parse_entry returns None for such entries, which is fine.
Raises OSError if tokens.json is corrupt (same contract as add_token).