the37lab-authlib 0.1.1758268086__py3-none-any.whl → 0.1.1758535366__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.

Potentially problematic release.


This version of the37lab-authlib might be problematic. Click here for more details.

@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.4
2
+ Name: the37lab_authlib
3
+ Version: 0.1.1758535366
4
+ Summary: Python SDK for the Authlib
5
+ Author-email: the37lab <info@the37lab.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: flask
11
+ Requires-Dist: psycopg2-binary
12
+ Requires-Dist: pyjwt
13
+ Requires-Dist: python-dotenv
14
+ Requires-Dist: requests
15
+ Requires-Dist: authlib
16
+ Requires-Dist: bcrypt
17
+ Requires-Dist: msal
@@ -4,7 +4,7 @@ the37lab_authlib/db.py,sha256=cmnmykKvq6V5e-D0HGiRN4DjFBOGB-SL1HpFjR5uyCw,3162
4
4
  the37lab_authlib/decorators.py,sha256=L-gJUUwDUT2JXTptQ6XEey1LkI5RprbqzEfArWI7F8Y,1305
5
5
  the37lab_authlib/exceptions.py,sha256=mdplK5sKNtagPAzSGq5NGsrQ4r-k03DKJBKx6myWwZc,317
6
6
  the37lab_authlib/models.py,sha256=-PlvQlHGIsSdrH0H9Cdh_vTPlltGV8G1Z1mmGQvAg9Y,3422
