sanic-security 1.12.1__tar.gz → 1.12.3__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.
- {sanic_security-1.12.1/sanic_security.egg-info → sanic_security-1.12.3}/PKG-INFO +7 -15
- {sanic_security-1.12.1 → sanic_security-1.12.3}/README.md +6 -14
- {sanic_security-1.12.1 → sanic_security-1.12.3}/pyproject.toml +1 -1
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/authentication.py +4 -3
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/configuration.py +2 -2
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/models.py +9 -3
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/test/server.py +1 -3
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/test/tests.py +2 -1
- {sanic_security-1.12.1 → sanic_security-1.12.3/sanic_security.egg-info}/PKG-INFO +7 -15
- {sanic_security-1.12.1 → sanic_security-1.12.3}/LICENSE +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/__init__.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/authorization.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/exceptions.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/test/__init__.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/utils.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security/verification.py +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security.egg-info/SOURCES.txt +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security.egg-info/dependency_links.txt +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security.egg-info/requires.txt +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security.egg-info/top_level.txt +0 -0
- {sanic_security-1.12.1 → sanic_security-1.12.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sanic-security
|
3
|
-
Version: 1.12.
|
3
|
+
Version: 1.12.3
|
4
4
|
Summary: An async security library for the Sanic framework.
|
5
5
|
Author-email: Aidan Stewart <na.stewart365@gmail.com>
|
6
6
|
Project-URL: Documentation, https://security.na-stewart.com/
|
@@ -156,7 +156,7 @@ You can load environment variables with a different prefix via `config.load_envi
|
|
156
156
|
| **CAPTCHA_FONT** | captcha-font.ttf | The file path to the font being used for captcha generation. |
|
157
157
|
| **TWO_STEP_SESSION_EXPIRATION** | 200 | The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration. |
|
158
158
|
| **AUTHENTICATION_SESSION_EXPIRATION** | 86400 | The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration. |
|
159
|
-
| **AUTHENTICATION_REFRESH_EXPIRATION** |
|
159
|
+
| **AUTHENTICATION_REFRESH_EXPIRATION** | 604800 | The amount of seconds till authentication refresh expiration. Setting to 0 will disable refresh mechanism. |
|
160
160
|
| **ALLOW_LOGIN_WITH_USERNAME** | False | Allows login via username and email. |
|
161
161
|
| **INITIAL_ADMIN_EMAIL** | admin@example.com | Email used when creating the initial admin account. |
|
162
162
|
| **INITIAL_ADMIN_PASSWORD** | admin123 | Password used when creating the initial admin account. |
|
@@ -295,8 +295,6 @@ async def on_logout(request):
|
|
295
295
|
|
296
296
|
* Authenticate
|
297
297
|
|
298
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
299
|
-
|
300
298
|
```python
|
301
299
|
@app.post("api/security/auth")
|
302
300
|
async def on_authenticate(request):
|
@@ -305,15 +303,11 @@ async def on_authenticate(request):
|
|
305
303
|
"You have been authenticated.",
|
306
304
|
authentication_session.json,
|
307
305
|
)
|
308
|
-
if authentication_session.is_refresh:
|
309
|
-
authentication_session.encode(response)
|
310
306
|
return response
|
311
307
|
```
|
312
308
|
|
313
309
|
* Requires Authentication (This method is not called directly and instead used as a decorator)
|
314
310
|
|
315
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
316
|
-
|
317
311
|
```python
|
318
312
|
@app.post("api/security/auth")
|
319
313
|
@requires_authentication
|
@@ -323,24 +317,22 @@ async def on_authenticate(request):
|
|
323
317
|
"You have been authenticated.",
|
324
318
|
authentication_session.json,
|
325
319
|
)
|
326
|
-
if authentication_session.is_refresh:
|
327
|
-
authentication_session.encode(response)
|
328
320
|
return response
|
329
321
|
```
|
330
322
|
|
331
|
-
* Authentication
|
323
|
+
* Authentication Middleware
|
324
|
+
|
325
|
+
New/Refreshed session returned if client's session expired during authentication, requires encoding.
|
332
326
|
|
333
|
-
|
327
|
+
Middleware is recommended to automatically encode the refreshed session.
|
334
328
|
|
335
329
|
```python
|
336
330
|
@app.on_response
|
337
331
|
async def authentication_refresh_encoder(request, response):
|
338
|
-
|
332
|
+
if hasattr(request.ctx, "authentication_session"):
|
339
333
|
authentication_session = request.ctx.authentication_session
|
340
334
|
if authentication_session.is_refresh:
|
341
335
|
authentication_session.encode(response)
|
342
|
-
except AttributeError:
|
343
|
-
pass
|
344
336
|
```
|
345
337
|
|
346
338
|
## Captcha
|
@@ -125,7 +125,7 @@ You can load environment variables with a different prefix via `config.load_envi
|
|
125
125
|
| **CAPTCHA_FONT** | captcha-font.ttf | The file path to the font being used for captcha generation. |
|
126
126
|
| **TWO_STEP_SESSION_EXPIRATION** | 200 | The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration. |
|
127
127
|
| **AUTHENTICATION_SESSION_EXPIRATION** | 86400 | The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration. |
|
128
|
-
| **AUTHENTICATION_REFRESH_EXPIRATION** |
|
128
|
+
| **AUTHENTICATION_REFRESH_EXPIRATION** | 604800 | The amount of seconds till authentication refresh expiration. Setting to 0 will disable refresh mechanism. |
|
129
129
|
| **ALLOW_LOGIN_WITH_USERNAME** | False | Allows login via username and email. |
|
130
130
|
| **INITIAL_ADMIN_EMAIL** | admin@example.com | Email used when creating the initial admin account. |
|
131
131
|
| **INITIAL_ADMIN_PASSWORD** | admin123 | Password used when creating the initial admin account. |
|
@@ -264,8 +264,6 @@ async def on_logout(request):
|
|
264
264
|
|
265
265
|
* Authenticate
|
266
266
|
|
267
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
268
|
-
|
269
267
|
```python
|
270
268
|
@app.post("api/security/auth")
|
271
269
|
async def on_authenticate(request):
|
@@ -274,15 +272,11 @@ async def on_authenticate(request):
|
|
274
272
|
"You have been authenticated.",
|
275
273
|
authentication_session.json,
|
276
274
|
)
|
277
|
-
if authentication_session.is_refresh:
|
278
|
-
authentication_session.encode(response)
|
279
275
|
return response
|
280
276
|
```
|
281
277
|
|
282
278
|
* Requires Authentication (This method is not called directly and instead used as a decorator)
|
283
279
|
|
284
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
285
|
-
|
286
280
|
```python
|
287
281
|
@app.post("api/security/auth")
|
288
282
|
@requires_authentication
|
@@ -292,24 +286,22 @@ async def on_authenticate(request):
|
|
292
286
|
"You have been authenticated.",
|
293
287
|
authentication_session.json,
|
294
288
|
)
|
295
|
-
if authentication_session.is_refresh:
|
296
|
-
authentication_session.encode(response)
|
297
289
|
return response
|
298
290
|
```
|
299
291
|
|
300
|
-
* Authentication
|
292
|
+
* Authentication Middleware
|
293
|
+
|
294
|
+
New/Refreshed session returned if client's session expired during authentication, requires encoding.
|
301
295
|
|
302
|
-
|
296
|
+
Middleware is recommended to automatically encode the refreshed session.
|
303
297
|
|
304
298
|
```python
|
305
299
|
@app.on_response
|
306
300
|
async def authentication_refresh_encoder(request, response):
|
307
|
-
|
301
|
+
if hasattr(request.ctx, "authentication_session"):
|
308
302
|
authentication_session = request.ctx.authentication_session
|
309
303
|
if authentication_session.is_refresh:
|
310
304
|
authentication_session.encode(response)
|
311
|
-
except AttributeError:
|
312
|
-
pass
|
313
305
|
```
|
314
306
|
|
315
307
|
## Captcha
|
@@ -198,7 +198,7 @@ async def fulfill_second_factor(request: Request) -> AuthenticationSession:
|
|
198
198
|
async def authenticate(request: Request) -> AuthenticationSession:
|
199
199
|
"""
|
200
200
|
Validates client's authentication session and account. New/Refreshed session automatically returned
|
201
|
-
if expired during authentication, requires encoding.
|
201
|
+
if client's session expired during authentication, requires encoding.
|
202
202
|
|
203
203
|
Args:
|
204
204
|
request (Request): Sanic request parameter.
|
@@ -223,13 +223,14 @@ async def authenticate(request: Request) -> AuthenticationSession:
|
|
223
223
|
authentication_session.bearer.validate()
|
224
224
|
except ExpiredError:
|
225
225
|
authentication_session = await authentication_session.refresh(request)
|
226
|
+
request.ctx.authentication_session = authentication_session
|
226
227
|
return authentication_session
|
227
228
|
|
228
229
|
|
229
230
|
def requires_authentication(arg=None):
|
230
231
|
"""
|
231
|
-
Validates client's authentication session and account. New/Refreshed session automatically returned
|
232
|
-
during authentication, requires encoding.
|
232
|
+
Validates client's authentication session and account. New/Refreshed session automatically returned
|
233
|
+
if client's session expired during authentication, requires encoding.
|
233
234
|
|
234
235
|
Example:
|
235
236
|
This method is not called directly and instead used as a decorator:
|
@@ -39,7 +39,7 @@ DEFAULT_CONFIG = {
|
|
39
39
|
"CAPTCHA_FONT": "captcha-font.ttf",
|
40
40
|
"TWO_STEP_SESSION_EXPIRATION": 300,
|
41
41
|
"AUTHENTICATION_SESSION_EXPIRATION": 86400,
|
42
|
-
"AUTHENTICATION_REFRESH_EXPIRATION":
|
42
|
+
"AUTHENTICATION_REFRESH_EXPIRATION": 604800,
|
43
43
|
"ALLOW_LOGIN_WITH_USERNAME": False,
|
44
44
|
"INITIAL_ADMIN_EMAIL": "admin@example.com",
|
45
45
|
"INITIAL_ADMIN_PASSWORD": "admin123",
|
@@ -65,7 +65,7 @@ class Config(dict):
|
|
65
65
|
CAPTCHA_FONT (str): The file path to the font being used for captcha generation.
|
66
66
|
TWO_STEP_SESSION_EXPIRATION (int): The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration.
|
67
67
|
AUTHENTICATION_SESSION_EXPIRATION (int): The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration.
|
68
|
-
AUTHENTICATION_REFRESH_EXPIRATION (int): The amount of seconds till authentication session refresh expiration.
|
68
|
+
AUTHENTICATION_REFRESH_EXPIRATION (int): The amount of seconds till authentication session refresh expiration. Setting to 0 will disable refresh mechanism.
|
69
69
|
ALLOW_LOGIN_WITH_USERNAME (bool): Allows login via username and email.
|
70
70
|
INITIAL_ADMIN_EMAIL (str): Email used when creating the initial admin account.
|
71
71
|
INITIAL_ADMIN_PASSWORD (str): Password used when creating the initial admin account.
|
@@ -296,8 +296,13 @@ class Session(BaseModel):
|
|
296
296
|
samesite=security_config.SESSION_SAMESITE,
|
297
297
|
secure=security_config.SESSION_SECURE,
|
298
298
|
)
|
299
|
-
if self.expiration_date:
|
300
|
-
|
299
|
+
if self.expiration_date: # Overrides refresh expiration.
|
300
|
+
if hasattr(self, "refresh_expiration_date"):
|
301
|
+
response.cookies.get_cookie(cookie).expires = (
|
302
|
+
self.refresh_expiration_date
|
303
|
+
)
|
304
|
+
else:
|
305
|
+
response.cookies.get_cookie(cookie).expires = self.expiration_date
|
301
306
|
if security_config.SESSION_DOMAIN:
|
302
307
|
response.cookies.get_cookie(cookie).domain = security_config.SESSION_DOMAIN
|
303
308
|
|
@@ -566,7 +571,8 @@ class AuthenticationSession(Session):
|
|
566
571
|
raise NotExpiredError()
|
567
572
|
except ExpiredError as e:
|
568
573
|
if (
|
569
|
-
|
574
|
+
self.refresh_expiration_date
|
575
|
+
and datetime.datetime.now(datetime.timezone.utc)
|
570
576
|
<= self.refresh_expiration_date
|
571
577
|
):
|
572
578
|
self.active = False
|
@@ -175,12 +175,10 @@ async def on_authenticate(request):
|
|
175
175
|
|
176
176
|
@app.on_response
|
177
177
|
async def authentication_refresh_encoder(request, response):
|
178
|
-
|
178
|
+
if hasattr(request.ctx, "authentication_session"):
|
179
179
|
authentication_session = request.ctx.authentication_session
|
180
180
|
if authentication_session.is_refresh:
|
181
181
|
authentication_session.encode(response)
|
182
|
-
except AttributeError:
|
183
|
-
pass
|
184
182
|
|
185
183
|
|
186
184
|
@app.post("api/test/auth/expire")
|
@@ -570,8 +570,9 @@ class MiscTest(TestCase):
|
|
570
570
|
"http://127.0.0.1:8000/api/test/auth",
|
571
571
|
)
|
572
572
|
assert (
|
573
|
-
|
573
|
+
authenticate_refresh_response.status_code == 200
|
574
574
|
), authenticate_refresh_response.text
|
575
|
+
assert json.loads(authenticate_refresh_response.text)["data"]["refresh"] is True
|
575
576
|
authenticate_response = self.client.post(
|
576
577
|
"http://127.0.0.1:8000/api/test/auth",
|
577
578
|
) # Since session refresh handling is complete, it will be returned as a regular session now.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sanic-security
|
3
|
-
Version: 1.12.
|
3
|
+
Version: 1.12.3
|
4
4
|
Summary: An async security library for the Sanic framework.
|
5
5
|
Author-email: Aidan Stewart <na.stewart365@gmail.com>
|
6
6
|
Project-URL: Documentation, https://security.na-stewart.com/
|
@@ -156,7 +156,7 @@ You can load environment variables with a different prefix via `config.load_envi
|
|
156
156
|
| **CAPTCHA_FONT** | captcha-font.ttf | The file path to the font being used for captcha generation. |
|
157
157
|
| **TWO_STEP_SESSION_EXPIRATION** | 200 | The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration. |
|
158
158
|
| **AUTHENTICATION_SESSION_EXPIRATION** | 86400 | The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration. |
|
159
|
-
| **AUTHENTICATION_REFRESH_EXPIRATION** |
|
159
|
+
| **AUTHENTICATION_REFRESH_EXPIRATION** | 604800 | The amount of seconds till authentication refresh expiration. Setting to 0 will disable refresh mechanism. |
|
160
160
|
| **ALLOW_LOGIN_WITH_USERNAME** | False | Allows login via username and email. |
|
161
161
|
| **INITIAL_ADMIN_EMAIL** | admin@example.com | Email used when creating the initial admin account. |
|
162
162
|
| **INITIAL_ADMIN_PASSWORD** | admin123 | Password used when creating the initial admin account. |
|
@@ -295,8 +295,6 @@ async def on_logout(request):
|
|
295
295
|
|
296
296
|
* Authenticate
|
297
297
|
|
298
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
299
|
-
|
300
298
|
```python
|
301
299
|
@app.post("api/security/auth")
|
302
300
|
async def on_authenticate(request):
|
@@ -305,15 +303,11 @@ async def on_authenticate(request):
|
|
305
303
|
"You have been authenticated.",
|
306
304
|
authentication_session.json,
|
307
305
|
)
|
308
|
-
if authentication_session.is_refresh:
|
309
|
-
authentication_session.encode(response)
|
310
306
|
return response
|
311
307
|
```
|
312
308
|
|
313
309
|
* Requires Authentication (This method is not called directly and instead used as a decorator)
|
314
310
|
|
315
|
-
New/Refreshed session will be returned if expired, requires encoding.
|
316
|
-
|
317
311
|
```python
|
318
312
|
@app.post("api/security/auth")
|
319
313
|
@requires_authentication
|
@@ -323,24 +317,22 @@ async def on_authenticate(request):
|
|
323
317
|
"You have been authenticated.",
|
324
318
|
authentication_session.json,
|
325
319
|
)
|
326
|
-
if authentication_session.is_refresh:
|
327
|
-
authentication_session.encode(response)
|
328
320
|
return response
|
329
321
|
```
|
330
322
|
|
331
|
-
* Authentication
|
323
|
+
* Authentication Middleware
|
324
|
+
|
325
|
+
New/Refreshed session returned if client's session expired during authentication, requires encoding.
|
332
326
|
|
333
|
-
|
327
|
+
Middleware is recommended to automatically encode the refreshed session.
|
334
328
|
|
335
329
|
```python
|
336
330
|
@app.on_response
|
337
331
|
async def authentication_refresh_encoder(request, response):
|
338
|
-
|
332
|
+
if hasattr(request.ctx, "authentication_session"):
|
339
333
|
authentication_session = request.ctx.authentication_session
|
340
334
|
if authentication_session.is_refresh:
|
341
335
|
authentication_session.encode(response)
|
342
|
-
except AttributeError:
|
343
|
-
pass
|
344
336
|
```
|
345
337
|
|
346
338
|
## Captcha
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{sanic_security-1.12.1 → sanic_security-1.12.3}/sanic_security.egg-info/dependency_links.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|