belgie 0.1.0a4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- belgie/__init__.py +97 -0
- belgie/alchemy.py +24 -0
- belgie/auth/__init__.py +65 -0
- belgie/auth/core/__init__.py +0 -0
- belgie/auth/core/auth.py +368 -0
- belgie/auth/core/client.py +204 -0
- belgie/auth/core/exceptions.py +26 -0
- belgie/auth/core/hooks.py +87 -0
- belgie/auth/core/settings.py +100 -0
- belgie/auth/providers/__init__.py +10 -0
- belgie/auth/providers/google.py +284 -0
- belgie/auth/providers/protocols.py +43 -0
- belgie/auth/py.typed +0 -0
- belgie/auth/session/__init__.py +3 -0
- belgie/auth/session/manager.py +168 -0
- belgie/auth/utils/__init__.py +9 -0
- belgie/auth/utils/crypto.py +10 -0
- belgie/auth/utils/scopes.py +49 -0
- belgie/mcp.py +12 -0
- belgie/oauth.py +12 -0
- belgie/proto.py +22 -0
- belgie-0.1.0a4.dist-info/METADATA +231 -0
- belgie-0.1.0a4.dist-info/RECORD +24 -0
- belgie-0.1.0a4.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: belgie
|
|
3
|
+
Version: 0.1.0a4
|
|
4
|
+
Summary: Modern authentication for FastAPI
|
|
5
|
+
Author: Matt LeMay
|
|
6
|
+
Author-email: Matt LeMay <mplemay@users.noreply.github.com>
|
|
7
|
+
Requires-Dist: belgie-proto
|
|
8
|
+
Requires-Dist: fastapi>=0.100
|
|
9
|
+
Requires-Dist: httpx>=0.24
|
|
10
|
+
Requires-Dist: pydantic>=2.0
|
|
11
|
+
Requires-Dist: pydantic-settings>=2.0
|
|
12
|
+
Requires-Dist: python-multipart>=0.0.20
|
|
13
|
+
Requires-Dist: belgie-alchemy ; extra == 'alchemy'
|
|
14
|
+
Requires-Dist: belgie-alchemy ; extra == 'all'
|
|
15
|
+
Requires-Dist: belgie-mcp ; extra == 'all'
|
|
16
|
+
Requires-Dist: belgie-oauth ; extra == 'all'
|
|
17
|
+
Requires-Dist: uvicorn[standard]>=0.38.0 ; extra == 'examples'
|
|
18
|
+
Requires-Dist: belgie-mcp ; extra == 'mcp'
|
|
19
|
+
Requires-Dist: belgie-oauth ; extra == 'oauth'
|
|
20
|
+
Requires-Python: >=3.12, <3.15
|
|
21
|
+
Provides-Extra: alchemy
|
|
22
|
+
Provides-Extra: all
|
|
23
|
+
Provides-Extra: examples
|
|
24
|
+
Provides-Extra: mcp
|
|
25
|
+
Provides-Extra: oauth
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# Belgie
|
|
29
|
+
|
|
30
|
+
Self-hosted, type-safe authentication for FastAPI that makes Google OAuth and secure session cookies work with almost
|
|
31
|
+
zero glue code. Keep your data, skip per-user SaaS bills, and still get a polished developer experience.
|
|
32
|
+
|
|
33
|
+
## Who this is for
|
|
34
|
+
|
|
35
|
+
- FastAPI teams that want Google sign-in and protected routes today, not after weeks of wiring.
|
|
36
|
+
- Product engineers who prefer first-class type hints and adapter-driven design over magic.
|
|
37
|
+
- Startups that would rather own their user data and avoid per-MAU pricing from hosted identity vendors.
|
|
38
|
+
|
|
39
|
+
## What it solves
|
|
40
|
+
|
|
41
|
+
- End-to-end Google OAuth 2.0 flow with CSRF-safe state storage.
|
|
42
|
+
- Sliding-window, signed session cookies (no JWT juggling required).
|
|
43
|
+
- Drop-in FastAPI dependencies for `auth.user`, `auth.session`, and scoped access.
|
|
44
|
+
- A thin SQLAlchemy adapter that works with your existing models.
|
|
45
|
+
- Hooks so you can plug in logging, analytics, or audit trails without forking.
|
|
46
|
+
|
|
47
|
+
## How it compares
|
|
48
|
+
|
|
49
|
+
- **fastapi-users**: feature-rich but now in maintenance mode and optimized for password-plus-OAuth flows. Belgie
|
|
50
|
+
focuses on OAuth + session UX, keeps the surface area small, and ships type-driven adapters out of the box.
|
|
51
|
+
- **Hosted identity (Auth0, Clerk, Supabase Auth)**: great UIs and more providers, but billed per Monthly Active User
|
|
52
|
+
and hosted off your stack. Belgie is MIT-licensed, runs in your app, and never charges per user.
|
|
53
|
+
|
|
54
|
+
## Features at a glance
|
|
55
|
+
|
|
56
|
+
- Google OAuth provider with ready-made router (`/auth/signin/google`, `/auth/callback/google`, `/auth/signout`).
|
|
57
|
+
- Session manager with sliding expiry and secure cookie defaults (HttpOnly, SameSite, Secure).
|
|
58
|
+
- Scope-aware dependency for route protection (`Security(auth.user, scopes=[...])`).
|
|
59
|
+
- Modern Python (3.12+), full typing, and protocol-based models.
|
|
60
|
+
- Event hooks and utility helpers for custom workflows.
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install belgie
|
|
66
|
+
# or with uv
|
|
67
|
+
uv add belgie
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
For SQLAlchemy adapter support:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install belgie[alchemy]
|
|
74
|
+
# or with uv
|
|
75
|
+
uv add belgie[alchemy]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Optional extras: `belgie[mcp]`, `belgie[oauth]`, or `belgie[all]`.
|
|
79
|
+
|
|
80
|
+
## Quick start
|
|
81
|
+
|
|
82
|
+
### 1) Define models
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from datetime import UTC, datetime
|
|
86
|
+
from uuid import UUID, uuid4
|
|
87
|
+
from sqlalchemy import ForeignKey, String
|
|
88
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class Base(DeclarativeBase):
|
|
92
|
+
pass
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class User(Base):
|
|
96
|
+
__tablename__ = "users"
|
|
97
|
+
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
|
|
98
|
+
email: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
|
99
|
+
name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
100
|
+
image: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
|
101
|
+
email_verified: Mapped[bool] = mapped_column(default=False)
|
|
102
|
+
created_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
103
|
+
updated_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Account(Base):
|
|
107
|
+
__tablename__ = "accounts"
|
|
108
|
+
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
|
|
109
|
+
user_id: Mapped[UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
110
|
+
provider: Mapped[str] = mapped_column(String(50))
|
|
111
|
+
provider_account_id: Mapped[str] = mapped_column(String(255))
|
|
112
|
+
access_token: Mapped[str | None] = mapped_column(String(1000), nullable=True)
|
|
113
|
+
refresh_token: Mapped[str | None] = mapped_column(String(1000), nullable=True)
|
|
114
|
+
expires_at: Mapped[datetime | None] = mapped_column(nullable=True)
|
|
115
|
+
scope: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
|
116
|
+
created_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class Session(Base):
|
|
120
|
+
__tablename__ = "sessions"
|
|
121
|
+
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
|
|
122
|
+
user_id: Mapped[UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
123
|
+
expires_at: Mapped[datetime] = mapped_column(index=True)
|
|
124
|
+
created_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class OAuthState(Base):
|
|
128
|
+
__tablename__ = "oauth_states"
|
|
129
|
+
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
|
|
130
|
+
state: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
|
131
|
+
expires_at: Mapped[datetime] = mapped_column(index=True)
|
|
132
|
+
created_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 2) Configure Belgie
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from belgie.auth import Auth, AuthSettings, GoogleProviderSettings
|
|
139
|
+
from belgie_alchemy import AlchemyAdapter
|
|
140
|
+
|
|
141
|
+
settings = AuthSettings(
|
|
142
|
+
secret="your-secret-key",
|
|
143
|
+
base_url="http://localhost:8000",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
adapter = AlchemyAdapter(
|
|
147
|
+
user=User,
|
|
148
|
+
account=Account,
|
|
149
|
+
session=Session,
|
|
150
|
+
oauth_state=OAuthState,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
auth = Auth(
|
|
154
|
+
settings=settings,
|
|
155
|
+
adapter=adapter,
|
|
156
|
+
providers={
|
|
157
|
+
"google": GoogleProviderSettings(
|
|
158
|
+
client_id="your-google-client-id",
|
|
159
|
+
client_secret="your-google-client-secret",
|
|
160
|
+
redirect_uri="http://localhost:8000/auth/provider/google/callback",
|
|
161
|
+
scopes=["openid", "email", "profile"],
|
|
162
|
+
),
|
|
163
|
+
},
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 3) Add routes to FastAPI
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from fastapi import Depends, FastAPI, Security
|
|
171
|
+
|
|
172
|
+
app = FastAPI()
|
|
173
|
+
app.include_router(auth.router)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@app.get("/")
|
|
177
|
+
async def home():
|
|
178
|
+
return {"message": "Welcome! Visit /auth/provider/google/signin to sign in"}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@app.get("/protected")
|
|
182
|
+
async def protected(user: User = Depends(auth.user)):
|
|
183
|
+
return {"email": user.email}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@app.get("/profile")
|
|
187
|
+
async def profile(user: User = Security(auth.user, scopes=["profile"])):
|
|
188
|
+
return {"name": user.name, "email": user.email}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Run it:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
uvicorn main:app --reload
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Visit `http://localhost:8000/auth/signin/google` to sign in.
|
|
198
|
+
|
|
199
|
+
## Configuration shortcuts
|
|
200
|
+
|
|
201
|
+
- Environment variables: `BELGIE_SECRET`, `BELGIE_BASE_URL`, `BELGIE_GOOGLE_CLIENT_ID`, `BELGIE_GOOGLE_CLIENT_SECRET`,
|
|
202
|
+
`BELGIE_GOOGLE_REDIRECT_URI` (loaded automatically by `AuthSettings()`).
|
|
203
|
+
- Session tuning: `SessionSettings(cookie_name, max_age, update_age)` controls lifetime and sliding refresh.
|
|
204
|
+
- Cookie hardening: `CookieSettings(http_only, secure, same_site)` for production-ready defaults.
|
|
205
|
+
|
|
206
|
+
## Router endpoints
|
|
207
|
+
|
|
208
|
+
- `GET /auth/signin/google` – start OAuth flow
|
|
209
|
+
- `GET /auth/callback/google` – handle Google callback
|
|
210
|
+
- `POST /auth/signout` – clear session cookie and invalidate server session
|
|
211
|
+
|
|
212
|
+
## Limitations today
|
|
213
|
+
|
|
214
|
+
- Google is the only built-in provider; more providers and email/password are on the roadmap.
|
|
215
|
+
- You manage your own database migrations and deployment (by design—no third-party control plane).
|
|
216
|
+
|
|
217
|
+
## Why teams pick Belgie
|
|
218
|
+
|
|
219
|
+
- Keep control of data and infra while getting a batteries-included OAuth flow.
|
|
220
|
+
- Minimal surface area: a single `Auth` instance exposes router + dependencies.
|
|
221
|
+
- Modern typing and clear protocols reduce integration mistakes and make refactors safer.
|
|
222
|
+
- MIT license, zero per-user costs.
|
|
223
|
+
|
|
224
|
+
## Documentation and examples
|
|
225
|
+
|
|
226
|
+
- [docs/quickstart.md](docs/quickstart.md) for full walkthrough
|
|
227
|
+
- [examples/auth](examples/auth) for a runnable app
|
|
228
|
+
|
|
229
|
+
## Contributing
|
|
230
|
+
|
|
231
|
+
MIT licensed. Issues and PRs welcome.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
belgie/__init__.py,sha256=LnGyYbGGkEt0UAb_Y-gHRcRSFECPNsTFca7xTZttPmM,2093
|
|
2
|
+
belgie/alchemy.py,sha256=8EywiqMMDYkLQ0WvnnxEKaVo2BQhAqq6R0NyIFkA9dI,595
|
|
3
|
+
belgie/auth/__init__.py,sha256=NPl9oQBF8TVLxFEz6tzdCBf2eHbLtpDevoDz8H14zsA,1680
|
|
4
|
+
belgie/auth/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
belgie/auth/core/auth.py,sha256=CnkT6SSAjt2J-dAL2ye8-1aiILSM-tVPRXbReihVbwA,13335
|
|
6
|
+
belgie/auth/core/client.py,sha256=foNqGInCuWM-neSCc63bsGePTd8IB-RxM1gMDpui26c,7102
|
|
7
|
+
belgie/auth/core/exceptions.py,sha256=KF_pBbw-KJcrn5S6qCTa55YMcwRLETc5vEwXl4EmGwk,340
|
|
8
|
+
belgie/auth/core/hooks.py,sha256=mjksSE5YxjD-0WIn_mGIOl6DA_3socxoN1IpIrzo6uU,2993
|
|
9
|
+
belgie/auth/core/settings.py,sha256=SN-ChAVFSpgyBbWtTrdpNkBamjY1XjnbxqHCp5Mjg8U,3258
|
|
10
|
+
belgie/auth/providers/__init__.py,sha256=N0E7Kv_-Hc7PE0C-S2cS5uQDt9BXxRJHbYToUlipniE,318
|
|
11
|
+
belgie/auth/providers/google.py,sha256=MfHBcXlbyOKBP97fZPiSj76ct53sToXdqZbXcZAv6Aw,11411
|
|
12
|
+
belgie/auth/providers/protocols.py,sha256=1EeBmRDDbiYkITDE-EMNlddp-fUjkkq1NV1Nz0BUPlg,1312
|
|
13
|
+
belgie/auth/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
belgie/auth/session/__init__.py,sha256=p7DWWy_TMMWYgg-ltZn8lFJgHJXi_yPWhgjE54i7ptk,85
|
|
15
|
+
belgie/auth/session/manager.py,sha256=XLUyihJTpvcRmsPa2Nidc2GuxyNB7cVhHqPABuZvcXM,5353
|
|
16
|
+
belgie/auth/utils/__init__.py,sha256=VqaeGLT2v-giVLOXOWAJ8gFu89IqMiknXXHpBKIUijU,259
|
|
17
|
+
belgie/auth/utils/crypto.py,sha256=mtV3HNIWu1hDw7JaKqUWMdWi6_x2crEY5sg49AoUL3U,174
|
|
18
|
+
belgie/auth/utils/scopes.py,sha256=OIdI039U7uB9w7Y91QNuY05PBSrtcZz0ruqIY0Fvyik,1598
|
|
19
|
+
belgie/mcp.py,sha256=FS3K9jE883yH8pA4agkvI5r_WukJCI2X4j1utwJs48A,322
|
|
20
|
+
belgie/oauth.py,sha256=ArGIJUnAlPz3Xa4Tiyzo0nYUC9Vuv7R2XEGxgfS-Uuc,336
|
|
21
|
+
belgie/proto.py,sha256=gRSvV5DGulNFFzPnC983jEKlbj7oUdG9Gk259qJ_rqE,562
|
|
22
|
+
belgie-0.1.0a4.dist-info/WHEEL,sha256=fAguSjoiATBe7TNBkJwOjyL1Tt4wwiaQGtNtjRPNMQA,80
|
|
23
|
+
belgie-0.1.0a4.dist-info/METADATA,sha256=dD2y0UNM05GVCrdYBgleM00VtMnZ45LWOgSsLQrdCiU,7971
|
|
24
|
+
belgie-0.1.0a4.dist-info/RECORD,,
|