pypomes-jwt 0.8.0__py3-none-any.whl → 0.8.2__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.

Potentially problematic release.


This version of pypomes-jwt might be problematic. Click here for more details.

@@ -47,7 +47,8 @@ JWT_DB_ENGINE: Final[DbEngine] = DbEngine(__db_engine) if __db_engine else None
47
47
 
48
48
  # one of HS256, HS512, RSA256, RSA512
49
49
  JWT_DEFAULT_ALGORITHM: Final[str] = env_get_str(key=f"{APP_PREFIX}_JWT_DEFAULT_ALGORITHM",
50
- def_value="HS256")
50
+ def_value="RS256")
51
+
51
52
  # recommended: between 5 min and 1 hour (set to 5 min)
52
53
  JWT_ACCESS_MAX_AGE: Final[int] = env_get_int(key=f"{APP_PREFIX}_JWT_ACCESS_MAX_AGE",
53
54
  def_value=300)
pypomes_jwt/jwt_data.py CHANGED
@@ -239,8 +239,8 @@ class JwtData:
239
239
  just_now: int = int(datetime.now(tz=timezone.utc).timestamp())
240
240
  current_claims["iat"] = just_now
241
241
  token_header: dict[str, Any] = None \
242
- if JWT_DEFAULT_ALGORITHM not in ["RSA256", "RSA512"] \
243
- else {"kid": JWT_DECODING_KEY}
242
+ if JWT_DEFAULT_ALGORITHM not in ["RS256", "RS512"] \
243
+ else {"kid": JWT_DECODING_KEY.hex()}
244
244
 
245
245
  # issue the access token first
246
246
  current_claims["nat"] = "A"
@@ -397,8 +397,9 @@ def _jwt_persist_token(errors: list[str],
397
397
  # ruff: noqa: S324
398
398
  hasher = hashlib.new(name="md5",
399
399
  data=jwt_token.encode())
400
- token_hash: str = hasher.digest().decode()
400
+ token_hash: str = hasher.digest().hex()
401
401
  db_insert(errors=errors,
402
402
  insert_stmt=f"INSERT INTO {JWT_DB_TABLE}",
403
- insert_data={"ds_hash": token_hash,
404
- "ds_token": jwt_token})
403
+ insert_data={JWT_DB_COL_ACCOUNT: account_id,
404
+ JWT_DB_COL_HASH: token_hash,
405
+ JWT_DB_COL_TOKEN: jwt_token})
pypomes_jwt/jwt_pomes.py CHANGED
@@ -7,7 +7,7 @@ from typing import Any, Literal
7
7
  from .jwt_constants import (
8
8
  JWT_ACCESS_MAX_AGE, JWT_REFRESH_MAX_AGE,
9
9
  JWT_DEFAULT_ALGORITHM, JWT_DECODING_KEY,
10
- JWT_DB_ENGINE, JWT_DB_TABLE, JWT_DB_COL_ACCOUNT, JWT_DB_COL_TOKEN
10
+ JWT_DB_ENGINE, JWT_DB_TABLE, JWT_DB_COL_HASH
11
11
  )
12
12
  from .jwt_data import JwtData
13
13
 
