pypomes-jwt 1.0.0__tar.gz → 1.0.2__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 pypomes-jwt might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pypomes_jwt
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: A collection of Python pomes, penyeach (JWT module)
5
5
  Project-URL: Homepage, https://github.com/TheWiseCoder/PyPomes-JWT
6
6
  Project-URL: Bug Tracker, https://github.com/TheWiseCoder/PyPomes-JWT/issues
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "pypomes_jwt"
9
- version = "1.0.0"
9
+ version = "1.0.2"
10
10
  authors = [
11
11
  { name="GT Nunes", email="wisecoder01@gmail.com" }
12
12
  ]
@@ -36,45 +36,33 @@ def jwt_needed(func: callable) -> callable:
36
36
  return wrapper
37
37
 
38
38
 
39
- def jwt_verify_request(request: Request,
40
- logger: Logger = None) -> Response:
39
+ def jwt_verify_request(request: Request) -> Response:
41
40
  """
42
- Verify wheher the HTTP *request* has the proper authorization, as per the JWT standard.
41
+ Verify whether the HTTP *request* has the proper authorization, as per the JWT standard.
43
42
 
44
43
  :param request: the request to be verified
45
- :param logger: optional logger
46
44
  :return: *None* if the request is valid, otherwise a *Response* object reporting the error
47
45
  """
48
46
  # initialize the return variable
49
47
  result: Response | None = None
50
48
 
51
- if logger:
52
- logger.debug(msg="Validate a JWT token")
53
- err_msg: str | None = None
54
-
55
49
  # retrieve the authorization from the request header
56
50
  auth_header: str = request.headers.get("Authorization")
57
51
 
58
52
  # was a 'Bearer' authorization obtained ?
53
+ bad_token: bool = False
59
54
  if auth_header and auth_header.startswith("Bearer "):
60
55
  # yes, extract and validate the JWT access token
61
56
  token: str = auth_header.split(" ")[1]
62
- if logger:
63
- logger.debug(msg="Bearer token was retrieved")
64
57
  errors: list[str] = []
65
58
  jwt_validate_token(errors=errors,
66
59
  nature="A",
67
60
  token=token)
68
61
  if errors:
69
- err_msg = "; ".join(errors)
70
- else:
71
- # no 'Bearer' found, report the error
72
- err_msg = "Request header has no 'Bearer' data"
62
+ bad_token = True
73
63
 
74
- # log the error and deny the authorization
75
- if err_msg:
76
- if logger:
77
- logger.error(msg=err_msg)
64
+ # deny the authorization
65
+ if bad_token:
78
66
  result = Response(response="Authorization failed",
79
67
  status=401)
80
68
  return result
