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

pypomes_jwt/__init__.py CHANGED
@@ -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,20 +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
- # "client-id": <client-identification>,
18
- # "use-header": <bool>,
19
- # "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>,
20
20
  # "expiration": <timestamp>
21
21
  # }
22
22
  # }
@@ -25,30 +25,38 @@ _provider_registry: dict[str, dict[str, Any]] = {}
25
25
 
26
26
  def provider_register(provider_id: str,
27
27
  access_url: str,
28
- grant_type: str,
29
28
  auth_user: str,
30
29
  auth_pwd: str,
31
- client_id: str = None,
32
- 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:
33
33
  """
34
34
  Register an external authentication token provider.
35
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*.
43
+
36
44
  :param provider_id: the provider's identification
37
- :param grant_type: the type of grant to request (typically, 'client_credentials' or 'password')
38
45
  :param access_url: the url to request authentication tokens with
39
46
  :param auth_user: the basic authorization user
40
47
  :param auth_pwd: the basic authorization password
41
- :param client_id: optional client identification to add to the request data
42
- :param use_header: add authorization data to HTTP header (defaults to sending them in the body of 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
43
51
  """
44
52
  global _provider_registry # noqa: PLW0602
45
53
  _provider_registry[provider_id] = {
46
54
  "url": access_url,
47
- "grant_type": grant_type,
48
55
  "user": auth_user,
49
56
  "pwd": auth_pwd,
50
- "client-id": client_id,
51
- "use-header": use_header,
57
+ "custom-auth": custom_auth,
58
+ "headers-data": headers_data,
59
+ "body-data": body_data,
52
60
  "token": None,
53
61
  "expiration": datetime.now(tz=TZ_LOCAL).timestamp()
54
62
  }
@@ -75,16 +83,15 @@ def provider_get_token(errors: list[str] | None,
75
83
  if now > provider.get("expiration"):
76
84
  user: str = provider.get("user")
77
85
  pwd: str = provider.get("pwd")
78
- data: dict[str, str] = {"grant-type": provider.get("grant-type")}
79
- headers: dict[str, str] = {"Content-Type": Mimetype.URLENCODED}
80
- if provider.get("use-header"):
81
- enc_bytes: bytes = b64encode(f"{user}:{pwd}".encode())
82
- 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
83
92
  else:
84
- data["username"] = user
85
- data["password"] = pwd
86
- if provider.get("client-id"):
87
- 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()}"
88
95
  url: str = provider.get("url")
89
96
  try:
90
97
  # typical return on a token request:
@@ -94,8 +101,8 @@ def provider_get_token(errors: list[str] | None,
94
101
  # "access_token": <the-token>
95
102
  # }
96
103
  response: Response = requests.post(url=url,
97
- data=data,
98
- headers=headers,
104
+ data=body_data,
105
+ headers=headers_data,
99
106
  timeout=None)
100
107
  if response.status_code < 200 or response.status_code >= 300:
101
108
  # request resulted in error, report the problem
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pypomes_jwt
3
- Version: 1.2.5
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
@@ -0,0 +1,9 @@
1
+ pypomes_jwt/__init__.py,sha256=vXAeaEnuUqpvGtV465TsW2Lf3ihijrMP2Hm4My79y88,968
2
+ pypomes_jwt/jwt_config.py,sha256=3r9XPWXXAG_wKUs_FDZoTj9j6lmTdNymud_q4E4Opk0,3338
3
+ pypomes_jwt/jwt_pomes.py,sha256=em_apYg0ptfa5BvobnfXz7BPh_ghPhXGg7zHFK3A8y4,23901
4
+ pypomes_jwt/jwt_providers.py,sha256=w66KBxGOHIht4eQLfAXAnLabJWjxfubPQk3rWfo_bmY,5787
5
+ pypomes_jwt/jwt_registry.py,sha256=pNBpPiR2xINzNnWu_2dcPtRVfXqqPzMVYCXG1HtatUA,22268
6
+ pypomes_jwt-1.2.6.dist-info/METADATA,sha256=W6rje1AuqC-Tkz8gQSCM0fhi5Vwsb6ToqpwiyYfWnXc,660
7
+ pypomes_jwt-1.2.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ pypomes_jwt-1.2.6.dist-info/licenses/LICENSE,sha256=NdakochSXm_H_-DSL_x2JlRCkYikj3snYYvTwgR5d_c,1086
9
+ pypomes_jwt-1.2.6.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- pypomes_jwt/__init__.py,sha256=XS646zYwxnTHHfqnl5ioRN3YGi5QvIpKfrMZcw-grjM,966
2
- pypomes_jwt/jwt_config.py,sha256=3r9XPWXXAG_wKUs_FDZoTj9j6lmTdNymud_q4E4Opk0,3338
3
- pypomes_jwt/jwt_external.py,sha256=tEMsOO3Q-rbWR-6TR_7yapB7Xe9qDVvrL-h8RbxgB2I,5145
4
- pypomes_jwt/jwt_pomes.py,sha256=em_apYg0ptfa5BvobnfXz7BPh_ghPhXGg7zHFK3A8y4,23901
5
- pypomes_jwt/jwt_registry.py,sha256=pNBpPiR2xINzNnWu_2dcPtRVfXqqPzMVYCXG1HtatUA,22268
6
- pypomes_jwt-1.2.5.dist-info/METADATA,sha256=Ib7ZJwqF9ZzIENLC27xxddSw-_oPIlnDnftKEVxvs6c,660
7
- pypomes_jwt-1.2.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- pypomes_jwt-1.2.5.dist-info/licenses/LICENSE,sha256=NdakochSXm_H_-DSL_x2JlRCkYikj3snYYvTwgR5d_c,1086
9
- pypomes_jwt-1.2.5.dist-info/RECORD,,