Metadata-Version: 2.4
Name: buwigs-auth-sdk
Version: 0.2.0a1
Summary: Buwigs Auth OIDC integration helpers
License-Expression: Apache-2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: PyJWT[crypto]==2.15.1
Requires-Dist: requests==2.34.2
Provides-Extra: fastapi
Requires-Dist: fastapi<1,>=0.115; extra == "fastapi"
Dynamic: license-file

# Buwigs Auth Python SDK

**Version 0.2.0a1 · alpha · Python 3.10+**

This package provides OIDC client and resource-server helpers for Python
applications. It validates Buwigs Auth tokens and returns an immutable
`(issuer, subject)` principal. Your application remains responsible for
mapping that identity to its own account, tenant, and permissions.

## Install

```sh
pip install buwigs-auth-sdk==0.2.0a1
```

FastAPI support is optional:

```sh
pip install 'buwigs-auth-sdk[fastapi]'
```

## Authorization Code + PKCE

Create a client with the exact issuer, client ID, API audience, and registered
redirect URI. Store state, nonce, and the PKCE verifier in a short-lived,
server-side transaction bound to the browser session. Consume the transaction
once in the callback.

```python
from buwigs_auth_sdk import OidcClient

oidc = OidcClient(
    issuer="https://auth.buwigs.com/realms/buwigs-local",
    client_id="your-registered-client-id",
    api_audience="your-api-audience",
    redirect_uri="https://app.example.com/auth/callback",
)
transaction = oidc.authorization_request()
# Save transaction state, nonce, and verifier server-side before redirecting.
```

At the callback, validate state, exchange the code, validate the ID token and
nonce, then establish your application's session:

```python
oidc.validate_state(saved_state, returned_state)
tokens = oidc.exchange_code(code, saved_verifier, client_secret=server_secret)
principal = oidc.verify_id_token(tokens["id_token"], expected_nonce=saved_nonce)
api_principal = oidc.verify_access_token(tokens["access_token"])
```

Only use `client_secret` in a trusted server process. Never send it to browser
or Electron code. Public clients must not have a client secret. Validate the
ID token before creating an application session and validate access tokens on
each protected resource request.

## FastAPI resource-server validation

```python
from fastapi import Depends, FastAPI
from buwigs_auth_sdk import OidcClient, Principal
from buwigs_auth_sdk.fastapi import principal_dependency

oidc = OidcClient(
    issuer="https://auth.buwigs.com/realms/buwigs-local",
    client_id="your-client-id",
    api_audience="your-api-audience",
    redirect_uri="https://app.example.com/auth/callback",
)
require_principal = principal_dependency(oidc)
app = FastAPI()

@app.get("/api/me")
def me(principal: Principal = Depends(require_principal)):
    # Resolve subject in this application's own database and authorize there.
    return {"auth_subject": principal.subject}
```

The dependency validates a Bearer access token against the configured issuer,
RS256 signing keys, and API audience. It returns a generic unauthorized
response for missing or invalid tokens. It does not assign application roles.

## Other OIDC helpers

The client also provides refresh-token exchange, token revocation, UserInfo
subject checking, and an RP-initiated logout URL. Persist rotated refresh
tokens atomically in your server-side session store. The SDK does not store
tokens, create sessions, or implement a login UI.

## Security and support

- Use HTTPS issuer and redirect URLs in deployed applications.
- Bind state, nonce, and PKCE verifier to one browser transaction and consume
  them once.
- Keep confidential client credentials in backend secret storage.
- Do not accept identity claims or roles from request bodies.
- Do not use email or phone as a substitute for the immutable subject.
- Pin the package version and review release notes before upgrading.

The service URL is the intended Buwigs issuer; verify its discovery document
and supported API version before integration. This alpha SDK is not a browser
authentication/session SDK and does not imply a security audit or OIDC
conformance certification.

## License

This SDK source is licensed under Apache-2.0. See the bundled `LICENSE` and
`NOTICE`. Its runtime dependencies are distributed separately under their
respective licenses. The Buwigs Auth server is separate proprietary software.
