scute.tokens.verify(token) checks an access token locally: an RS256 signature against the app's published keys, the expiry, and that the token belongs to this app and to a user. It returns a Session or raises InvalidToken. Verification needs the app id only, not the secret.
from scute import InvalidToken, Scutescute = Scute()try: session = scute.tokens.verify(token)except InvalidToken as e: ... # e.reasonsession.user_idSignature#
scute.tokens.verify(token: str | None, *, remote: bool = False) -> SessionWhat is checked#
- The token has three parts that decode to JSON objects.
- The header's alg is RS256.
- The signature matches one of the app's keys from GET /v1/auth/:app_id/.well-known/jwks.json. With a kid in the header only that key is tried; without one, every key is tried.
- exp is present and not in the past. 30 seconds of clock skew are allowed.
- aid equals the app's public id (app_...).
- The token is a user's session: it has a uuid claim and no m2m claim.
- With remote=True, Scute still knows the session.
Key caching#
| Behavior | Value |
|---|---|
| Keys are re-read after | 10 minutes (Tokens.KEYS_TTL = 600) |
| On a signature that matches no cached key | the keys are re-read, at most once a minute (Tokens.REFETCH_AFTER = 60) |
| Clock skew allowed on exp | 30 seconds (Tokens.LEEWAY = 30) |
| Accepted algorithms | RS256 (Tokens.ALGORITHMS) |
Key rotation needs no action: a token signed with a new key triggers a re-read.
The JWKS fetch is an HTTP call. If it fails, verify raises ConnectionError or APIError, not InvalidToken.
Local or remote#
Local verification cannot see a session that was revoked after the token was issued: the token stays valid until exp. Pass remote=True on routes where a revoked session must fail at once (payments, account changes). It costs one API call per verification.
session = scute.tokens.verify(token, remote=True)With remote=True, a 401, 403 or 404 from Scute raises InvalidToken with reason revoked. Any other API error is raised as APIError.
Failure reasons#
InvalidToken.reason is one of:
| Reason | Cause |
|---|---|
| missing | No token. |
| malformed | Not a JWT, the parts are not JSON objects, or there is no numeric exp. |
| algorithm | The header's alg is not RS256. |
| signature | The signature matches none of the app's keys, also after a re-read. |
| expired | exp plus 30 seconds is in the past. |
| wrong_app | aid is another app's id. |
| not_a_user | A machine-to-machine token (m2m) or no uuid claim. |
| revoked | remote=True and Scute no longer knows the session. |
Session#
Session is a frozen dataclass.
| Field | Type | From |
|---|---|---|
| user_id | str | the uuid claim |
| app_id | str | the aid claim |
| workspace_id | str or None | the wid claim |
| expires_at | float | exp, Unix seconds |
| actor | dict or None | the act claim, only when imp is true |
| claims | dict | every claim in the token |
Support access#
When someone signs in as the user (see support access), the token carries imp and act.
session.impersonated # True when someone else is signed in as this usersession.actor # {"kind": "backend", "email": "support@acme.com"}impersonated reads imp the way Scute's engine does: True, non-zero numbers and strings such as "true", "t" or "yes" count; False, 0, "", "0", "f", "false", "off" and None do not.
Context for authorization checks#
session.authz_context()# {} for a normal session# {"impersonated": True, "actor": {...}} when someone is signed in as the userPass it as context on every authorization check made for the request. A permission marked "not while impersonating" is then refused inside a support session. When the actor is unknown, actor is the string "unknown".
The app's public id#
scute.tokens.public_app_id # "app_..."When app_id already starts with app_, this is the app id. Otherwise the SDK reads GET /v1/apps/:app_id once and caches the id it returns.