Python SDK

Verify access tokens

Check a user's Scute access token on your server against the app's JWKS, and read the session it carries.

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_id

Signature#

scute.tokens.verify(token: str | None, *, remote: bool = False) -> Session
tokenstr | Nonerequired
The access token (a JWT). None or an empty string fails with reason missing.
remotebool
Also ask Scute whether the session is still live (one call to GET /v1/auth/:app_id/current_user). Default False.

What is checked#

  1. The token has three parts that decode to JSON objects.
  2. The header's alg is RS256.
  3. 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.
  4. exp is present and not in the past. 30 seconds of clock skew are allowed.
  5. aid equals the app's public id (app_...).
  6. The token is a user's session: it has a uuid claim and no m2m claim.
  7. With remote=True, Scute still knows the session.

Key caching#

BehaviorValue
Keys are re-read after10 minutes (Tokens.KEYS_TTL = 600)
On a signature that matches no cached keythe keys are re-read, at most once a minute (Tokens.REFETCH_AFTER = 60)
Clock skew allowed on exp30 seconds (Tokens.LEEWAY = 30)
Accepted algorithmsRS256 (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:

ReasonCause
missingNo token.
malformedNot a JWT, the parts are not JSON objects, or there is no numeric exp.
algorithmThe header's alg is not RS256.
signatureThe signature matches none of the app's keys, also after a re-read.
expiredexp plus 30 seconds is in the past.
wrong_appaid is another app's id.
not_a_userA machine-to-machine token (m2m) or no uuid claim.
revokedremote=True and Scute no longer knows the session.

Session#

Session is a frozen dataclass.

FieldTypeFrom
user_idstrthe uuid claim
app_idstrthe aid claim
workspace_idstr or Nonethe wid claim
expires_atfloatexp, Unix seconds
actordict or Nonethe act claim, only when imp is true
claimsdictevery 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 user

Pass 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.