Metadata-Version: 2.5
Name: authfn
Version: 0.3.0
Summary: Python authfn kernel — same routes, envelopes, plugins, and OpenAPI as the TypeScript package
Project-URL: Documentation, https://docs.superfunctions.dev/authfn
Project-URL: Repository, https://github.com/21nCo/super-functions
Project-URL: Issues, https://github.com/21nCo/super-functions/issues
Author: 21n
License-Expression: MIT
License-File: LICENSE
Keywords: api-keys,auth,authentication,superfunctions
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: cryptography>=41.0.0
Requires-Dist: idna>=3.4
Requires-Dist: pydantic>=2.5.0
Requires-Dist: superfunctions-core<0.2.0,>=0.1.1
Provides-Extra: dev
Requires-Dist: fastapi>=0.104.0; extra == 'dev'
Requires-Dist: flask>=3.0.0; extra == 'dev'
Requires-Dist: httpx>=0.27.0; extra == 'dev'
Requires-Dist: mypy>=1.7.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.1.0; extra == 'dev'
Requires-Dist: pytest>=7.4.0; extra == 'dev'
Requires-Dist: ruff>=0.1.6; extra == 'dev'
Requires-Dist: superfunctions-fastapi<0.2.0,>=0.1.1; extra == 'dev'
Requires-Dist: superfunctions-flask<0.2.0,>=0.1.1; extra == 'dev'
Provides-Extra: fastapi
Requires-Dist: fastapi>=0.104.0; extra == 'fastapi'
Provides-Extra: flask
Requires-Dist: flask>=3.0.0; extra == 'flask'
Description-Content-Type: text/markdown

# authfn Python SDK

The Python `authfn` package mirrors the TypeScript authfn contract for backend runtimes that want the same routes, schema output, plugin surface, and canonical error envelopes.

## What It Covers

- Browser session routes and cookie handling
- Password sign-up/sign-in and password reset
- Email OTP send/verify
- Google, Apple, and GitHub social OAuth sign-in
- User-owned API keys
- TOTP-based 2FA
- Multi-region lookup and runtime resolution
- Shared OpenAPI generation through the Python `superfunctions.http.openapi` layer

## Quick Start

```python
from authfn import (
    AuthFnConfig,
    authfn_email_otp_plugin,
    authfn_multi_region_plugin,
    authfn_password_plugin,
    authfn_social_oauth_plugin,
    authfn_two_factor_plugin,
    create_authfn,
)
from authfn.plugins.social_oauth import SocialOAuthPluginConfig, SocialProviderConfig

auth = create_authfn(
    AuthFnConfig(
        database=my_database_adapter,
        namespace="authfn",
        plugins=[
            authfn_password_plugin(),
            authfn_email_otp_plugin(
                {
                    "delivery": my_delivery_provider,
                }
            ),
            authfn_social_oauth_plugin(
                SocialOAuthPluginConfig(
                    providers={
                        "google": SocialProviderConfig(
                            client_id="google-client-id",
                            client_secret="google-client-secret",
                            allowlisted_return_to=["https://app.example.com/post-auth"],
                        )
                    },
                    # Secret key (at least 32 bytes) that encrypts stored
                    # provider tokens; required.
                    encryption_key_resolver=lambda key_ref: load_secret(key_ref),
                )
            ),
            authfn_two_factor_plugin(),
            authfn_multi_region_plugin(),
        ],
    )
)
```

After upgrading from a release that stored social OAuth tokens without a configured key, re-encrypt them once with `await SocialOAuthService(config, plugin_config).token_vault.migrate_legacy_tokens()` (when your resolver only knows a new key reference, set the plugin's `encryption_key_ref` to it and pass the same value as `destination_key_ref=`).

Mount the emitted routes through the shared adapters:

- `superfunctions-fastapi`
- `superfunctions-flask`

## Route Surface

- `GET /auth/session`
- `GET /auth/sessions`
- `POST /auth/sign-out`
- `POST /auth/sessions/:sessionId/revoke`
- `POST /auth/sign-up/password`
- `POST /auth/sign-in/password`
- `POST /auth/password/reset/start`
- `POST /auth/password/reset/complete`
- `POST /auth/otp/send`
- `POST /auth/otp/verify`
- `POST /auth/social/start`
- `GET /auth/social/callback/:provider`
- `POST /auth/social/disconnect/:provider`
- `POST /auth/api-keys`
- `GET /auth/api-keys`
- `DELETE /auth/api-keys/:keyId`
- `POST /auth/2fa/enroll`
- `POST /auth/2fa/confirm`
- `POST /auth/2fa/challenge`
- `POST /auth/2fa/disable`
- `POST /auth/regions/lookup`
- `GET /auth/environment`
- `GET /auth/runtime` (compatibility alias for `/auth/environment`)

## OpenAPI

Python authfn uses the shared OpenAPI generator rather than maintaining a custom authfn-specific implementation:

```python
document = auth.open_api()
```

The output is deterministic and matches the TypeScript path surface.

## Notes

- Python authfn follows the same canonical envelopes and error codes as TypeScript.
- FastAPI and Flask cookie propagation is covered by the shared package layer and authfn parity tests.
- Sensitive values such as passwords, OTP codes, API key secrets, and OAuth tokens are omitted or redacted from observability events.
- CI discovers this package from the monorepo root (`.github/workflows/ci.yml`) and runs pytest, ruff, and `python -m build`.

### Awaiting placement verification telemetry

Use `await verifier.verify_signed_async(assertion)` (also available on the issuer) in request-scoped or serverless runtimes. It waits for configured verification event delivery on both success and rejection. The synchronous verification API remains available, but cannot guarantee asynchronous telemetry completes before the request lifetime ends.