@@ -448,11 +436,11 @@ def jwt_refresh_tokens(errors: list[str] | None,
448
436
  # assert the refresh token
449
437
  if refresh_token:
450
438
  # is the refresh token valid ?
451
- account_claims = jwt_validate_token(errors=op_errors,
452
- token=refresh_token,
453
- nature="R",
454
- account_id=account_id,
455
- logger=logger)
439
+ account_claims = (jwt_validate_token(errors=op_errors,
440
+ token=refresh_token,
441
+ nature="R",
442
+ account_id=account_id,
443
+ logger=logger) or {}).get("payload")
456
444
  # if it is, revoke current refresh token
457
445
  if account_claims and jwt_revoke_token(errors=op_errors,
458
446
  account_id=account_id,
@@ -5,7 +5,9 @@ from base64 import urlsafe_b64encode
5
5
  from datetime import datetime, timezone
6
6
  from logging import Logger
7
7
  from pypomes_core import str_random
8
- from pypomes_db import db_connect, db_commit, db_update, db_delete
8
+ from pypomes_db import (
9
+ db_connect, db_commit, db_select, db_insert, db_update, db_delete
10
+ )
9
11
  from threading import Lock
10
12
  from typing import Any
11
13
 
@@ -275,12 +277,11 @@ class JwtRegistry:
275
277
  key=JWT_ENCODING_KEY,
276
278
  algorithm=JWT_DEFAULT_ALGORITHM,
277
279
  headers={"kid": "R0"})
278
- # obtain a DB connection (may raise an exception)
280
+ # obtain a DB connection
279
281
  db_conn: Any = db_connect(errors=errors,
280
282
  logger=logger)
281
283
  # persist the candidate token (may raise an exception)
282
- token_id: int = _jwt_persist_token(errors=errors,
283
- account_id=account_id,
284
+ token_id: int = _jwt_persist_token(account_id=account_id,
284
285
  jwt_token=refresh_token,
285
286
  db_conn=db_conn,
286
287
  logger=logger)
@@ -297,9 +298,10 @@ class JwtRegistry:
297
298
  connection=db_conn,
298
299
  logger=logger)
299
300
  # commit the transaction
300
- db_commit(errors=errors,
301
- connection=db_conn,
302
- logger=logger)
301
+ if not errors:
302
+ db_commit(errors=errors,
303
+ connection=db_conn,
304
+ logger=logger)
303
305
  if errors:
304
306
  raise RuntimeError("; ".join(errors))
305
307
 
@@ -339,8 +341,7 @@ class JwtRegistry:
339
341
  return result
340
342
 
341
343
 
342
- def _jwt_persist_token(errors: list[str],
343
- account_id: str,
344
+ def _jwt_persist_token(account_id: str,
344
345
  jwt_token: str,
345
346
  db_conn: Any = None,
346
347
  logger: Logger = None) -> int:
@@ -354,7 +355,6 @@ def _jwt_persist_token(errors: list[str],
354
355
 
355
356
  If *db_conn* is provided, then all DB operations will be carried out in the scope of a single transaction.
356
357
 
357
- :param errors: incidental errors
358
358
  :param account_id: the account identification
359
359
  :param jwt_token: the JWT token to persist
360
360
  :param db_conn: the database connection to use
@@ -362,17 +362,19 @@ def _jwt_persist_token(errors: list[str],
362
362
  :return: the storage id of the inserted token
363
363
  :raises RuntimeError: error accessing the token database
364
364
  """
365
- from pypomes_db import db_select, db_insert, db_delete
366
365
  from .jwt_pomes import jwt_get_claims
367
366
 
368
367
  # retrieve the account's tokens
368
+ errors: list[str] = []
369
369
  # noinspection PyTypeChecker
370
370
  recs: list[tuple[int, str, str, str]] = \
371
371
  db_select(errors=errors,
372
372
  sel_stmt=f"SELECT {JWT_DB_COL_KID}, {JWT_DB_COL_TOKEN} "
373
373
  f"FROM {JWT_DB_TABLE}",
374
374
  where_data={JWT_DB_COL_ACCOUNT: account_id},
375
- connection=db_conn)
375
+ connection=db_conn,
376
+ committable=False,
377
+ logger=logger)
376
378
  if errors:
377
379
  raise RuntimeError("; ".join(errors))
378
380
 
@@ -409,6 +411,7 @@ def _jwt_persist_token(errors: list[str],
409
411
  delete_stmt=f"DELETE FROM {JWT_DB_TABLE}",
410
412
  where_data={JWT_DB_COL_KID: expired},
411
413
  connection=db_conn,
414
+ committable=False,
412
415
  logger=logger)
413
416
  if errors:
414
417
  raise RuntimeError("; ".join(errors))
@@ -422,6 +425,7 @@ def _jwt_persist_token(errors: list[str],
422
425
  delete_stmt=f"DELETE FROM {JWT_DB_TABLE}",
423
426
  where_data={JWT_DB_COL_KID: oldest_id},
424
427
  connection=db_conn,
428
+ committable=False,
425
429
  logger=logger)
426
430
  if errors:
427
431
  raise RuntimeError("; ".join(errors))
@@ -436,6 +440,7 @@ def _jwt_persist_token(errors: list[str],
436
440
  JWT_DB_COL_ALGORITHM: JWT_DEFAULT_ALGORITHM,
437
441
  JWT_DB_COL_DECODER: urlsafe_b64encode(JWT_DECODING_KEY).decode()},
438
442
  connection=db_conn,
443
+ committable=False,
439
444
  logger=logger)
440
445
  if errors:
441
446
  raise RuntimeError("; ".join(errors))
@@ -446,6 +451,7 @@ def _jwt_persist_token(errors: list[str],
446
451
  f"FROM {JWT_DB_TABLE}",
447
452
  where_data={JWT_DB_COL_TOKEN: jwt_token},
448
453
  connection=db_conn,
454
+ committable=False,
449
455
  logger=logger)
450
456
  if errors:
451
457
  raise RuntimeError("; ".join(errors))
File without changes
File without changes
File without changes