the37lab-authlib 0.1.1751357568__tar.gz → 0.1.1751371611__tar.gz

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.

Files changed (15) hide show
  1. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/PKG-INFO +38 -1
  2. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/README.md +37 -0
  3. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/pyproject.toml +1 -1
  4. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/auth.py +37 -1
  5. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib.egg-info/PKG-INFO +38 -1
  6. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/setup.cfg +0 -0
  7. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/__init__.py +0 -0
  8. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/db.py +0 -0
  9. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/decorators.py +0 -0
  10. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/exceptions.py +0 -0
  11. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib/models.py +0 -0
  12. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib.egg-info/SOURCES.txt +0 -0
  13. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib.egg-info/dependency_links.txt +0 -0
  14. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib.egg-info/requires.txt +0 -0
  15. {the37lab_authlib-0.1.1751357568 → the37lab_authlib-0.1.1751371611}/src/the37lab_authlib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: the37lab_authlib
3
- Version: 0.1.1751357568
3
+ Version: 0.1.1751371611
4
4
  Summary: Python SDK for the Authlib
5
5
  Author-email: the37lab <info@the37lab.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -39,6 +39,12 @@ A Python authentication library that provides JWT, OAuth2, and API token authent
39
39
  - [Setup](#setup)
40
40
  - [Database Setup](#database-setup)
41
41
  - [Running Tests](#running-tests)
42
+ - [API Token Override for Testing](#api-token-override-for-testing)
43
+ - [Usage](#usage)
44
+ - [Warning](#warning)
45
+ - [User Override for Testing](#user-override-for-testing)
46
+ - [Usage](#usage-1)
47
+ - [Warning](#warning-1)
42
48
 
43
49
  ## Installation
44
50
 
@@ -211,3 +217,34 @@ python -m authlib.cli db init
211
217
  ```bash
212
218
  pytest
213
219
  ```
220
+
221
+ ## API Token Override for Testing
222
+
223
+ 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.
224
+
225
+ ### Usage
226
+
227
+ - **Constructor argument:**
228
+ ```python
229
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
230
+ ```
231
+ - **Environment variable:**
232
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
233
+ ```
234
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
235
+ ```
236
+ Replace `MYAPP` with your environment prefix.
237
+
238
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
239
+
240
+ ## User Override for Testing
241
+
242
+ For testing purposes, you can force all authentication to return a specific user by setting the `{PREFIX}USER_OVERRIDE` environment variable:
243
+
244
+ ```bash
245
+ export MYAPP_USER_OVERRIDE="testuser"
246
+ ```
247
+
248
+ 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`.
249
+
250
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
@@ -22,6 +22,12 @@ A Python authentication library that provides JWT, OAuth2, and API token authent
22
22
  - [Setup](#setup)
23
23
  - [Database Setup](#database-setup)
24
24
  - [Running Tests](#running-tests)
25
+ - [API Token Override for Testing](#api-token-override-for-testing)
26
+ - [Usage](#usage)
27
+ - [Warning](#warning)
28
+ - [User Override for Testing](#user-override-for-testing)
29
+ - [Usage](#usage-1)
30
+ - [Warning](#warning-1)
25
31
 
26
32
  ## Installation
27
33
 
@@ -194,3 +200,34 @@ python -m authlib.cli db init
194
200
  ```bash
195
201
  pytest
196
202
  ```
203
+
204
+ ## API Token Override for Testing
205
+
206
+ 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.
207
+
208
+ ### Usage
209
+
210
+ - **Constructor argument:**
211
+ ```python
212
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
213
+ ```
214
+ - **Environment variable:**
215
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
216
+ ```
217
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
218
+ ```
219
+ Replace `MYAPP` with your environment prefix.
220
+
221
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
222
+
223
+ ## User Override for Testing
224
+
225
+ For testing purposes, you can force all authentication to return a specific user by setting the `{PREFIX}USER_OVERRIDE` environment variable:
226
+
227
+ ```bash
228
+ export MYAPP_USER_OVERRIDE="testuser"
229
+ ```
230
+
231
+ 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`.
232
+
233
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "the37lab_authlib"
7
- version = "0.1.1751357568"
7
+ version = "0.1.1751371611"
8
8
  description = "Python SDK for the Authlib"
9
9
  authors = [{name = "the37lab", email = "info@the37lab.com"}]
10
10
  dependencies = ["flask", "psycopg2-binary", "pyjwt", "python-dotenv", "requests", "authlib", "bcrypt"]
@@ -16,7 +16,8 @@ logging.basicConfig(level=logging.DEBUG)
16
16
  logger = logging.getLogger(__name__)
17
17
 
18
18
  class AuthManager:
19
- def __init__(self, app=None, db_dsn=None, jwt_secret=None, oauth_config=None, id_type='integer', environment_prefix=None):
19
+ def __init__(self, app=None, db_dsn=None, jwt_secret=None, oauth_config=None, id_type='integer', environment_prefix=None, api_tokens=None):
20
+ self.user_override = None
20
21
  if environment_prefix:
21
22
  prefix = environment_prefix.upper() + '_'
22
23
  db_dsn = os.getenv(f'{prefix}DATABASE_URL')
@@ -29,6 +30,21 @@ class AuthManager:
29
30
  'client_id': google_client_id,
30
31
  'client_secret': google_client_secret
31
32
  }
33
+ api_tokens_env = os.getenv(f'{prefix}API_TOKENS')
34
+ if api_tokens_env:
35
+ api_tokens = {}
36
+ for entry in api_tokens_env.split(','):
37
+ if ':' in entry:
38
+ key, user = entry.split(':', 1)
39
+ api_tokens[key.strip()] = user.strip()
40
+ user_override_env = os.getenv(f'{prefix}USER_OVERRIDE')
41
+ if user_override_env:
42
+ self.user_override = user_override_env
43
+ if self.user_override and (api_tokens or db_dsn):
44
+ raise ValueError('Cannot set user_override together with api_tokens or db_dsn')
45
+ if api_tokens and db_dsn:
46
+ raise ValueError('Cannot set both api_tokens and db_dsn')
47
+ self.api_tokens = api_tokens or None
32
48
  self.db = Database(db_dsn, id_type=id_type) if db_dsn else None
33
49
  self.jwt_secret = jwt_secret
34
50
  self.oauth_config = oauth_config or {}
@@ -61,6 +77,18 @@ class AuthManager:
61
77
  return redirect_uri
62
78
 
63
79
  def _validate_api_token(self, api_token):
80
+ if self.api_tokens is not None:
81
+ username = self.api_tokens.get(api_token)
82
+ if not username:
83
+ raise AuthError('Invalid API token')
84
+ # Return a minimal user dict
85
+ return {
86
+ 'id': username,
87
+ 'username': username,
88
+ 'email': '',
89
+ 'real_name': username,
90
+ 'roles': []
91
+ }
64
92
  try:
65
93
  parsed = ApiToken.parse_token(api_token)
66
94
  with self.db.get_cursor() as cur:
@@ -109,6 +137,14 @@ class AuthManager:
109
137
  raise AuthError('Invalid token format')
110
138
 
111
139
  def _authenticate_request(self):
140
+ if self.user_override:
141
+ return {
142
+ 'id': self.user_override,
143
+ 'username': self.user_override,
144
+ 'email': '',
145
+ 'real_name': self.user_override,
146
+ 'roles': []
147
+ }
112
148
  auth_header = request.headers.get('Authorization')
113
149
  api_token = request.headers.get('X-API-Token')
114
150
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: the37lab_authlib
3
- Version: 0.1.1751357568
3
+ Version: 0.1.1751371611
4
4
  Summary: Python SDK for the Authlib
5
5
  Author-email: the37lab <info@the37lab.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -39,6 +39,12 @@ A Python authentication library that provides JWT, OAuth2, and API token authent
39
39
  - [Setup](#setup)
40
40
  - [Database Setup](#database-setup)
41
41
  - [Running Tests](#running-tests)
42
+ - [API Token Override for Testing](#api-token-override-for-testing)
43
+ - [Usage](#usage)
44
+ - [Warning](#warning)
45
+ - [User Override for Testing](#user-override-for-testing)
46
+ - [Usage](#usage-1)
47
+ - [Warning](#warning-1)
42
48
 
43
49
  ## Installation
44
50
 
@@ -211,3 +217,34 @@ python -m authlib.cli db init
211
217
  ```bash
212
218
  pytest
213
219
  ```
220
+
221
+ ## API Token Override for Testing
222
+
223
+ 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.
224
+
225
+ ### Usage
226
+
227
+ - **Constructor argument:**
228
+ ```python
229
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
230
+ ```
231
+ - **Environment variable:**
232
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
233
+ ```
234
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
235
+ ```
236
+ Replace `MYAPP` with your environment prefix.
237
+
238
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
239
+
240
+ ## User Override for Testing
241
+
242
+ For testing purposes, you can force all authentication to return a specific user by setting the `{PREFIX}USER_OVERRIDE` environment variable:
243
+
244
+ ```bash
245
+ export MYAPP_USER_OVERRIDE="testuser"
246
+ ```
247
+
248
+ 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`.
249
+
250
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.