Metadata-Version: 2.5
Name: ais-identity-fastapi
Version: 0.1.0
Summary: FastAPI dependencies for Alpin Insight Identity Control Plane (ICp): bearer verification and access requirements over the ais-identity core.
Project-URL: Homepage, https://github.com/alpininsight/insight-identity-sdk
Project-URL: Documentation, https://github.com/alpininsight/insight-identity-sdk/tree/develop/packages/ais-identity-fastapi#readme
Project-URL: Issues, https://github.com/alpininsight/insight-identity-sdk/issues
Author: Alpin Insight Solutions GmbH & Co. KG
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: ais-identity==0.1.0
Requires-Dist: fastapi<1,>=0.115
Description-Content-Type: text/markdown

# ais-identity-fastapi

Part of the Python SDK for **Alpin Insight Identity Control Plane (ICp)**.

FastAPI dependencies over the `ais-identity` core: bearer-token verification
and access requirements for resource servers and MCP backends.

Pre-release: not yet published to PyPI. See the
[repository README](https://github.com/alpininsight/insight-identity-sdk#readme).

## Usage

```python
from typing import Annotated

from ais_identity import AccessContext
from ais_identity_fastapi import Identity
from fastapi import Depends, FastAPI

identity = Identity.from_issuer(
    "https://id.example.com",
    expected_audience="https://api.alpininsight.ai/api/v1/sources",
)
app = FastAPI()


@app.get("/reports")
def reports(
    caller: Annotated[AccessContext, Depends(identity.require_access(any_scopes=["sources:read"]))],
): ...
```

`expected_audience` is the API's canonical resource URI: gateway host plus the
group's catalog `base_path`, compared exactly. Sources is
`https://api.alpininsight.ai/api/v1/sources` (development
`https://api.dev.alpininsight.ai/api/v1/sources`). Digitraffic marine is
`https://api.alpininsight.ai/api/v1/digitraffic/marine`. Sources and
source-control share that sources audience and are distinguished by scope.
The verifier accepts one string or a one-entry array and requires `client_id`.
Multiple audience entries are rejected, including duplicates.

Today, on provider `develop` (`de7c7c9`), every service-API token has `aud`
equal to `client_id`. `resource` binds only MCP scopes. The canonical URI as
`aud` is
[insight-oidc-provider#821](https://github.com/alpininsight/insight-oidc-provider/pull/821)
(still open, `f856b25`) and is not today's behavior. Configuring
`expected_audience` without `legacy_client_audiences` rejects every real
token today.

Pass `legacy_client_audiences` for those client ids
([insight-oidc-provider#819](https://github.com/alpininsight/insight-oidc-provider/issues/819)).
A match also requires `sub == client_id` and `principal_type` `service-client`
([`MACHINE_PRINCIPAL_TYPE`](https://github.com/alpininsight/insight-oidc-provider/blob/de7c7c9f112dbe9ad648b8132e6da731179608d9/src/oidc_core/access_tokens.py#L59),
only on
[`client_credentials`](https://github.com/alpininsight/insight-oidc-provider/blob/de7c7c9f112dbe9ad648b8132e6da731179608d9/src/oidc_core/access_tokens.py#L229-L242)).
A missing claim is never a service client. `workload` from the SPIRE token
exchange, off by default
([`spire_federation.py` L251-L257](https://github.com/alpininsight/insight-oidc-provider/blob/de7c7c9f112dbe9ad648b8132e6da731179608d9/src/oidc_core/spire_federation.py#L251-L257)),
is rejected on purpose even when `aud` equals `client_id`. A non-empty list
emits `PendingDeprecationWarning`, because every resource server needs the
list today. After #821 is deployed that warning becomes `DeprecationWarning`.
Empty the list only then. The parameter stays, so dropping the fallback later
is not a breaking change. There is no slug audience.
`audience=` is deprecated and cannot be combined with `expected_audience`.

`require_access` verifies the bearer token, then decides with the core:

| Outcome | Response |
|---|---|
| no credentials | 401, `WWW-Authenticate: Bearer` |
| invalid token | 401, `Bearer error="invalid_token", error_description="<code>"` |
| missing scope | 403, `Bearer error="insufficient_scope"` |
| any other denial | 403 with the reason code |
| provider unreachable (JWKS) | 503 |

Scopes are read from every scope claim by default; a tenant is required unless
`require_tenant=False`. `node="path:unit"` with `node_action` asks the provider
about the organization unit named by a path parameter. `write=True` applies the
write rule. `Depends(identity.context)` returns the context without deciding.

Dependencies are synchronous on purpose: FastAPI runs them in its thread pool,
so a JWKS fetch never blocks the event loop.

## Gateway headers: opt-in, with a trust check

```python
from ais_identity_fastapi import GatewayHeaders, Identity

identity = Identity(
    verifier=...,
    gateway_headers=GatewayHeaders(
        trusted_peer_ids=[
            "spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account"
        ],
    ),
)
```

Headers are read only when the request comes from a configured gateway:
`trusted_networks` (TCP peer address) and/or `trusted_peer_ids` (SPIFFE ID from
the `x-forwarded-client-cert` header an Envoy sidecar sets under Istio mTLS).
Every configured check must pass. A bearer token always wins and never falls
back to headers. `GatewayHeaders()` without a trust check raises.

## Rollouts

`Enforcement.REPORT_ONLY` logs every denial as an `ais_identity.access_decision`
event and lets the request continue.

## Migrating from insight_service_runtime.authz

`ais_identity_fastapi.compat.require_access(identity, any_of_scopes=..., write=..., mode=...)`
keeps the legacy meaning: any-of scopes from every scope source, a tenant
required, `warn` as report-only, and write violations reported as denials.
`LEGACY_SERVICE_RUNTIME_HEADERS` lists the headers the runtime reads today. The
deliberate change: headers count only from a trusted gateway.
