Python SDK

Flask

Verify sessions, require permissions and get a per-request client in Flask with scute.contrib.flask.

scute.contrib.flask.ScuteAuth gives you Flask decorators for the signed-in session and permission checks, and a Scute client that carries the person's IP address.

pip install "scute[flask] @ git+https://github.com/scuteai/scute-python"
app.pypython
from flask import Flask, gfrom scute import Scutefrom scute.contrib.flask import ScuteAuthapp = Flask(__name__)auth = ScuteAuth(Scute())@app.get("/me")@auth.requireddef me():    return {"user_id": g.scute_session.user_id}@app.post("/invoices/<id>/refund")@auth.permission("refund", "invoice:{id}")def refund(id):    return {"refunded": id}

Put the Scute decorators below @app.get / @app.post.

ScuteAuth#

ScuteAuth(scute: Scute, *, remote: bool = False)

remote=True verifies every token with Scute as well, so revoked sessions fail at once. See Verify tokens.

@auth.required#

Verifies the session and stores it on flask.g.scute_session. The token is read from the X-Authorization header, then Authorization: Bearer <token>, then the browser SDK's cookie sc-access-token__<app id>.

A missing or invalid token returns 401:

{ "error": "The token has expired", "reason": "expired" }

reason is one of the failure reasons.

@auth.permission#

auth.permission(action: str, resource: str | None = None)

The decorator:

  1. uses g.scute_session if @auth.required already set it, or verifies the session (401 on failure),
  2. fills resource with the view's URL arguments: "invoice:{id}" becomes "invoice:42",
  3. calls authz.check with session.authz_context() as context, through auth.client(),
  4. returns 403 unless the decision is a plain allow, and otherwise stores it on g.scute_decision and calls the view.
{ "error": "Ada can't delete invoices.", "decision": { "decision": "deny", "reason": "no_role_grants_permission", "permission": "invoice:delete", "explanation": "Ada can't delete invoices." } }

error is the decision's explanation, or Not allowed. decision is the API's full answer. A step-up or approval answer also gets 403; read decision.decision to tell them apart.

Unlike the FastAPI helper, permission takes no extra context. For conditions that need request facts, call auth.client().authz.check(...) in the view with context={**facts, **g.scute_session.authz_context()}.

auth.client()#

Returns a Scute client for the current request. Its secret-key calls send request.remote_addr as X-Scute-Client-IP when that is one IP address; otherwise it returns the shared client. @auth.permission uses it.

@app.post("/signup")def signup():    return auth.client().users.create("ada@example.com")

The helper never reads X-Forwarded-For. Behind a proxy, set up werkzeug's ProxyFix so remote_addr is the person's address. See Client configuration.