sanic-security 1.12.4__tar.gz → 1.12.5__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.
Files changed (21) hide show
  1. {sanic_security-1.12.4/sanic_security.egg-info → sanic_security-1.12.5}/PKG-INFO +8 -12
  2. {sanic_security-1.12.4 → sanic_security-1.12.5}/README.md +7 -11
  3. {sanic_security-1.12.4 → sanic_security-1.12.5}/pyproject.toml +1 -1
  4. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/authentication.py +16 -0
  5. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/exceptions.py +1 -1
  6. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/test/server.py +2 -8
  7. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/test/tests.py +1 -3
  8. {sanic_security-1.12.4 → sanic_security-1.12.5/sanic_security.egg-info}/PKG-INFO +8 -12
  9. {sanic_security-1.12.4 → sanic_security-1.12.5}/LICENSE +0 -0
  10. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/__init__.py +0 -0
  11. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/authorization.py +0 -0
  12. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/configuration.py +0 -0
  13. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/models.py +0 -0
  14. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/test/__init__.py +0 -0
  15. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/utils.py +0 -0
  16. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security/verification.py +0 -0
  17. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security.egg-info/SOURCES.txt +0 -0
  18. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security.egg-info/dependency_links.txt +0 -0
  19. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security.egg-info/requires.txt +0 -0
  20. {sanic_security-1.12.4 → sanic_security-1.12.5}/sanic_security.egg-info/top_level.txt +0 -0
  21. {sanic_security-1.12.4 → sanic_security-1.12.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sanic-security
3
- Version: 1.12.4
3
+ Version: 1.12.5
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/
@@ -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
 
@@ -321,19 +321,15 @@ async def on_authenticate(request):
321
321
  return response
322
322
  ```
323
323
 
324
- * Authentication Middleware
324
+ * Refresh Encoder
325
325
 
326
- New/Refreshed session returned if client's session expired during authentication, requires encoding.
327
-
328
- Middleware is recommended to automatically encode the refreshed session.
326
+ A new/refreshed session is returned during authentication if the client's session expired during authentication and
327
+ requires encoding. Rather than doing so manually, it can be done automatically via middleware.
329
328
 
330
329
  ```python
331
- @app.on_response
332
- async def authentication_refresh_encoder(request, response):
333
- if hasattr(request.ctx, "authentication_session"):
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
@@ -217,7 +217,7 @@ async def on_login(request):
217
217
  return response
218
218
  ```
219
219
 
220
- If this isn't desired, you can pass an account and password attempt directly into the login instead.
220
+ If this isn't desired, you can pass an account and password attempt directly into the login method instead.
221
221
 
222
222
  * Fulfill Second Factor
223
223
 
@@ -290,19 +290,15 @@ async def on_authenticate(request):
290
290
  return response
291
291
  ```
292
292
 
293
- * Authentication Middleware
293
+ * Refresh Encoder
294
294
 
295
- New/Refreshed session returned if client's session expired during authentication, requires encoding.
296
-
297
- Middleware is recommended to automatically encode the refreshed session.
295
+ A new/refreshed session is returned during authentication if the client's session expired during authentication and
296
+ requires encoding. Rather than doing so manually, it can be done automatically via middleware.
298
297
 
299
298
  ```python
300
- @app.on_response
301
- async def authentication_refresh_encoder(request, response):
302
- if hasattr(request.ctx, "authentication_session"):
303
- authentication_session = request.ctx.authentication_session
304
- if authentication_session.is_refresh:
305
- authentication_session.encode(response)
299
+ attach_refresh_encoder(app)
300
+ if __name__ == "__main__":
301
+ app.run(host="127.0.0.1", port=8000)
306
302
  ```
307
303
 
308
304
  ## Captcha
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sanic-security"
7
- version = "1.12.4"
7
+ version = "1.12.5"
8
8
  requires-python = ">=3.8"
9
9
  dependencies = [
10
10
  "tortoise-orm>=0.17.0",
@@ -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.
@@ -130,7 +130,7 @@ class DeactivatedError(SessionError):
130
130
 
131
131
  def __init__(
132
132
  self,
133
- message: str = "Session has been deactivated or refreshed.",
133
+ message: str = "Session has been deactivated.",
134
134
  code: int = 401,
135
135
  ):
136
136
  super().__init__(message, code)
@@ -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)
@@ -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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sanic-security
3
- Version: 1.12.4
3
+ Version: 1.12.5
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/
@@ -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
 
@@ -321,19 +321,15 @@ async def on_authenticate(request):
321
321
  return response
322
322
  ```
323
323
 
324
- * Authentication Middleware
324
+ * Refresh Encoder
325
325
 
326
- New/Refreshed session returned if client's session expired during authentication, requires encoding.
327
-
328
- Middleware is recommended to automatically encode the refreshed session.
326
+ A new/refreshed session is returned during authentication if the client's session expired during authentication and
327
+ requires encoding. Rather than doing so manually, it can be done automatically via middleware.
329
328
 
330
329
  ```python
331
- @app.on_response
332
- async def authentication_refresh_encoder(request, response):
333
- if hasattr(request.ctx, "authentication_session"):
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
File without changes