@@ -185,6 +185,17 @@ def jwt_validate_token(errors: list[str] | None,
185
185
  if nature and nature != claims.get("nat"):
186
186
  nat: str = "an access" if nature == "A" else "a refresh"
187
187
  err_msg = f"Token is not {nat} token"
188
+ elif JWT_DB_ENGINE and claims.get("nat") == "R":
189
+ from pypomes_db import db_exists
190
+ # ruff: noqa: S324
191
+ hasher = hashlib.new(name="md5",
192
+ data=token.encode())
193
+ token_hash: str = hasher.digest().hex()
194
+ if not db_exists(errors=errors,
195
+ table=JWT_DB_TABLE,
196
+ where_data={JWT_DB_COL_HASH: token_hash},
197
+ logger=logger):
198
+ err_msg = "Token is not valid"
188
199
  except Exception as e:
189
200
  err_msg = str(e)
190
201
 
@@ -226,14 +237,14 @@ def jwt_revoke_token(errors: list[str] | None,
226
237
  # ruff: noqa: S324
227
238
  hasher = hashlib.new(name="md5",
228
239
  data=refresh_token.encode())
229
- token_hash: str = hasher.digest().decode()
240
+ token_hash: str = hasher.digest().hex()
230
241
  if db_exists(errors=op_errors,
231
242
  table=JWT_DB_TABLE,
232
- where_data={"ds_hash": token_hash},
243
+ where_data={JWT_DB_COL_HASH: token_hash},
233
244
  logger=logger):
234
245
  db_delete(errors=errors,
235
246
  delete_stmt=f"DELETE FROM {JWT_DB_TABLE}",
236
- where_data={"ds_hash": token_hash},
247
+ where_data={JWT_DB_COL_HASH: token_hash},
237
248
  logger=logger)
238
249
  elif not op_errors:
239
250
  op_errors.append("Token was not found")
@@ -286,19 +297,21 @@ def jwt_get_tokens(errors: list[str] | None,
286
297
  if refresh_token:
287
298
  # verify whether this refresh token is legitimate
288
299
  if JWT_DB_ENGINE:
289
- from pypomes_db import db_select
290
- recs: list[tuple[str]] = db_select(errors=op_errors,
291
- sel_stmt=f"SELECT {JWT_DB_COL_TOKEN} "
292
- f"FROM {JWT_DB_TABLE}",
293
- where_data={JWT_DB_COL_ACCOUNT: account_id},
294
- logger=logger)
295
- if not op_errors and \
296
- (len(recs) == 0 or recs[0][0] != refresh_token):
300
+ from pypomes_db import db_exists
301
+ # ruff: noqa: S324
302
+ hasher = hashlib.new(name="md5",
303
+ data=refresh_token.encode())
304
+ token_hash: str = hasher.digest().hex()
305
+ if db_exists(errors=op_errors,
306
+ table=JWT_DB_TABLE,
307
+ where_data={JWT_DB_COL_HASH: token_hash},
308
+ logger=logger) is False:
297
309
  op_errors.append("Invalid refresh token")
310
+
298
311
  if not op_errors:
299
312
  account_claims = jwt_get_claims(errors=op_errors,
300
313
  token=refresh_token)
301
- if not op_errors and account_claims.get("nat") != "R":
314
+ if not op_errors and (account_claims.get("payload") or {}).get("nat") != "R":
302
315
  op_errors.append("Invalid parameters")
303
316
 
304
317
  if not op_errors:
@@ -336,7 +349,7 @@ def jwt_get_claims(errors: list[str] | None,
336
349
  Structure of the returned data:
337
350
  {
338
351
  "header": {
339
- "alg": "HS256",
352
+ "alg": "RS256",
340
353
  "typ": "JWT",
341
354
  "kid": "rt466ytRTYH64577uydhDFGHDYJH2341"
342
355
  },
@@ -347,10 +360,10 @@ def jwt_get_claims(errors: list[str] | None,
347
360
  "iat": 1516239022,
348
361
  "iss": "https://my_id_provider/issue",
349
362
  "jti": "Uhsdfgr67FGH567qwSDF33er89retert",
350
- "gender": "M,
363
+ "gender": "M",
351
364
  "name": "John Doe",
352
365
  "nbt": 1516249022
353
- "sub": "1234567890",
366
+ "sub": "11111111111",
354
367
  "roles": [
355
368
  "administrator",
356
369
  "operator"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pypomes_jwt
3
- Version: 0.8.0
3
+ Version: 0.8.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
@@ -0,0 +1,8 @@
1
+ pypomes_jwt/__init__.py,sha256=06WdwiP2m5jtrFjpPSacg4fRd2Dh6gVo93xJhmu73J4,1134
2
+ pypomes_jwt/jwt_constants.py,sha256=EjdrTP5AptGoOdI0gzsxexmM4lrgm2r0KHX-DyyGhFc,4330
3
+ pypomes_jwt/jwt_data.py,sha256=d11IsRLKF7_3RTfm5ju-U--eCHJemD50OzQBOzFNtYQ,19243
4
+ pypomes_jwt/jwt_pomes.py,sha256=hsWrlq_9OqcScS1fPKFl5yxxjicj_AAE2Z5NfKicDkw,15686
5
+ pypomes_jwt-0.8.2.dist-info/METADATA,sha256=gHPs2FSSALkn4gsXnCXnbNBIjDYt7a4QxMY11NYBvb8,599
6
+ pypomes_jwt-0.8.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ pypomes_jwt-0.8.2.dist-info/licenses/LICENSE,sha256=NdakochSXm_H_-DSL_x2JlRCkYikj3snYYvTwgR5d_c,1086
8
+ pypomes_jwt-0.8.2.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- pypomes_jwt/__init__.py,sha256=06WdwiP2m5jtrFjpPSacg4fRd2Dh6gVo93xJhmu73J4,1134
2
- pypomes_jwt/jwt_constants.py,sha256=6-Jw4ORgf32hRWnaGyVISXMJMtTBk7LdKl3RrDy7Ll0,4328
3
- pypomes_jwt/jwt_data.py,sha256=gyhGquSQbHevOKIoXmAmjMSwCjXB7pYbI2sY-7sGGO8,19158
4
- pypomes_jwt/jwt_pomes.py,sha256=PPx-JTlR2dVsLUFlkCiZCxYOnH-BbwqpptBDHO_PIfI,15213
5
- pypomes_jwt-0.8.0.dist-info/METADATA,sha256=C2fRw5H6are30XZfHLK5IpyqNFlL59Kt9Z0VtJXF07E,599
6
- pypomes_jwt-0.8.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- pypomes_jwt-0.8.0.dist-info/licenses/LICENSE,sha256=NdakochSXm_H_-DSL_x2JlRCkYikj3snYYvTwgR5d_c,1086
8
- pypomes_jwt-0.8.0.dist-info/RECORD,,