pypomes-jwt 1.2.4__tar.gz → 1.2.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.4
2
2
  Name: pypomes_jwt
3
- Version: 1.2.4
3
+ Version: 1.2.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 = "1.2.4"
9
+ version = "1.2.6"
10
10
  authors = [
11
11
  { name="GT Nunes", email="wisecoder01@gmail.com" }
12
12
  ]
@@ -1,26 +1,26 @@
1
1
  from .jwt_config import (
2
2
  JwtConfig, JwtDbConfig, JwtAlgorithm
3
3
  )
4
- from .jwt_external import (
5
- provider_register, provider_get_token
6
- )
7
4
  from .jwt_pomes import (
8
5
  jwt_needed, jwt_verify_request,
9
6
  jwt_assert_account, jwt_set_account, jwt_remove_account,
10
7
  jwt_issue_token, jwt_issue_tokens, jwt_refresh_tokens,
11
8
  jwt_get_claims, jwt_validate_token, jwt_revoke_token
12
9
  )
10
+ from .jwt_providers import (
11
+ provider_register, provider_get_token
12
+ )
13
13
 
14
14
  __all__ = [
15
15
  # jwt_constants
16
16
  "JwtConfig", "JwtDbConfig", "JwtAlgorithm",
17
- # jwt_external
18
- "provider_register", "provider_get_token",
19
17
  # jwt_pomes
20
18
  "jwt_needed", "jwt_verify_request",
21
19
  "jwt_assert_account", "jwt_set_account", "jwt_remove_account",
22
20
  "jwt_issue_token", "jwt_issue_tokens", "jwt_refresh_tokens",
23
- "jwt_get_claims", "jwt_validate_token", "jwt_revoke_token"
21
+ "jwt_get_claims", "jwt_validate_token", "jwt_revoke_token",
22
+ # jwt_providers
23
+ "provider_register", "provider_get_token"
24
24
  ]
25
25
 
26
26
  from importlib.metadata import version
@@ -3,18 +3,20 @@ import sys
3
3
  from base64 import b64encode
4
4
  from datetime import datetime
5
5
  from logging import Logger
6
- from pypomes_core import TZ_LOCAL, Mimetype, exc_format
6
+ from pypomes_core import TZ_LOCAL, exc_format
7
7
  from requests import Response
8
8
  from typing import Any
9
9
 
10
10
  # structure:
11
11
  # {
12
12
  # <provider-id>: {
13
- # "url": <access-url>,
14
- # "grant-type": <type-of-grant-to-request>,
15
- # "user": <basic-auth-user>,
16
- # "pwd": <basic-auth-pwd>,
17
- # "token": <auth-token>,
13
+ # "url": <strl>,
14
+ # "user": <str>,
15
+ # "pwd": <str>,
16
+ # "basic-auth": <bool>,
17
+ # "headers-data": <dict[str, str]>,
18
+ # "body-data": <dict[str, str],
19
+ # "token": <str>,
18
20
  # "expiration": <timestamp>
19
21
  # }
20
22
  # }
@@ -23,30 +25,38 @@ _provider_registry: dict[str, dict[str, Any]] = {}
23
25
 
24
26
  def provider_register(provider_id: str,
25
27
  access_url: str,
26
- grant_type: str,
27
28
  auth_user: str,
28
29
  auth_pwd: str,
29
- client_id: str = None,
30
- use_header: bool = None) -> None:
30
+ custom_auth: tuple[str, str] = None,
31
+ headers_data: dict[str, str] = None,
32
+ body_data: dict[str, str] = None) -> None:
31
33
  """
32
- Register an external token provider.
34
+ Register an external authentication token provider.
35
+
36
+ If specified, *custom_auth* provides key names for sending credentials (username and password, in this order)
37
+ as key-value pairs in the body of the request. Otherwise, the external provider *provider_id* uses the standard
38
+ HTTP Basic Authorization scheme, wherein the credentials are B64-encoded and send in the request headers.
39
+
40
+ Optional constant key-value pairs (such as ['Content-Type', 'application/x-www-form-urlencoded']), to be
41
+ added to the request headers, may be specified in *header_data*. Likewise, optional constant key-value pairs
42
+ (such as ['grant-type', 'client_crdentials']), to be added to the request body, may be specified in *body_data*.
33
43
 
34
44
  :param provider_id: the provider's identification
35
- :param grant_type: the type of grant to request (typically, 'client_credentials' or 'password')
36
- :param access_url: the url to request tokens with
45
+ :param access_url: the url to request authentication tokens with
37
46
  :param auth_user: the basic authorization user
38
47
  :param auth_pwd: the basic authorization password
39
- :param client_id: optional client id to add to the request body
40
- :param use_header: use HTTP header on the request
48
+ :param custom_auth: optional key names for sending the credentials as key-value pairs in the body of the request
49
+ :param headers_data: optional key-value pairs to be added to the request headers
50
+ :param body_data: optional key-value pairs to be added to the request body
41
51
  """
42
52
  global _provider_registry # noqa: PLW0602
43
53
  _provider_registry[provider_id] = {
44
54
  "url": access_url,
45
- "grant_type": grant_type,
46
55
  "user": auth_user,
47
56
  "pwd": auth_pwd,
48
- "client_id": client_id,
49
- "use_header": use_header,
57
+ "custom-auth": custom_auth,
58
+ "headers-data": headers_data,
59
+ "body-data": body_data,
50
60
  "token": None,
51
61
  "expiration": datetime.now(tz=TZ_LOCAL).timestamp()
52
62
  }
@@ -71,18 +81,17 @@ def provider_get_token(errors: list[str] | None,
71
81
  if provider:
72
82
  now: float = datetime.now(tz=TZ_LOCAL).timestamp()
73
83
  if now > provider.get("expiration"):
74
- data: dict[str, str] = {"grant_type": provider.get("grant-type")}
75
- headers: dict[str, str] = {"Content-Type": Mimetype.URLENCODED}
76
84
  user: str = provider.get("user")
77
85
  pwd: str = provider.get("pwd")
78
- if provider.get("use_header"):
79
- enc_bytes: bytes = b64encode(f"{user}:{pwd}".encode())
80
- headers["Authorization"] = f"Basic {enc_bytes.decode()}"
86
+ headers_data: dict[str, str] = provider.get("headers-data") or {}
87
+ body_data: dict[str, str] = provider.get("body-data") or {}
88
+ custom_auth: tuple[str, str] = provider.get("custom-auth")
89
+ if custom_auth:
90
+ body_data[custom_auth[0]] = user
91
+ body_data[custom_auth[1]] = pwd
81
92
  else:
82
- data["username"] = user
83
- data["password"] = pwd
84
- if provider.get("client_id"):
85
- data["client_id"] = provider.get("client_id")
93
+ enc_bytes: bytes = b64encode(f"{user}:{pwd}".encode())
94
+ headers_data["Authorization"] = f"Basic {enc_bytes.decode()}"
86
95
  url: str = provider.get("url")
87
96
  try:
88
97
  # typical return on a token request:
@@ -92,8 +101,8 @@ def provider_get_token(errors: list[str] | None,
92
101
  # "access_token": <the-token>
93
102
  # }
94
103
  response: Response = requests.post(url=url,
95
- data=data,
96
- headers=headers,
104
+ data=body_data,
105
+ headers=headers_data,
97
106
  timeout=None)
98
107
  if response.status_code < 200 or response.status_code >= 300:
99
108
  # request resulted in error, report the problem
File without changes
File without changes
File without changes