Metadata-Version: 2.4
Name: fastapi-trusted-devices
Version: 0.1.0
Summary: Trusted-device management and session security for FastAPI applications.
Project-URL: Homepage, https://github.com/javlondevv/fastapi-trusted-devices
Project-URL: Repository, https://github.com/javlondevv/fastapi-trusted-devices
Project-URL: Changelog, https://github.com/javlondevv/fastapi-trusted-devices/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/javlondevv/fastapi-trusted-devices/issues
Author-email: Javlon Baxtiyorov <baxtiyorovjavlon8@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Javlon Baxtiyorov
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: authentication,devices,fastapi,jwt,security,sessions
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
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
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: fastapi>=0.110
Requires-Dist: pydantic>=2.5
Requires-Dist: sqlalchemy[asyncio]>=2.0
Provides-Extra: dev
Requires-Dist: aiosqlite>=0.19; extra == 'dev'
Requires-Dist: httpx>=0.27; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pyjwt>=2.8; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: geo
Requires-Dist: httpx>=0.27; extra == 'geo'
Provides-Extra: jwt
Requires-Dist: pyjwt>=2.8; extra == 'jwt'
Description-Content-Type: text/markdown

# fastapi-trusted-devices

Trusted-device management and session security for **FastAPI**.

Bind every authenticated session to a known device, list and revoke devices,
and detect suspicious activity — without locking your app into a specific auth
library or ORM.

## Status

`0.1.0` — **Alpha.** Core device registry + management endpoints. The public API
may change before `1.0`. See [`CHANGELOG.md`](./CHANGELOG.md) and the
[roadmap](#roadmap).

## Why

FastAPI gives you authentication primitives but no notion of *which device* a
token belongs to. `fastapi-trusted-devices` adds that layer:

- Associate each session with a `device_uid`.
- List a user's active devices and revoke any of them.
- Per-device permissions (who may update/revoke other devices).
- Hooks for "new device", "device revoked", and (from `0.2`) suspicious-login
  and session-hijack events.

It is **auth-agnostic** (you keep your own login/JWT flow) and **storage
abstracted** behind a `DeviceRepository` protocol (SQLAlchemy 2.0 async adapter
included).

## Install

```bash
pip install fastapi-trusted-devices
# optional extras:
pip install "fastapi-trusted-devices[geo]"   # httpx geolocation backend (0.2+)
pip install "fastapi-trusted-devices[jwt]"   # PyJWT token helpers
```

## Quickstart

```python
from fastapi import Depends, FastAPI
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine

from fastapi_trusted_devices import (
    Base,
    TrustedDevices,
    TrustedDeviceConfig,
)

engine = create_async_engine("sqlite+aiosqlite:///./devices.db")
sessionmaker = async_sessionmaker(engine, expire_on_commit=False)

td = TrustedDevices(
    config=TrustedDeviceConfig(max_devices_per_user=10),
    sessionmaker=sessionmaker,
    # tell the library how to identify the caller + their device from a request:
    get_user_id=lambda request: request.headers["x-user-id"],
    get_device_uid=lambda request: request.headers.get("x-device-uid"),
)

app = FastAPI()
app.include_router(td.router, prefix="/trusted-devices", tags=["devices"])
td.install_exception_handlers(app)


@app.on_event("startup")
async def _startup() -> None:
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)


@app.get("/me", dependencies=[Depends(td.require_trusted_device)])
async def me() -> dict[str, str]:
    return {"ok": "this route requires a recognized device"}
```

### Endpoints

| Method | Path | Purpose |
|---|---|---|
| `GET` | `/` | List the current user's devices |
| `PATCH` | `/{device_uid}` | Rename / change permissions of a device |
| `DELETE` | `/{device_uid}` | Revoke a specific device |
| `POST` | `/logout` | Revoke the current device |
| `POST` | `/revoke-all` | Revoke every device except the current one |

## Roadmap

- **0.1** — core registry, CRUD endpoints, dependencies, SQLAlchemy adapter.
- **0.2** — geolocation backend + cache, `X-Forwarded-For` parsing,
  suspicious-login detection.
- **0.3** — concurrent-session/hijack detection, max-device eviction policies,
  rate limiting, PyJWT helpers, docs site.
- **1.0** — API freeze + semver guarantee.

## License

MIT — see [`LICENSE`](./LICENSE).
