pypomes-jwt 0.4.5__tar.gz → 0.4.6__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.3
2
2
  Name: pypomes_jwt
3
- Version: 0.4.5
3
+ Version: 0.4.6
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 = "0.4.5"
9
+ version = "0.4.6"
10
10
  authors = [
11
11
  { name="GT Nunes", email="wisecoder01@gmail.com" }
12
12
  ]
@@ -85,7 +85,8 @@ class JwtData:
85
85
  :param logger: optional logger
86
86
  """
87
87
  # obtain the item in s"torage
88
- item_data: dict[str, dict[str, Any]] = self.retrieve_access_data(service_url=service_url)
88
+ item_data: dict[str, dict[str, Any]] = self.retrieve_access_data(service_url=service_url,
89
+ logger=logger)
89
90
  if not item_data:
90
91
  # build control data
91
92
  control_data: dict[str, Any] = {
@@ -122,9 +123,9 @@ class JwtData:
122
123
  with self.access_lock:
123
124
  self.access_data.append(item_data)
124
125
  if logger:
125
- logger.debug(f"JWT token data added to storage for '{service_url}'")
126
+ logger.debug(f"JWT data added for '{service_url}': {item_data}")
126
127
  elif logger:
127
- logger.warning(f"JWT token data already exists for '{service_url}'")
128
+ logger.warning(f"JWT data already exists for '{service_url}'")
128
129
 
129
130
  def remove_access_data(self,
130
131
  service_url: str,
@@ -136,33 +137,15 @@ class JwtData:
136
137
  :param logger: optional logger
137
138
  """
138
139
  # obtain the item in storage
139
- item_data: dict[str, dict[str, Any]] = self.retrieve_access_data(service_url=service_url)
140
+ item_data: dict[str, dict[str, Any]] = self.retrieve_access_data(service_url=service_url,
141
+ logger=logger)
140
142
  if item_data:
141
143
  with self.access_lock:
142
144
  self.access_data.remove(item_data)
143
145
  if logger:
144
- logger.debug(f"Removed access data for '{service_url}'")
146
+ logger.debug(f"Removed JWT data for '{service_url}'")
145
147
  elif logger:
146
- logger.warning(f"No access data found for '{service_url}'")
147
-
148
- def retrieve_access_data(self,
149
- service_url: str) -> dict[str, dict[str, Any]]:
150
- """
151
- Retrieve and return the access data in storage corresponding to the given parameters.
152
-
153
- :param service_url: the reference URL for obtaining JWT tokens
154
- :return: the corresponding item in storage, or 'None' if not found
155
- """
156
- # initialize the return variable
157
- result: dict[str, dict[str, Any]] | None = None
158
-
159
- with self.access_lock:
160
- for item_data in self.access_data:
161
- if service_url == item_data.get("control-data").get("service-url"):
162
- result = item_data
163
- break
164
-
165
- return result
148
+ logger.warning(f"No JWT data found for '{service_url}'")
166
149
 
167
150
  def get_token_data(self,
168
151
  service_url: str,
@@ -196,7 +179,8 @@ class JwtData:
196
179
  result: dict[str, Any]
197
180
 
198
181
  # obtain the item in storage
199
- item_data: dict[str, Any] = self.retrieve_access_data(service_url=service_url)
182
+ item_data: dict[str, Any] = self.retrieve_access_data(service_url=service_url,
183
+ logger=logger)
200
184
  # was the JWT data obtained ?
201
185
  if item_data:
202
186
  # yes, proceed
@@ -250,7 +234,7 @@ class JwtData:
250
234
  }
251
235
  else:
252
236
  # JWT data not found
253
- err_msg: str = f"No access data found for {service_url}"
237
+ err_msg: str = f"No JWT data found for {service_url}"
254
238
  if logger:
255
239
  logger.error(err_msg)
256
240
  raise RuntimeError(err_msg)
@@ -258,11 +242,13 @@ class JwtData:
258
242
  return result
259
243
 