7
- the37lab_authlib-0.1.1758268086.dist-info/METADATA,sha256=ELtg_InNIbasakQ8pvlhx3XC3q5nFtIBpJ1PDdi9SgU,8339
8
- the37lab_authlib-0.1.1758268086.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- the37lab_authlib-0.1.1758268086.dist-info/top_level.txt,sha256=6Jmxw4UeLrhfJXgRKbXWY4OhxRSaMs0dKKhNCGWWSwc,17
10
- the37lab_authlib-0.1.1758268086.dist-info/RECORD,,
7
+ the37lab_authlib-0.1.1758535366.dist-info/METADATA,sha256=mxQRaTiocpKcU-BePHUXJHrua5wbmPerI2LDqYBP8Zc,497
8
+ the37lab_authlib-0.1.1758535366.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ the37lab_authlib-0.1.1758535366.dist-info/top_level.txt,sha256=6Jmxw4UeLrhfJXgRKbXWY4OhxRSaMs0dKKhNCGWWSwc,17
10
+ the37lab_authlib-0.1.1758535366.dist-info/RECORD,,
@@ -1,251 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: the37lab_authlib
3
- Version: 0.1.1758268086
4
- Summary: Python SDK for the Authlib
5
- Author-email: the37lab <info@the37lab.com>
6
- Classifier: Programming Language :: Python :: 3
7
- Classifier: Operating System :: OS Independent
8
- Requires-Python: >=3.9
9
- Description-Content-Type: text/markdown
10
- Requires-Dist: flask
11
- Requires-Dist: psycopg2-binary
12
- Requires-Dist: pyjwt
13
- Requires-Dist: python-dotenv
14
- Requires-Dist: requests
15
- Requires-Dist: authlib
16
- Requires-Dist: bcrypt
17
- Requires-Dist: msal
18
-
19
- # AuthLib
20
-
21
- A Python authentication library that provides JWT, OAuth2, and API token authentication with PostgreSQL backend. This library is designed for seamless integration with Flask applications and provides a robust set of endpoints and utilities for user management, authentication, and API token handling.
22
-
23
- ## Table of Contents
24
- - [AuthLib](#authlib)
25
- - [Table of Contents](#table-of-contents)
26
- - [Installation](#installation)
27
- - [Quick Start](#quick-start)
28
- - [Configuration](#configuration)
29
- - [Required Parameters](#required-parameters)
30
- - [Optional Parameters](#optional-parameters)
31
- - [Example `oauth_config`:](#example-oauth_config)
32
- - [API Endpoints](#api-endpoints)
33
- - [Authentication](#authentication)
34
- - [User Management](#user-management)
35
- - [API Tokens](#api-tokens)
36
- - [Authentication Flow](#authentication-flow)
37
- - [User Object](#user-object)
38
- - [Token Management](#token-management)
39
- - [Development](#development)
40
- - [Setup](#setup)
41
- - [Database Setup](#database-setup)
42
- - [Running Tests](#running-tests)
43
- - [API Token Override for Testing](#api-token-override-for-testing)
44
- - [Usage](#usage)
45
- - [Warning](#warning)
46
- - [User Override for Testing](#user-override-for-testing)
47
- - [Usage](#usage-1)
48
- - [Warning](#warning-1)
49
-
50
- ## Installation
51
-
52
- ```bash
53
- pip install -e .
54
- ```
55
-
56
- ## Quick Start
57
-
58
- ```python
59
- from flask import Flask
60
- from authlib import AuthManager
61
-
62
- app = Flask(__name__)
63
-
64
- # Option 1: Explicit configuration
65
- auth = AuthManager(
66
- app=app,
67
- db_dsn="postgresql://user:pass@localhost/dbname",
68
- jwt_secret="your-secret-key",
69
- oauth_config={
70
- "google": {
71
- "client_id": "your-client-id",
72
- "client_secret": "your-client-secret"
73
- }
74
- }
75
- )
76
-
77
- # Option 2: Use environment variables with a prefix (e.g., AMPA_)
78
- # This will load:
79
- # AMPA_DATABASE_URL, AMPA_JWT_SECRET, AMPA_GOOGLE_CLIENT_ID, AMPA_GOOGLE_CLIENT_SECRET
80
- # auth = AuthManager(app=app, environment_prefix="AMPA")
81
-
82
- @app.route("/protected")
83
- @auth.require_auth(roles=["admin"])
84
- def protected_route():
85
- return "Protected content"
86
-
87
- @app.route("/public")
88
- @auth.public_endpoint
89
- def custom_public_route():
90
- return "Public content"
91
- ```
92
-
93
- `AuthManager`'s blueprint now registers a global error handler for
94
- `AuthError` and authenticates requests for all of its routes by default.
95
- Authenticated users are made available as `flask.g.requesting_user`.
96
- Only the login, OAuth, token refresh, registration and role listing
97
- endpoints are exempt from this check. Additional routes can be marked as
98
- public using the `@auth.public_endpoint` decorator or
99
- `auth.add_public_endpoint("auth.some_endpoint")`.
100
-
101
- ## Configuration
102
-
103
- ### Required Parameters
104
- - `app`: Flask application instance
105
- - `db_dsn`: PostgreSQL connection string
106
- - `jwt_secret`: Secret key for JWT signing
107
-
108
- ### Optional Parameters
109
- - `oauth_config`: Dictionary of OAuth provider configurations (see below)
110
- - `token_expiry`: JWT token expiry time in seconds (default: 3600)
111
- - `refresh_token_expiry`: Refresh token expiry time in seconds (default: 2592000)
112
- - `environment_prefix`: If set, loads all configuration from environment variables with this prefix (e.g., `AMPA_DATABASE_URL`, `AMPA_JWT_SECRET`, `AMPA_GOOGLE_CLIENT_ID`, `AMPA_GOOGLE_CLIENT_SECRET`). Overrides other config if set.
113
-
114
- #### Example `oauth_config`:
115
- ```python
116
- {
117
- "google": {
118
- "client_id": "...",
119
- "client_secret": "..."
120
- },
121
- "github": {
122
- "client_id": "...",
123
- "client_secret": "..."
124
- }
125
- }
126
- ```
127
-
128
- ## API Endpoints
129
-
130
- ### Authentication
131
- - `POST /api/v1/users/login` - Login with username/password
132
- - **Request:** `{ "username": "string", "password": "string" }`
133
- - **Response:** `{ "token": "jwt", "refresh_token": "jwt", "user": { ... } }`
134
- - `POST /api/v1/users/login/oauth` - Get OAuth redirect URL
135
- - **Request:** `{ "provider": "google|github|..." }`
136
- - **Response:** `{ "redirect_url": "string" }`
137
- - `GET /api/v1/users/login/oauth2callback` - OAuth callback
138
- - **Query Params:** `code`, `state`, `provider`
139
- - **Response:** `{ "token": "jwt", "refresh_token": "jwt", "user": { ... } }`
140
- - `POST /api/v1/users/token-refresh` - Refresh JWT token
141
- - **Request:** `{ "refresh_token": "jwt" }`
142
- - **Response:** `{ "token": "jwt", "refresh_token": "jwt" }`
143
-
144
- ### User Management
145
- - `POST /api/v1/users/register` - Register new user
146
- - **Request:** `{ "username": "string", "password": "string", "email": "string", ... }`
147
- - **Response:** `{ "user": { ... }, "token": "jwt", "refresh_token": "jwt" }`
148
- - `GET /api/v1/users/login/profile` - Get user profile
149
- - **Auth:** Bearer JWT
150
- - **Response:** `{ "user": { ... } }`
151
- - `GET /api/v1/users/roles` - Get available roles
152
- - **Response:** `[ "admin", "user", ... ]`
153
-
154
- ### API Tokens
155
- - `POST /api/v1/users/{user}/api-tokens` - Create API token
156
- - **Request:** `{ "name": "string", "scopes": [ ... ] }`
157
- - **Response:** `{ "token": "string", "id": "uuid", ... }`
158
- - `GET /api/v1/users/{user}/api-tokens` - List API tokens
159
- - **Response:** `[ { "id": "uuid", "name": "string", ... } ]`
160
- - `DELETE /api/v1/users/{user}/api-tokens/{token_id}` - Delete API token
161
- - **Response:** `{ "success": true }`
162
-
163
- ## Authentication Flow
164
-
165
- 1. **Login:**
166
- - User submits credentials to `/api/v1/users/login`.
167
- - Receives JWT and refresh token.
168
- 2. **Token Refresh:**
169
- - Use `/api/v1/users/token-refresh` with refresh token to get new JWT.
170
- 3. **OAuth:**
171
- - Get redirect URL from `/api/v1/users/login/oauth`.
172
- - Complete OAuth flow via `/api/v1/users/login/oauth2callback`.
173
- 4. **Protected Routes:**
174
- - All routes inside the provided blueprint are authenticated by default.
175
- The authenticated user can be accessed via `g.requesting_user`.
176
- Use `@auth.require_auth()` to protect custom routes in your application.
177
-
178
- ## User Object
179
-
180
- The user object returned by the API typically includes:
181
- ```json
182
- {
183
- "id": "uuid",
184
- "username": "string",
185
- "email": "string",
186
- "roles": ["user", "admin"],
187
- "created_at": "timestamp",
188
- "last_login": "timestamp"
189
- }
190
- ```
191
-
192
- ## Token Management
193
- - **JWT:** Used for authenticating API requests. Include in `Authorization: Bearer <token>` header.
194
- - **Refresh Token:** Used to obtain new JWTs without re-authenticating.
195
- - **API Tokens:** Long-lived tokens for programmatic access, managed per user.
196
-
197
- ## Development
198
-
199
- ### Setup
200
- 1. Clone the repository
201
- 2. Create virtual environment:
202
- ```bash
203
- python -m venv venv
204
- venv\Scripts\activate
205
- ```
206
- 3. Install dependencies:
207
- ```bash
208
- pip install -e ".[dev]"
209
- ```
210
-
211
- ### Database Setup
212
- ```bash
213
- createdb authlib
214
- python -m authlib.cli db init
215
- ```
216
-
217
- ### Running Tests
218
- ```bash
219
- pytest
220
- ```
221
-
222
- ## API Token Override for Testing
223
-
224
- For testing purposes, you can bypass the database and provide a static mapping of API tokens to usernames using the `api_tokens` argument to `AuthManager` or the `{PREFIX}API_TOKENS` environment variable.
225
-
226
- ### Usage
227
-
228
- - **Constructor argument:**
229
- ```python
230
- AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
231
- ```
232
- - **Environment variable:**
233
- Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
234
- ```
235
- export MYAPP_API_TOKENS="token1:user1,token2:user2"
236
- ```
237
- Replace `MYAPP` with your environment prefix.
238
-
239
- **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
240
-
241
- ## User Override for Testing
242
-
243
- For testing purposes, you can force all authentication to return a specific user by setting the `{PREFIX}USER_OVERRIDE` environment variable:
244
-
245
- ```bash
246
- export MYAPP_USER_OVERRIDE="testuser"
247
- ```
248
-
249
- If set, all requests will be authenticated as the specified user, regardless of any tokens or credentials provided. This cannot be combined with `api_tokens` or `db_dsn`.
250
-
251
- **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.