Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Fornax Cutouts User Documentation

Building an Auth Provider

This guide walks through implementing an auth provider and registering it with the Fornax Cutouts auth registry, to raise or remove the anonymous cutout limit for known users.


What Is an Auth Provider?

An auth provider is a Python class that answers one question: given a request, who is making it, and what cutout budget should they get?

The framework calls your provider’s resolve() method before dispatching an async job. Everything else — reserving the budget, checking it against the rolling window, reconciling it against the actual cutout count — is handled by Fornax Cutouts.


Step 0: Enable cutout limiting

Limits are disabled until you opt in:

CUTOUTS__CUTOUT_LIMIT__ENABLED=true
CUTOUTS__CUTOUT_LIMIT__ANON_CUTOUT_LIMIT=10
CUTOUTS__CUTOUT_LIMIT__WINDOW_SECONDS=60

If you run behind a load balancer or ingress, also set CUTOUTS__NUM_TRUSTED_PROXIES so anonymous identities use the real client IP (see Auth Providers Overview).


Step 1: Create the Provider File

Create a new .py file anywhere under your CUTOUTS__SOURCE_PATH directory — the same directory used for mission sources:

sources/
├── my_mission.py
└── my_auth_provider.py

The file name does not matter. The registry discovers all *.py files under the configured path recursively, for both mission sources and auth providers.

To keep the provider outside the mission sources directory, point CUTOUTS__CUTOUT_LIMIT__PRINCIPAL_RESOLVER at the file (or at a directory to glob) instead; it is loaded at startup along with the mission sources.

CUTOUTS__CUTOUT_LIMIT__PRINCIPAL_RESOLVER=/app/auth/my_auth_provider.py

Step 2: Implement the Provider Class

from starlette.requests import Request

from fornax_cutouts.auth import AbstractAuthProvider, Principal, auth_registry


@auth_registry.register_provider
class ApiKeyAuthProvider(AbstractAuthProvider):
    name = "api_key"
    anon_limit_hit_info = "Send an X-API-Key header to raise this limit."
    authenticated_limit_hit_info = "Retry after the window resets, or contact support if you need a higher limit."

    # Replace with a real lookup (database, cache, secrets manager, etc.)
    _KNOWN_KEYS = {
        "trusted-partner-key": Principal(identity="trusted-partner", cutout_limit=50_000),
        "internal-key": Principal(identity="internal", cutout_limit=None),  # unlimited
    }

    async def resolve(self, request: Request) -> Principal | None:
        api_key = request.headers.get("x-api-key")
        if api_key is None:
            return None  # Not our concern; fall back to the anonymous default

        return self._KNOWN_KEYS.get(api_key)

Key points


Step 3: Choose a Budget

Principal.cutout_limit fully controls the cutout limit for that identity:

Principal(identity="trusted-partner", cutout_limit=50_000)  # raise the limit
Principal(identity="internal", cutout_limit=None)  # remove the limit entirely

Optionally override the rolling window per identity too:

Principal(identity="burst-tier", cutout_limit=200, window_seconds=60)  # 200 cutouts per minute

Leaving window_seconds=None (the default) uses CUTOUTS__CUTOUT_LIMIT__WINDOW_SECONDS.


Step 4: Handle Real Auth Backends

For a real identity provider (JWT, OAuth introspection, session lookup), do the verification inside resolve() and return None on failure so requests without valid credentials fall back to the anonymous limit instead of erroring:

import jwt
from starlette.requests import Request

from fornax_cutouts.auth import AbstractAuthProvider, Principal, auth_registry


@auth_registry.register_provider
class JwtAuthProvider(AbstractAuthProvider):
    name = "jwt"

    async def resolve(self, request: Request) -> Principal | None:
        auth_header = request.headers.get("authorization", "")
        if not auth_header.startswith("Bearer "):
            return None

        token = auth_header.removeprefix("Bearer ")
        try:
            claims = jwt.decode(token, options={"verify_signature": True}, algorithms=["RS256"])
        except jwt.PyJWTError:
            return None  # Invalid token → anonymous limit, same as missing credentials

        return Principal(
            identity=claims["sub"],
            cutout_limit=claims.get("cutout_limit", 5_000),
        )

For failures that mean your auth backend is broken rather than the caller presenting bad credentials, raise PrincipalResolutionError:

from fornax_cutouts.utils.exceptions import PrincipalResolutionError

raise PrincipalResolutionError(self.name, "user directory unreachable")

Step 5: Verify Registration

Start the API and check the startup logs:

fornax-cutouts api

At info level you should see registration when the module loads:

Registered api_key as the authz provider

At debug level, after discover_sources() completes:

Registered mission sources: ['my_mission']; auth provider: api_key

Step 6: Test the Limit

With cutout limiting enabled:

# Anonymous request — subject to CUTOUTS__CUTOUT_LIMIT__ANON_CUTOUT_LIMIT
curl -i -X POST http://localhost:8000/api/v0/cutouts/async \
  -F "position[]=83.8221,-5.3911" \
  -F "size=500"

# Authenticated request — uses the provider's Principal.cutout_limit
curl -i -X POST http://localhost:8000/api/v0/cutouts/async \
  -H "X-API-Key: trusted-partner-key" \
  -F "position[]=83.8221,-5.3911" \
  -F "size=500"

Once an identity’s budget is exhausted within the window, subsequent requests receive 429 Too Many Requests with a Retry-After header. The body is the limit message. It also includes anon_limit_hit_info or authenticated_limit_hit_info on the next line, when that string is non-empty:

Cutout limit exceeded: 10 used + at least 1 requested > 10 limit (retry after 42s)
Send an X-API-Key header to raise this limit.

If a job passes the initial reservation but the worker discovers more cutouts than expected (e.g. multiple filters per position), reconcile may fail the job with ExecutionPhase.ERROR instead. The client should check GET /cutouts/async/{job_id}/error and retry after the window rolls. That error summary is the same message, including anon_limit_hit_info or authenticated_limit_hit_info.


Step 7: Run the test suite

Auth limiter logic is covered under tests/auth/ (Lua scripts and CutoutLimiter integration). CI runs these against a Valkey service; locally:

# Valkey or Redis on localhost:6379
uv sync --group test
uv run pytest tests/auth/ -v

Checklist