the37lab-authlib 0.1.1750952955__tar.gz → 0.1.1751369506__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.1750952955 → the37lab_authlib-0.1.1751369506}/PKG-INFO +30 -1
  2. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/README.md +29 -0
  3. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/pyproject.toml +1 -1
  4. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/auth.py +35 -1
  5. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib.egg-info/PKG-INFO +30 -1
  6. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/setup.cfg +0 -0
  7. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/__init__.py +0 -0
  8. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/db.py +0 -0
  9. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/decorators.py +0 -0
  10. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/exceptions.py +0 -0
  11. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib/models.py +0 -0
  12. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib.egg-info/SOURCES.txt +0 -0
  13. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib.egg-info/dependency_links.txt +0 -0
  14. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/src/the37lab_authlib.egg-info/requires.txt +0 -0
  15. {the37lab_authlib-0.1.1750952955 → the37lab_authlib-0.1.1751369506}/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.1750952955
3
+ Version: 0.1.1751369506
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,9 @@ 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)
42
45
 
43
46
  ## Installation
44
47
 
@@ -54,6 +57,7 @@ from authlib import AuthManager
54
57
 
55
58
  app = Flask(__name__)
56
59
 
60
+ # Option 1: Explicit configuration
57
61
  auth = AuthManager(
58
62
  app=app,
59
63
  db_dsn="postgresql://user:pass@localhost/dbname",
@@ -66,6 +70,11 @@ auth = AuthManager(
66
70
  }
67
71
  )
68
72
 
73
+ # Option 2: Use environment variables with a prefix (e.g., AMPA_)
74
+ # This will load:
75
+ # AMPA_DATABASE_URL, AMPA_JWT_SECRET, AMPA_GOOGLE_CLIENT_ID, AMPA_GOOGLE_CLIENT_SECRET
76
+ # auth = AuthManager(app=app, environment_prefix="AMPA")
77
+
69
78
  @app.route("/protected")
70
79
  @auth.require_auth(roles=["admin"])
71
80
  def protected_route():
@@ -96,6 +105,7 @@ public using the `@auth.public_endpoint` decorator or
96
105
  - `oauth_config`: Dictionary of OAuth provider configurations (see below)
97
106
  - `token_expiry`: JWT token expiry time in seconds (default: 3600)
98
107
  - `refresh_token_expiry`: Refresh token expiry time in seconds (default: 2592000)
108
+ - `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.
99
109
 
100
110
  #### Example `oauth_config`:
101
111
  ```python
@@ -204,3 +214,22 @@ python -m authlib.cli db init
204
214
  ```bash
205
215
  pytest
206
216
  ```
217
+
218
+ ## API Token Override for Testing
219
+
220
+ 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.
221
+
222
+ ### Usage
223
+
224
+ - **Constructor argument:**
225
+ ```python
226
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
227
+ ```
228
+ - **Environment variable:**
229
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
230
+ ```
231
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
232
+ ```
233
+ Replace `MYAPP` with your environment prefix.
234
+
235
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.
@@ -22,6 +22,9 @@ 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)
25
28
 
26
29
  ## Installation
27
30
 
@@ -37,6 +40,7 @@ from authlib import AuthManager
37
40
 
38
41
  app = Flask(__name__)
39
42
 
43
+ # Option 1: Explicit configuration
40
44
  auth = AuthManager(
41
45
  app=app,
42
46
  db_dsn="postgresql://user:pass@localhost/dbname",
@@ -49,6 +53,11 @@ auth = AuthManager(
49
53
  }
50
54
  )
51
55
 
56
+ # Option 2: Use environment variables with a prefix (e.g., AMPA_)
57
+ # This will load:
58
+ # AMPA_DATABASE_URL, AMPA_JWT_SECRET, AMPA_GOOGLE_CLIENT_ID, AMPA_GOOGLE_CLIENT_SECRET
59
+ # auth = AuthManager(app=app, environment_prefix="AMPA")
60
+
52
61
  @app.route("/protected")
53
62
  @auth.require_auth(roles=["admin"])
54
63
  def protected_route():
@@ -79,6 +88,7 @@ public using the `@auth.public_endpoint` decorator or
79
88
  - `oauth_config`: Dictionary of OAuth provider configurations (see below)
80
89
  - `token_expiry`: JWT token expiry time in seconds (default: 3600)
81
90
  - `refresh_token_expiry`: Refresh token expiry time in seconds (default: 2592000)
91
+ - `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.
82
92
 
83
93
  #### Example `oauth_config`:
84
94
  ```python
@@ -187,3 +197,22 @@ python -m authlib.cli db init
187
197
  ```bash
188
198
  pytest
189
199
  ```
