pypomes-iam 0.2.8__tar.gz → 0.3.0__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-iam might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pypomes_iam
3
- Version: 0.2.8
3
+ Version: 0.3.0
4
4
  Summary: A collection of Python pomes, penyeach (IAM modules)
5
5
  Project-URL: Homepage, https://github.com/TheWiseCoder/PyPomes-IAM
6
6
  Project-URL: Bug Tracker, https://github.com/TheWiseCoder/PyPomes-IAM/issues
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "pypomes_iam"
9
- version = "0.2.8"
9
+ version = "0.3.0"
10
10
  authors = [
11
11
  { name="GT Nunes", email="wisecoder01@gmail.com" }
12
12
  ]
@@ -1,3 +1,5 @@
1
+ import json
2
+ import requests
1
3
  from flask import Response, request, jsonify
2
4
  from logging import Logger
3
5
  from typing import Any
@@ -103,14 +105,52 @@ def service_callback() -> Response:
103
105
  args=request.args,
104
106
  errors=errors,
105
107
  logger=logger)
108
+ # exchange the token
109
+ if request.endpoint.startswith("jusbr-"):
110
+ keycloak_registry: dict[str, Any] = __get_iam_registry(endpoint="keycloak-token")
111
+ payload: dict[str, str] = {
112
+ "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
113
+ "subject_token": token_data[1],
114
+ "subject_token_type": "urn:ietf:params:oauth:token-type:access_token",
115
+ "client_id": keycloak_registry["client-id"],
116
+ "client_secret": keycloak_registry["client-secret"],
117
+ "requested_token_type": "urn:ietf:params:oauth:token-type:access_token",
118
+ "audience": token_data[0],
119
+ "subject_issuer": "oidc"
120
+ }
121
+ exchange_url = f"{keycloak_registry['base-url']}/protocol/openid-connect/token"
122
+ if logger:
123
+ logger.debug(msg=f"POST '{exchange_url}', data {json.dumps(obj=payload,
124
+ ensure_ascii=False)}")
125
+ headers: dict[str, str] = {
126
+ "Content-Type": "application/x-www-form-urlencoded"
127
+ }
128
+ response: requests.Response = requests.post(url=exchange_url,
129
+ data=payload,
130
+ headers=headers)
131
+ if response.status_code == 200:
132
+ # request succeeded
133
+ if logger:
134
+ logger.debug(msg=f"POST success, status {response.status_code}")
135
+ reply: dict[str, Any] = response.json()
136
+ token_data = (token_data[0], reply.get("access_token"))
137
+ else:
138
+ # request resulted in error
139
+ err_msg = f"POST failure, status {response.status_code}, reason '{response.reason}'"
140
+ if hasattr(response, "content") and response.content:
141
+ err_msg += f", content '{response.content}'"
142
+ errors.append(err_msg)
143
+
106
144
  result: Response
107
145
  if errors:
108
146
  result = jsonify({"errors": "; ".join(errors)})
109
147
  result.status_code = 400
148
+ if logger:
149
+ logger.error(msg=json.dumps(obj=result))
110
150
  else:
111
151
  result = jsonify({
112
- "user_id": token_data[0],
113
- "access_token": token_data[1]})
152
+ "user-id": token_data[0],
153
+ "access-token": token_data[1]})
114
154
 
115
155
  # log the response
116
156
  if logger:
File without changes
File without changes
File without changes