260
244
  def get_token_claims(self,
261
- token: str) -> dict[str, Any]:
245
+ token: str,
246
+ logger: Logger = None) -> dict[str, Any]:
262
247
  """
263
248
  Obtain and return the claims of a JWT *token*.
264
249
 
265
250
  :param token: the token to be inspected for claims
251
+ :param logger: optional logger
266
252
  :return: the token's claimset, or 'None' if error
267
253
  :raises InvalidTokenError: token is not valid
268
254
  :raises ExpiredSignatureError: token has expired
@@ -278,11 +264,40 @@ class JwtData:
278
264
  break
279
265
 
280
266
  if not algorithm or not key:
281
- raise InvalidTokenError("Token is not valid")
267
+ raise InvalidTokenError("JWT token is not valid")
282
268
 
283
- return jwt.decode(jwt=token,
284
- key=key,
285
- algorithms=[algorithm])
269
+ if logger:
270
+ logger.debug(msg=f"Retrieve claims for JWT token '{token}'")
271
+ result: dict[str, Any] = jwt.decode(jwt=token,
272
+ key=key,
273
+ algorithms=[algorithm])
274
+ if logger:
275
+ logger.debug(f"Retrieved claims for JWT token '{token}': {result}")
276
+
277
+ return result
278
+
279
+ def retrieve_access_data(self,
280
+ service_url: str,
281
+ logger: Logger = None) -> dict[str, dict[str, Any]]:
282
+ """
283
+ Retrieve and return the access data in storage corresponding to the given parameters.
284
+
285
+ :param service_url: the reference URL for obtaining JWT tokens
286
+ :param logger: optional logger
287
+ :return: the corresponding item in storage, or 'None' if not found
288
+ """
289
+ # initialize the return variable
290
+ result: dict[str, dict[str, Any]] | None = None
291
+
292
+ with self.access_lock:
293
+ for item_data in self.access_data:
294
+ if service_url == item_data.get("control-data").get("service-url"):
295
+ result = item_data
296
+ break
297
+ if logger:
298
+ logger.debug(f"JWT data for '{service_url}': {result}")
299
+
300
+ return result
286
301
 
287
302
 
288
303
  def jwt_request_token(errors: list[str],
@@ -312,7 +327,7 @@ def jwt_request_token(errors: list[str],
312
327
 
313
328
  # request the JWT token
314
329
  if logger:
315
- logger.debug(f"POST requesting JWT token to {service_url}")
330
+ logger.debug(f"POST request JWT token to '{service_url}'")
316
331
  response: Response = requests.post(
317
332
  url=service_url,
318
333
  json=claims,
@@ -324,7 +339,7 @@ def jwt_request_token(errors: list[str],
324
339
  # yes, save the access token data returned
325
340
  result = response.json()
326
341
  if logger:
327
- logger.debug(f"Access token obtained: {result}")
342
+ logger.debug(f"JWT token obtained: {result}")
328
343
  else:
329
344
  # no, report the problem
330
345
  err_msg: str = f"POST request of '{service_url}' failed: {response.reason}"
@@ -339,7 +354,8 @@ def jwt_request_token(errors: list[str],
339
354
 
340
355
  def jwt_validate_token(token: str,
341
356
  key: bytes | str,
342
- algorithm: str) -> None:
357
+ algorithm: str,
358
+ logger: Logger = None) -> None:
343
359
  """
344
360
  Verify if *token* ia a valid JWT token.
345
361
 
@@ -348,11 +364,14 @@ def jwt_validate_token(token: str,
348
364
  :param token: the token to be validated
349
365
  :param key: the secret or public key used to create the token (HS or RSA authentication, respectively)
350
366
  :param algorithm: the algorithm used to to sign the token with
367
+ :param logger: optional logger
351
368
  :raises InvalidTokenError: token is invalid
352
369
  :raises InvalidKeyError: authentication key is not in the proper format
353
370
  :raises ExpiredSignatureError: token and refresh period have expired
354
371
  :raises InvalidSignatureError: signature does not match the one provided as part of the token
355
372
  """
373
+ if logger:
374
+ logger.debug(msg=f"Verify request for JWT token '{token}'")
356
375
  jwt.decode(jwt=token,
357
376
  key=key,
358
377
  algorithms=[algorithm])
File without changes
File without changes
File without changes
File without changes