200
+
201
+ ## API Token Override for Testing
202
+
203
+ 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.
204
+
205
+ ### Usage
206
+
207
+ - **Constructor argument:**
208
+ ```python
209
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
210
+ ```
211
+ - **Environment variable:**
212
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
213
+ ```
214
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
215
+ ```
216
+ Replace `MYAPP` with your environment prefix.
217
+
218
+ **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.1750952955"
7
+ version = "0.1.1751369506"
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,29 @@ 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'):
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
+ if environment_prefix:
21
+ prefix = environment_prefix.upper() + '_'
22
+ db_dsn = os.getenv(f'{prefix}DATABASE_URL')
23
+ jwt_secret = os.getenv(f'{prefix}JWT_SECRET')
24
+ google_client_id = os.getenv(f'{prefix}GOOGLE_CLIENT_ID')
25
+ google_client_secret = os.getenv(f'{prefix}GOOGLE_CLIENT_SECRET')
26
+ oauth_config = {}
27
+ if google_client_id and google_client_secret:
28
+ oauth_config['google'] = {
29
+ 'client_id': google_client_id,
30
+ 'client_secret': google_client_secret
31
+ }
32
+ api_tokens_env = os.getenv(f'{prefix}API_TOKENS')
33
+ if api_tokens_env:
34
+ api_tokens = {}
35
+ for entry in api_tokens_env.split(','):
36
+ if ':' in entry:
37
+ key, user = entry.split(':', 1)
38
+ api_tokens[key.strip()] = user.strip()
39
+ if api_tokens and db_dsn:
40
+ raise ValueError('Cannot set both api_tokens and db_dsn')
41
+ self.api_tokens = api_tokens or None
20
42
  self.db = Database(db_dsn, id_type=id_type) if db_dsn else None
21
43
  self.jwt_secret = jwt_secret
22
44
  self.oauth_config = oauth_config or {}
@@ -49,6 +71,18 @@ class AuthManager:
49
71
  return redirect_uri
50
72
 
51
73
  def _validate_api_token(self, api_token):
74
+ if self.api_tokens is not None:
75
+ username = self.api_tokens.get(api_token)
76
+ if not username:
77
+ raise AuthError('Invalid API token')
78
+ # Return a minimal user dict
79
+ return {
80
+ 'id': username,
81
+ 'username': username,
82
+ 'email': '',
83
+ 'real_name': username,
84
+ 'roles': []
85
+ }
52
86
  try:
53
87
  parsed = ApiToken.parse_token(api_token)
54
88
  with self.db.get_cursor() as cur:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: the37lab_authlib
3
- Version: 0.1.1750952955
3
+ Version: 0.1.1751369506
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,9 @@ 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)
42
45
 
43
46
  ## Installation
44
47
 
@@ -54,6 +57,7 @@ from authlib import AuthManager
54
57
 
55
58
  app = Flask(__name__)
56
59
 
60
+ # Option 1: Explicit configuration
57
61
  auth = AuthManager(
58
62
  app=app,
59
63
  db_dsn="postgresql://user:pass@localhost/dbname",
@@ -66,6 +70,11 @@ auth = AuthManager(
66
70
  }
67
71
  )
68
72
 
73
+ # Option 2: Use environment variables with a prefix (e.g., AMPA_)
74
+ # This will load:
75
+ # AMPA_DATABASE_URL, AMPA_JWT_SECRET, AMPA_GOOGLE_CLIENT_ID, AMPA_GOOGLE_CLIENT_SECRET
76
+ # auth = AuthManager(app=app, environment_prefix="AMPA")
77
+
69
78
  @app.route("/protected")
70
79
  @auth.require_auth(roles=["admin"])
71
80
  def protected_route():
@@ -96,6 +105,7 @@ public using the `@auth.public_endpoint` decorator or
96
105
  - `oauth_config`: Dictionary of OAuth provider configurations (see below)
97
106
  - `token_expiry`: JWT token expiry time in seconds (default: 3600)
98
107
  - `refresh_token_expiry`: Refresh token expiry time in seconds (default: 2592000)
108
+ - `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.
99
109
 
100
110
  #### Example `oauth_config`:
101
111
  ```python
@@ -204,3 +214,22 @@ python -m authlib.cli db init
204
214
  ```bash
205
215
  pytest
206
216
  ```
217
+
218
+ ## API Token Override for Testing
219
+
220
+ 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.
221
+
222
+ ### Usage
223
+
224
+ - **Constructor argument:**
225
+ ```python
226
+ AuthManager(api_tokens={"token1": "user1", "token2": "user2"})
227
+ ```
228
+ - **Environment variable:**
229
+ Set `{PREFIX}API_TOKENS` to a comma-separated list of `token:username` pairs, e.g.:
230
+ ```
231
+ export MYAPP_API_TOKENS="token1:user1,token2:user2"
232
+ ```
233
+ Replace `MYAPP` with your environment prefix.
234
+
235
+ **Warning:** This method is intended only for testing and development. Do not use this approach in production environments.