Metadata-Version: 2.4
Name: konklav-relying
Version: 1.5.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Dist: httpx>=0.28
License-File: LICENSE
Summary: Decide Konklav authorization locally: token verification, cached entitlements and the change feed
License-Expression: MIT
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/cstruct/konklav/releases
Project-URL: Documentation, https://github.com/cstruct/konklav/tree/main/items/py
Project-URL: Issues, https://github.com/cstruct/konklav/issues
Project-URL: Repository, https://github.com/cstruct/konklav

# konklav-relying

Decide [Konklav](https://github.com/cstruct/konklav) authorization locally
from Python.

Konklav is a standalone identity and access service. This package is the
Python SDK for relying systems: services that accept Konklav's tokens and
need authorization decisions for them. It verifies tokens offline, caches
entitlements and keeps them fresh from Konklav's change feed, so most
decisions need no network round trip.

## Installing

```sh
pip install konklav-relying
```

Wheels are published for Python 3.12 and later on:

- Linux x86_64 and aarch64 with glibc 2.28 or later (manylinux_2_28)
- Linux x86_64 and aarch64 with musl 1.2 or later, such as Alpine
  (musllinux_1_2)
- macOS on Apple silicon

Elsewhere pip builds from the sdist, which needs a Rust toolchain.

## Usage

```python
from konklav_relying import RelyingClient

client = RelyingClient(
    relying_url="https://konklav:8081",
    subject_url="https://konklav:8080",
    key_id=key_id,
    secret=secret,
    issuer="https://auth.example",
    audience="api",
)
await client.start()

decision = await client.decide(
    token,
    {"scopes": [{"kind": "project", "id": project_id, "actions": ["document:read"]}]},
)
if decision["decision"] != "allow":
    ...

await client.stop()
```

`RelyingClient` runs on asyncio with httpx:

- It logs in as a service principal and follows the change feed in the
  background, so revocations and entitlement changes take effect within
  about a second.
- It decides locally, and falls back to Konklav's check API whenever its
  caches can't be trusted, for example while the feed is disconnected.
- `KonklavError` means Konklav could not be reached or answered with an
  error. Treat it as a deny.
- `await_cursor` waits until a change you just made through Konklav has
  reached the cache, for read-your-writes.
- `decide_request` and `verify_request` take a request's raw
  `Authorization` and `DPoP` headers, its method and the public URL, so
  sender-constrained (DPoP) tokens work too. A deny with reason
  `invalid_dpop_proof` should be answered with 401 and
  `WWW-Authenticate: DPoP error="invalid_dpop_proof"`.
- `accept_request` verifies a request's token, session and DPoP proof
  once and returns the verified token, and `decide_accepted` then decides
  for it without spending the proof again, for hosts that need the token
  before they know the requirements.

- `Policy` declares an operation's requirements once per route or router,
  merges the policies of nested layers and resolves them per request with
  the scope ids it names. `decide_typed`, `decide_request_typed`,
  `verify_typed` and `verify_request_typed` return a `Decision` and an
  `AccessToken` instead of JSON:

  ```python
  policy = Policy(tenant_actions={"project:read"}, tenant_facts={"tenant_type": ["client"]})
  decision = await client.decide_typed(token, policy.requirements())
  if not decision.allowed:
      ...
  ```

The decision logic lives in a Rust core shared with Konklav's other relying
bindings, so they can't disagree. Python does only the I/O.

## Versions

The package has the same version as the Konklav server it was released
with. Pre-releases, such as `1.3.0rc1`, are installed only with
`pip install --pre`.

## Links

- [Konklav](https://github.com/cstruct/konklav)
- [This binding's README](https://github.com/cstruct/konklav/tree/main/items/py)
- [Relying SDK design](https://github.com/cstruct/konklav/blob/main/docs/architecture/relying-sdk.md)
- [Releases and changelog](https://github.com/cstruct/konklav/releases)
- [Issues](https://github.com/cstruct/konklav/issues)

MIT licensed.

