sanic-security 1.12.4__py3-none-any.whl → 1.12.6__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.
- sanic_security/authentication.py +16 -0
- sanic_security/exceptions.py +1 -1
- sanic_security/test/server.py +2 -8
- sanic_security/test/tests.py +1 -3
- sanic_security/utils.py +4 -1
- {sanic_security-1.12.4.dist-info → sanic_security-1.12.6.dist-info}/METADATA +20 -24
- sanic_security-1.12.6.dist-info/RECORD +16 -0
- {sanic_security-1.12.4.dist-info → sanic_security-1.12.6.dist-info}/WHEEL +1 -1
- sanic_security-1.12.4.dist-info/RECORD +0 -16
- {sanic_security-1.12.4.dist-info → sanic_security-1.12.6.dist-info}/LICENSE +0 -0
- {sanic_security-1.12.4.dist-info → sanic_security-1.12.6.dist-info}/top_level.txt +0 -0
sanic_security/authentication.py
CHANGED
@@ -249,6 +249,22 @@ def requires_authentication(arg=None):
|
|
249
249
|
return decorator(arg) if callable(arg) else decorator
|
250
250
|
|
251
251
|
|
252
|
+
def attach_refresh_encoder(app: Sanic):
|
253
|
+
"""
|
254
|
+
Automatically encodes the new/refreshed session returned during authentication when client's current session expires.
|
255
|
+
|
256
|
+
Args:
|
257
|
+
app: (Sanic): The main Sanic application instance.
|
258
|
+
"""
|
259
|
+
|
260
|
+
@app.on_response
|
261
|
+
async def refresh_encoder_middleware(request, response):
|
262
|
+
if hasattr(request.ctx, "authentication_session"):
|
263
|
+
authentication_session = request.ctx.authentication_session
|
264
|
+
if authentication_session.is_refresh:
|
265
|
+
authentication_session.encode(response)
|
266
|
+
|
267
|
+
|
252
268
|
def create_initial_admin_account(app: Sanic) -> None:
|
253
269
|
"""
|
254
270
|
Creates the initial admin account that can be logged into and has complete authoritative access.
|
sanic_security/exceptions.py
CHANGED
sanic_security/test/server.py
CHANGED
@@ -12,6 +12,7 @@ from sanic_security.authentication import (
|
|
12
12
|
logout,
|
13
13
|
create_initial_admin_account,
|
14
14
|
fulfill_second_factor,
|
15
|
+
attach_refresh_encoder,
|
15
16
|
)
|
16
17
|
from sanic_security.authorization import (
|
17
18
|
assign_role,
|
@@ -173,14 +174,6 @@ async def on_authenticate(request):
|
|
173
174
|
return response
|
174
175
|
|
175
176
|
|
176
|
-
@app.on_response
|
177
|
-
async def authentication_refresh_encoder(request, response):
|
178
|
-
if hasattr(request.ctx, "authentication_session"):
|
179
|
-
authentication_session = request.ctx.authentication_session
|
180
|
-
if authentication_session.is_refresh:
|
181
|
-
authentication_session.encode(response)
|
182
|
-
|
183
|
-
|
184
177
|
@app.post("api/test/auth/expire")
|
185
178
|
@requires_authentication
|
186
179
|
async def on_authentication_expire(request):
|
@@ -351,6 +344,7 @@ register_tortoise(
|
|
351
344
|
modules={"models": ["sanic_security.models"]},
|
352
345
|
generate_schemas=True,
|
353
346
|
)
|
347
|
+
attach_refresh_encoder(app)
|
354
348
|
create_initial_admin_account(app)
|
355
349
|
if __name__ == "__main__":
|
356
350
|
app.run(host="127.0.0.1", port=8000, workers=1, debug=True)
|
sanic_security/test/tests.py
CHANGED
@@ -576,6 +576,4 @@ class MiscTest(TestCase):
|
|
576
576
|
authenticate_response = self.client.post(
|
577
577
|
"http://127.0.0.1:8000/api/test/auth",
|
578
578
|
) # Since session refresh handling is complete, it will be returned as a regular session now.
|
579
|
-
assert
|
580
|
-
json.loads(authenticate_response.text)["data"]["refresh"] is False
|
581
|
-
), authenticate_response.text
|
579
|
+
assert authenticate_response.status_code == 200, authenticate_response.text
|
sanic_security/utils.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import datetime
|
2
2
|
import random
|
3
|
+
import string
|
3
4
|
|
4
5
|
from sanic.request import Request
|
5
6
|
from sanic.response import json as sanic_json, HTTPResponse
|
@@ -47,7 +48,9 @@ def get_code() -> str:
|
|
47
48
|
Returns:
|
48
49
|
code
|
49
50
|
"""
|
50
|
-
return
|
51
|
+
return "".join(
|
52
|
+
random.choice(string.ascii_uppercase + string.digits) for _ in range(6)
|
53
|
+
)
|
51
54
|
|
52
55
|
|
53
56
|
def json(
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sanic-security
|
3
|
-
Version: 1.12.
|
3
|
+
Version: 1.12.6
|
4
4
|
Summary: An async security library for the Sanic framework.
|
5
|
-
Author-email: Aidan Stewart <na.
|
5
|
+
Author-email: Aidan Stewart <me@na-stewart.com>
|
6
6
|
Project-URL: Documentation, https://security.na-stewart.com/
|
7
7
|
Project-URL: Repository, https://github.com/na-stewart/sanic-security
|
8
8
|
Keywords: security,authentication,authorization,verification,async,sanic
|
@@ -197,7 +197,7 @@ async def on_register(request):
|
|
197
197
|
account = await register(request)
|
198
198
|
two_step_session = await request_two_step_verification(request, account)
|
199
199
|
await email_code(
|
200
|
-
account.email, two_step_session.code # Code =
|
200
|
+
account.email, two_step_session.code # Code = 24KF19
|
201
201
|
) # Custom method for emailing verification code.
|
202
202
|
response = json(
|
203
203
|
"Registration successful! Email verification required.",
|
@@ -213,7 +213,7 @@ Verifies the client's account via two-step session code.
|
|
213
213
|
|
214
214
|
| Key | Value |
|
215
215
|
|----------|--------|
|
216
|
-
| **code** |
|
216
|
+
| **code** | 24KF19 |
|
217
217
|
|
218
218
|
```python
|
219
219
|
@app.post("api/security/verify")
|
@@ -237,7 +237,7 @@ async def on_login(request):
|
|
237
237
|
request, authentication_session.bearer
|
238
238
|
)
|
239
239
|
await email_code(
|
240
|
-
authentication_session.bearer.email, two_step_session.code # Code =
|
240
|
+
authentication_session.bearer.email, two_step_session.code # Code = XGED2U
|
241
241
|
) # Custom method for emailing verification code.
|
242
242
|
response = json(
|
243
243
|
"Login successful! Two-factor authentication required.",
|
@@ -248,7 +248,7 @@ async def on_login(request):
|
|
248
248
|
return response
|
249
249
|
```
|
250
250
|
|
251
|
-
If this isn't desired, you can pass an account and password attempt directly into the login instead.
|
251
|
+
If this isn't desired, you can pass an account and password attempt directly into the login method instead.
|
252
252
|
|
253
253
|
* Fulfill Second Factor
|
254
254
|
|
@@ -256,7 +256,7 @@ Fulfills client authentication session's second factor requirement via two-step
|
|
256
256
|
|
257
257
|
| Key | Value |
|
258
258
|
|----------|--------|
|
259
|
-
| **code** |
|
259
|
+
| **code** | XGED2U |
|
260
260
|
|
261
261
|
```python
|
262
262
|
@app.post("api/security/fulfill-2fa")
|
@@ -321,19 +321,15 @@ async def on_authenticate(request):
|
|
321
321
|
return response
|
322
322
|
```
|
323
323
|
|
324
|
-
*
|
324
|
+
* Refresh Encoder
|
325
325
|
|
326
|
-
|
327
|
-
|
328
|
-
Middleware is recommended to automatically encode the refreshed session.
|
326
|
+
A new/refreshed session is returned during authentication when the client's current session expires and it
|
327
|
+
requires encoding. This should be be done automatically via middleware.
|
329
328
|
|
330
329
|
```python
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
authentication_session = request.ctx.authentication_session
|
335
|
-
if authentication_session.is_refresh:
|
336
|
-
authentication_session.encode(response)
|
330
|
+
attach_refresh_encoder(app)
|
331
|
+
if __name__ == "__main__":
|
332
|
+
app.run(host="127.0.0.1", port=8000)
|
337
333
|
```
|
338
334
|
|
339
335
|
## Captcha
|
@@ -351,7 +347,7 @@ downloading a .ttf font and defining the file's path in the configuration.
|
|
351
347
|
@app.get("api/security/captcha")
|
352
348
|
async def on_captcha_img_request(request):
|
353
349
|
captcha_session = await request_captcha(request)
|
354
|
-
response = captcha_session.get_image() # Captcha:
|
350
|
+
response = captcha_session.get_image() # Captcha: LJ0F3U
|
355
351
|
captcha_session.encode(response)
|
356
352
|
return response
|
357
353
|
```
|
@@ -360,7 +356,7 @@ async def on_captcha_img_request(request):
|
|
360
356
|
|
361
357
|
| Key | Value |
|
362
358
|
|-------------|--------|
|
363
|
-
| **captcha** |
|
359
|
+
| **captcha** | LJ0F3U |
|
364
360
|
|
365
361
|
```python
|
366
362
|
@app.post("api/security/captcha")
|
@@ -373,7 +369,7 @@ async def on_captcha(request):
|
|
373
369
|
|
374
370
|
| Key | Value |
|
375
371
|
|-------------|--------|
|
376
|
-
| **captcha** |
|
372
|
+
| **captcha** | LJ0F3U |
|
377
373
|
|
378
374
|
```python
|
379
375
|
@app.post("api/security/captcha")
|
@@ -395,7 +391,7 @@ Two-step verification should be integrated with other custom functionality. For
|
|
395
391
|
```python
|
396
392
|
@app.post("api/security/two-step/request")
|
397
393
|
async def on_two_step_request(request):
|
398
|
-
two_step_session = await request_two_step_verification(request) # Code =
|
394
|
+
two_step_session = await request_two_step_verification(request) # Code = T2I58I
|
399
395
|
await email_code(
|
400
396
|
two_step_session.bearer.email, two_step_session.code
|
401
397
|
) # Custom method for emailing verification code.
|
@@ -409,7 +405,7 @@ async def on_two_step_request(request):
|
|
409
405
|
```python
|
410
406
|
@app.post("api/security/two-step/resend")
|
411
407
|
async def on_two_step_resend(request):
|
412
|
-
two_step_session = await TwoStepSession.decode(request) # Code =
|
408
|
+
two_step_session = await TwoStepSession.decode(request) # Code = T2I58I
|
413
409
|
await email_code(
|
414
410
|
two_step_session.bearer.email, two_step_session.code
|
415
411
|
) # Custom method for emailing verification code.
|
@@ -420,7 +416,7 @@ async def on_two_step_resend(request):
|
|
420
416
|
|
421
417
|
| Key | Value |
|
422
418
|
|----------|--------|
|
423
|
-
| **code** |
|
419
|
+
| **code** | T2I58I |
|
424
420
|
|
425
421
|
```python
|
426
422
|
@app.post("api/security/two-step")
|
@@ -434,7 +430,7 @@ async def on_two_step_verification(request):
|
|
434
430
|
|
435
431
|
| Key | Value |
|
436
432
|
|----------|--------|
|
437
|
-
| **code** |
|
433
|
+
| **code** | T2I58I |
|
438
434
|
|
439
435
|
```python
|
440
436
|
@app.post("api/security/two-step")
|
@@ -0,0 +1,16 @@
|
|
1
|
+
sanic_security/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
sanic_security/authentication.py,sha256=obMKNnJXleeBGXqmsm1y5jFNI-FrW9krdO5SD6yOstE,12598
|
3
|
+
sanic_security/authorization.py,sha256=aQztMiZG9LDctr_C6QEzO5qScwbxpiLk96XVxwdCChM,6921
|
4
|
+
sanic_security/configuration.py,sha256=p44nTSrBQQSJZYN6qJEod_Ettf90rRNlmPxmNzxqQ9A,5514
|
5
|
+
sanic_security/exceptions.py,sha256=MTPF4tm_68Nmf_z06RHH_6DTiC_CNiLER1jzEoW1dFk,5398
|
6
|
+
sanic_security/models.py,sha256=nj5iYHzPZzdLs5dc3j6kdeScSk1SASizfK58Sa5YN8E,22527
|
7
|
+
sanic_security/utils.py,sha256=XAUNalcTi53qTz0D8xiDyDyRlq7Z7ffNBzUONJZqe90,2705
|
8
|
+
sanic_security/verification.py,sha256=vrxYborEOBKEirOHczul9WYub5j6T2ldXE1gsoA8iyY,7503
|
9
|
+
sanic_security/test/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
|
+
sanic_security/test/server.py,sha256=pwqsDS81joMdxIynivaNPCCMamv9qzAjknfZ01ZxQHc,12380
|
11
|
+
sanic_security/test/tests.py,sha256=6TUp5GVYIR27qCzwIw2qt7DvW7ohxj-seYpnpeMbuno,22407
|
12
|
+
sanic_security-1.12.6.dist-info/LICENSE,sha256=sXlJs9_mG-dCkPfWsDnuzydJWagS82E2gYtkVH9enHA,1100
|
13
|
+
sanic_security-1.12.6.dist-info/METADATA,sha256=aiKkOtkYiexSjoB4uysSQwxAVqRGAQnultZKvx5srAs,23382
|
14
|
+
sanic_security-1.12.6.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
15
|
+
sanic_security-1.12.6.dist-info/top_level.txt,sha256=ZybkhHXSjfzhmv8XeqLvnNmLmv21Z0oPX6Ep4DJN8b0,15
|
16
|
+
sanic_security-1.12.6.dist-info/RECORD,,
|
@@ -1,16 +0,0 @@
|
|
1
|
-
sanic_security/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
-
sanic_security/authentication.py,sha256=j_V-XEx8op5fYcszIE6PoeBDId0OJA8KlJW7FDXKr6s,12012
|
3
|
-
sanic_security/authorization.py,sha256=aQztMiZG9LDctr_C6QEzO5qScwbxpiLk96XVxwdCChM,6921
|
4
|
-
sanic_security/configuration.py,sha256=p44nTSrBQQSJZYN6qJEod_Ettf90rRNlmPxmNzxqQ9A,5514
|
5
|
-
sanic_security/exceptions.py,sha256=8c3xoQSiIKfSiOQOtw49RG8Qdlc3vZDzqjrEnPad4Ds,5411
|
6
|
-
sanic_security/models.py,sha256=nj5iYHzPZzdLs5dc3j6kdeScSk1SASizfK58Sa5YN8E,22527
|
7
|
-
sanic_security/utils.py,sha256=Zgde7W69ixwv_H8eTs7indO5_U2Jvq62YUpG6ipN768,2629
|
8
|
-
sanic_security/verification.py,sha256=vrxYborEOBKEirOHczul9WYub5j6T2ldXE1gsoA8iyY,7503
|
9
|
-
sanic_security/test/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
|
-
sanic_security/test/server.py,sha256=G5q7mzTUxOpKlhbzNbzTZYSWd6g8a0toOFX9qTA_nVg,12631
|
11
|
-
sanic_security/test/tests.py,sha256=Hg40wlZfC-CDZX6lIjeT6uXy-3BJMc4ChJsnCRCBIu8,22459
|
12
|
-
sanic_security-1.12.4.dist-info/LICENSE,sha256=sXlJs9_mG-dCkPfWsDnuzydJWagS82E2gYtkVH9enHA,1100
|
13
|
-
sanic_security-1.12.4.dist-info/METADATA,sha256=jBOYc-2TvLy6eFKw9FpiFoXe4toejAPeiw7eVlm0Gi0,23594
|
14
|
-
sanic_security-1.12.4.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
|
15
|
-
sanic_security-1.12.4.dist-info/top_level.txt,sha256=ZybkhHXSjfzhmv8XeqLvnNmLmv21Z0oPX6Ep4DJN8b0,15
|
16
|
-
sanic_security-1.12.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|