scute.contrib.fastapi.ScuteAuth gives you FastAPI dependencies for the signed-in session, permission checks and a Scute client that carries the person's IP address.
pip install "scute[fastapi] @ git+https://github.com/scuteai/scute-python"from typing import Annotatedfrom fastapi import Depends, FastAPIfrom scute import Scute, Sessionfrom scute.contrib.fastapi import ScuteAuthapp = FastAPI()auth = ScuteAuth(Scute())@app.get("/me")def me(session: Annotated[Session, Depends(auth.session)]) -> dict: return {"user_id": session.user_id}@app.post("/invoices/{id}/refund", dependencies=[Depends(auth.require("refund", "invoice:{id}"))])def refund(id: str) -> dict: return {"refunded": id}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.session#
A dependency that returns the verified Session. It reads the token from, in order:
- the X-Authorization header (what the Scute frontend SDKs send),
- Authorization: Bearer <token>,
- the browser SDK's cookie sc-access-token__<app id>. The SDK tries the configured app id and, when such a cookie is present, the app's public id.
It stores the session on request.state.scute_session. A missing or invalid token raises HTTPException(401):
{ "detail": { "error": "The token has expired", "reason": "expired" } }reason is one of the failure reasons.
auth.require#
auth.require(action: str, resource: str | None = None, context: Callable[[Request], dict] | None = None)Returns a dependency that:
- uses the session already on request.state, or verifies one (401 on failure),
- fills resource with the path parameters: "invoice:{id}" becomes "invoice:42",
- calls authz.check with the user, action, resource and context, through the per-request client,
- raises HTTPException(403) unless the decision is a plain allow, and returns the Decision otherwise.
The context sent is your context(request) merged with session.authz_context(); the impersonation keys win. A step-up or approval answer gets 403 too:
{ "detail": { "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, so a client can tell allow_with_step_up and allow_with_approval apart.
Use the decision in the route:
from scute import Decision@app.post("/invoices/{id}/approve")def approve(id: str, decision: Annotated[Decision, Depends(auth.require("approve", "invoice:{id}"))]) -> dict: return {"roles": decision.roles}Pass extra context for conditions:
from fastapi import Requestrequire_refund = auth.require("refund", "invoice:{id}", context=lambda request: {"ip_country": request.headers.get("cf-ipcountry")})auth.client#
A dependency that returns a Scute client for the request. Its secret-key calls send request.client.host as X-Scute-Client-IP when that is one IP address; otherwise it returns the shared client unchanged. auth.require uses it for its checks.
@app.post("/signup")def signup(scute: Annotated[Scute, Depends(auth.client)]) -> dict: return scute.users.create("ada@example.com")The helper never reads X-Forwarded-For. Behind a proxy or load balancer, run uvicorn with --proxy-headers and --forwarded-allow-ips set to your proxy, so request.client.host is the person's address. See Client configuration.