Python SDK

Local decisions

Answer authorization checks in-process from the app's policy snapshot, and fall back to the API when the snapshot cannot decide.

scute.local.decide_locally evaluates a check against a copy of the app's policy, with no network call. It gives the same answers as Scute's engine for everything the policy knows; the SDK's tests run the engine's shared conformance vectors. When it cannot decide, it answers "unknown" and you ask the API.

from scute.local import decide_locallyd = decide_locally(    policy,    roles=["clerk"],    action="approve",    resource={"type": "invoice", "attributes": {"amount": 4200}},)if d.decision == "unknown":    d = scute.authz.check(user_id=user_id, action="approve", resource="invoice:42")

Getting the policy#

The SDK has no method to fetch or verify the snapshot. Read it with the secret key from GET /v1/apps/:app_id/authz/snapshot. The answer has version and token, a JWT signed with the app's keys (verify it against /v1/auth/:app_id/.well-known/jwks.json). Its typ claim is scute-authz-snapshot and its policy claim is the policy to pass to decide_locally.

snapshot = scute.request("GET", scute.apps_path("/authz/snapshot"))# verify snapshot["token"] with the app's JWKS, then:policy = claims["policy"]

Re-read it before the token's exp. The user's roles are not in the snapshot: pass the roles you hold for the user in roles.

Signature#

decide_locally(policy, *, roles, action, resource=None, user=None, context=None, now=None, strict=False) -> LocalDecision
policydictrequired
The snapshot's policy: permissions, roles and optionally resources.
roleslist[str]required
Roles the user holds. Roles marked default in the policy are added automatically; unknown role names are ignored.
actionstrrequired
The action. Trimmed and lowercased.
resourcestr | dict | None
"invoice", "invoice:42", or {"type": ..., "key": ..., "attributes": {...}}.
userdict | None
User attributes for conditions (user.*).
contextdict | None
Request facts (context.*). Include session.authz_context().
nowdatetime | str | None
The time conditions see. An ISO 8601 string or a datetime; default the current time in UTC.
strictbool
When True, a missing attribute fails the condition instead of answering "unknown". Default False.

Conditions also see context.now, context.hour_utc and context.weekday_utc (monday to sunday), derived from now.

LocalDecision#

A frozen dataclass.

FieldType
decision"allow", "deny", "allow_with_step_up", "allow_with_approval" or "unknown"
reasonstr
permissionstr or None
rolestuple[str, ...]: the roles that granted it
allowedproperty, True only for "allow"

Reasons#

ReasonDecision
role_grantallow
verification_requiredallow_with_step_up
approval_requiredallow_with_approval
impersonatingdeny: the permission is blocked while impersonating and context.impersonated is set
unknown_permissiondeny: no such permission
permission_disableddeny
condition_faileddeny
no_role_grants_permissiondeny
needs_serverunknown

When the answer is unknown#

decide_locally answers unknown with reason needs_server when:

  • No role grants the permission, a key is given, and a role on objects of that type grants it (roles on one object live on the server).
  • A condition reads an attribute you did not pass that may exist on the server (user.*, or resource.* when a key is given), and strict is False.

In both cases call scute.authz.check.

Conditions#

scute.local.evaluate_condition(condition, attrs, may_exist_elsewhere=None) evaluates one condition and returns True, False or "unknown". Operators: all, any, not, exists, eq, ne, lt, lte, gt, gte, in, contains, starts_with. Booleans are never equal to numbers, and None counts as missing.

from scute.local import evaluate_conditionevaluate_condition({"gt": [{"var": "resource.amount"}, 1000]}, {"resource": {"amount": 4200}})  # True