sanic-security 1.12.1__py3-none-any.whl → 1.12.3__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.
@@ -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 if expired
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": 2592000,
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.
sanic_security/models.py CHANGED
@@ -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
- response.cookies.get_cookie(cookie).expires = self.expiration_date
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
- datetime.datetime.now(datetime.timezone.utc)
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
- try:
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
- json.loads(authenticate_refresh_response.text)["data"]["refresh"] is True
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.1
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** | 2592000 | The amount of seconds till 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 Refresh Middleware
323
+ * Authentication Middleware
324
+
325
+ New/Refreshed session returned if client's session expired during authentication, requires encoding.
332
326
 
333
- If it's inconvenient to encode the refreshed session during authentication, it can also be done automatically via middleware.
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
- try:
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
@@ -0,0 +1,16 @@
1
+ sanic_security/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ sanic_security/authentication.py,sha256=E17jQg1gD06CTRk7l9q8EUzgeAEXn2J0E02Va-QYx9I,12573
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=OEvO4xUh_7QCdwfaiKt51T3fmn3MJSrIcM1TszDfqgg,20776
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.3.dist-info/LICENSE,sha256=sXlJs9_mG-dCkPfWsDnuzydJWagS82E2gYtkVH9enHA,1100
13
+ sanic_security-1.12.3.dist-info/METADATA,sha256=Xaqk6JqUV3Y7IafPDQ85k7VTaghTLTQGtbLpKiZ7gEo,23680
14
+ sanic_security-1.12.3.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
15
+ sanic_security-1.12.3.dist-info/top_level.txt,sha256=ZybkhHXSjfzhmv8XeqLvnNmLmv21Z0oPX6Ep4DJN8b0,15
16
+ sanic_security-1.12.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.1.0)
2
+ Generator: setuptools (70.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,16 +0,0 @@
1
- sanic_security/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- sanic_security/authentication.py,sha256=ucOdF-g00BmztFAqFf7gy03PIwVXU0Jp0Y8NNC5OwAw,12474
3
- sanic_security/authorization.py,sha256=aQztMiZG9LDctr_C6QEzO5qScwbxpiLk96XVxwdCChM,6921
4
- sanic_security/configuration.py,sha256=U-xUgceT5ZRjbxYocrzhxyJYFBkobCrlxLNMVGJNX2k,5470
5
- sanic_security/exceptions.py,sha256=8c3xoQSiIKfSiOQOtw49RG8Qdlc3vZDzqjrEnPad4Ds,5411
6
- sanic_security/models.py,sha256=Kia53ynvlcw7QzePC7_vzNlFlIK89QTPcIAMpqfi1yo,20478
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=qQtbQh8m9QYf4g1SL8QJbOyyJzAXFaNmDyBxU8b6RBc,12627
11
- sanic_security/test/tests.py,sha256=e8J_QfX4QPJT0mxkB1tWBrK_2lrPNtGR8RFXYoQ2kOo,22394
12
- sanic_security-1.12.1.dist-info/LICENSE,sha256=sXlJs9_mG-dCkPfWsDnuzydJWagS82E2gYtkVH9enHA,1100
13
- sanic_security-1.12.1.dist-info/METADATA,sha256=Z2-rbZpVQm_obMP3sqeAoI3Ge3WbhM9EjsPtTPMjYXM,23963
14
- sanic_security-1.12.1.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
15
- sanic_security-1.12.1.dist-info/top_level.txt,sha256=ZybkhHXSjfzhmv8XeqLvnNmLmv21Z0oPX6Ep4DJN8b0,15
16
- sanic_security-1.12.1.dist-info/RECORD,,