stackit-core 0.2.0__py3-none-any.whl → 0.3.1__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.
@@ -42,6 +42,7 @@ class KeyAuth(AuthBase):
42
42
  DEFAULT_TOKEN_ENDPOINT = "https://service-account.api.stackit.cloud/token" # noqa S105 false positive
43
43
  TOKEN_EXPIRY_CHECK_INTERVAL = timedelta(seconds=60)
44
44
  EXPIRATION_LEEWAY = timedelta(minutes=5)
45
+ MAX_REFRESH_RETRIES = 3
45
46
 
46
47
  timeout: Optional[int] = 30
47
48
  initial_token: Optional[str]
@@ -73,9 +74,21 @@ class KeyAuth(AuthBase):
73
74
 
74
75
  def __call__(self, r: Request) -> Request:
75
76
  with self.lock:
77
+ if self.refresh_future is not None and self.refresh_future.done():
78
+ self.refresh_future.result()
76
79
  if self.__is_token_expired(self.access_token):
77
80
  if self.refresh_future is None or self.refresh_future.done():
78
81
  self.refresh_future = self.executor.submit(self.__refresh_token)
82
+ refresh_future = self.refresh_future
83
+ else:
84
+ refresh_future = None
85
+
86
+ # Do not hold the lock while waiting. The refresh worker acquires it when
87
+ # updating the access token.
88
+ if refresh_future is not None:
89
+ refresh_future.result()
90
+
91
+ with self.lock:
79
92
  r.headers["Authorization"] = f"Bearer {self.access_token}"
80
93
  return r
81
94
 
@@ -105,16 +118,19 @@ class KeyAuth(AuthBase):
105
118
  response = requests.post(self.token_endpoint, data=body, timeout=self.timeout)
106
119
  response.raise_for_status()
107
120
  response_json = response.json()
108
- self.access_token = response_json["access_token"]
109
- self.refresh_token = response_json["refresh_token"]
121
+ with self.lock:
122
+ self.access_token = response_json["access_token"]
123
+ self.refresh_token = response_json["refresh_token"]
110
124
  except requests.RequestException as e:
111
- print(f"Initial token fetch failed: {e}")
125
+ raise requests.RequestException("Initial token fetch failed") from e
112
126
 
113
127
  def __start_token_refresh_task(self):
114
128
  def token_refresh_task():
115
129
  while True:
116
130
  time.sleep(self.TOKEN_EXPIRY_CHECK_INTERVAL.total_seconds())
117
131
  with self.lock:
132
+ if self.refresh_future is not None and self.refresh_future.done():
133
+ self.refresh_future.result()
118
134
  if self.__is_token_expired(self.access_token) and (
119
135
  self.refresh_future is None or self.refresh_future.done()
120
136
  ):
@@ -125,38 +141,43 @@ class KeyAuth(AuthBase):
125
141
  thread.start()
126
142
 
127
143
  def __refresh_token(self):
128
- if self.__is_token_expired(self.refresh_token):
144
+ with self.lock:
145
+ refresh_token = self.refresh_token
146
+
147
+ if self.__is_token_expired(refresh_token):
129
148
  self.__create_initial_token()
130
149
  self.__fetch_token_from_endpoint()
131
150
  return
132
151
 
133
152
  body = {
134
153
  "grant_type": "refresh_token",
135
- "refresh_token": self.refresh_token,
154
+ "refresh_token": refresh_token,
136
155
  }
137
156
 
138
- try:
139
- response = requests.post(self.token_endpoint, data=body, timeout=self.timeout)
140
- response.raise_for_status()
141
- response_data = response.json()
142
- new_token = response_data.get("access_token")
143
- # with self.lock:
144
- self.access_token = new_token
145
- print("Token successfully refreshed!")
146
- except requests.RequestException as e:
147
- print(f"Token refresh failed: {e}")
157
+ last_exception = None
158
+ for _ in range(self.MAX_REFRESH_RETRIES):
159
+ try:
160
+ response = requests.post(self.token_endpoint, data=body, timeout=self.timeout)
161
+ response.raise_for_status()
162
+ response_data = response.json()
163
+ new_token = response_data.get("access_token")
164
+ with self.lock:
165
+ self.access_token = new_token
166
+ return
167
+ except requests.RequestException as e:
168
+ last_exception = e
169
+
170
+ raise requests.RequestException("Token refresh failed after retries") from last_exception
148
171
 
149
- def __is_token_expired(self, token: str) -> bool:
172
+ def __is_token_expired(self, token: Optional[str]) -> bool:
150
173
  try:
151
174
  decoded_token = jwt.decode(token, options={"verify_signature": False})
152
175
  exp = decoded_token.get("exp")
153
- if exp:
154
- return time.time() > (exp + self.EXPIRATION_LEEWAY.total_seconds())
155
- except jwt.ExpiredSignatureError:
156
- return True
157
- except jwt.DecodeError:
176
+ if exp is None:
177
+ return True
178
+ return time.time() > (float(exp) - self.EXPIRATION_LEEWAY.total_seconds())
179
+ except (jwt.InvalidTokenError, TypeError, ValueError):
158
180
  return True
159
- return False
160
181
 
161
182
  def __shutdown(self):
162
183
  self.executor.shutdown(wait=False)
@@ -1,24 +1,26 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.5
2
2
  Name: stackit-core
3
- Version: 0.2.0
3
+ Version: v0.3.1
4
4
  Summary: Core functionality for the STACKIT SDK for Python
5
- Author: STACKIT Developer Tools
6
- Author-email: developer-tools@stackit.cloud
7
- Requires-Python: >=3.8,<4.0
5
+ Project-URL: Homepage, https://github.com/stackitcloud/stackit-sdk-python
6
+ Project-URL: Issues, https://github.com/stackitcloud/stackit-sdk-python/issues
7
+ Author-email: STACKIT Developer Tools <developer-tools@stackit.cloud>
8
+ License-File: LICENSE.md
9
+ License-File: NOTICE.txt
8
10
  Classifier: License :: OSI Approved :: Apache Software License
9
11
  Classifier: Operating System :: OS Independent
10
12
  Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.8
12
- Classifier: Programming Language :: Python :: 3.9
13
13
  Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
16
  Classifier: Programming Language :: Python :: 3.13
17
- Requires-Dist: cryptography (>=43.0.1)
18
- Requires-Dist: pydantic (>=2.9.2)
19
- Requires-Dist: pyjwt (>=2.9.0)
20
- Requires-Dist: requests (>=2.32.3)
21
- Requires-Dist: urllib3 (>=2.2.3)
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Requires-Python: <4.0,>=3.10
19
+ Requires-Dist: cryptography>=46.0.5
20
+ Requires-Dist: pydantic>=2.9.2
21
+ Requires-Dist: pyjwt>=2.13.0
22
+ Requires-Dist: requests>=2.33.0
23
+ Requires-Dist: urllib3>=2.7.0
22
24
  Description-Content-Type: text/markdown
23
25
 
24
26
  > ⓘ INFO: The STACKIT Python SDK is in beta and in active development.
@@ -34,4 +36,3 @@ to use the [STACKIT Python SDK](https://github.com/stackitcloud/stackit-sdk-pyth
34
36
  ## Usage
35
37
 
36
38
  The core package does nothing on its own and must be used in conjunction with the services of the [STACKIT Python SDK](https://github.com/stackitcloud/stackit-sdk-python).
37
-
@@ -0,0 +1,10 @@
1
+ stackit/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ stackit/core/authorization.py,sha256=36fyp2JKmJ6R2BZGJofXQi0nuY7jcY-dB6QF-I8dED8,4769
3
+ stackit/core/configuration.py,sha256=aQEPK81h9RpJ4A_xjZZTiOBymbBWfaR9t4UJoh4vQDI,2818
4
+ stackit/core/auth_methods/key_auth.py,sha256=yhnY4v8HEUt2RcJ7WlI7VvdLGqgjY5Bh5VDCLGgYC2E,6801
5
+ stackit/core/auth_methods/token_auth.py,sha256=5QuqMWSGkQtmueqECXoJQioXTQROh_JKkgvev1B5Phs,342
6
+ stackit_core-0.3.1.dist-info/METADATA,sha256=Chznw_UZt8cc3uRoAg3b3MCsSG4A6uXQpkdFxB65pRU,1496
7
+ stackit_core-0.3.1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
8
+ stackit_core-0.3.1.dist-info/licenses/LICENSE.md,sha256=3dF8Tb7yZn2tS4zyNa-yNe-68pH8qyWdGz4ioMd3MgE,10933
9
+ stackit_core-0.3.1.dist-info/licenses/NOTICE.txt,sha256=TaVw_C4ro-FZROF4NZ3ygwgwF-2QkyySIP9_CPy3kAg,56
10
+ stackit_core-0.3.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.1
2
+ Generator: hatchling 1.32.4
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,10 +0,0 @@
1
- stackit/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- stackit/core/auth_methods/key_auth.py,sha256=RUiHk5n3lIWAZxVwMr-9XLRmLJjfy9q4AXOAHRdPfYM,5890
3
- stackit/core/auth_methods/token_auth.py,sha256=5QuqMWSGkQtmueqECXoJQioXTQROh_JKkgvev1B5Phs,342
4
- stackit/core/authorization.py,sha256=36fyp2JKmJ6R2BZGJofXQi0nuY7jcY-dB6QF-I8dED8,4769
5
- stackit/core/configuration.py,sha256=aQEPK81h9RpJ4A_xjZZTiOBymbBWfaR9t4UJoh4vQDI,2818
6
- stackit_core-0.2.0.dist-info/LICENSE.md,sha256=3dF8Tb7yZn2tS4zyNa-yNe-68pH8qyWdGz4ioMd3MgE,10933
7
- stackit_core-0.2.0.dist-info/METADATA,sha256=YefBYn5J0kNgZF7kJGdUwyKuKCk1frcjAJgaOwWSOZ0,1361
8
- stackit_core-0.2.0.dist-info/NOTICE.txt,sha256=TaVw_C4ro-FZROF4NZ3ygwgwF-2QkyySIP9_CPy3kAg,56
9
- stackit_core-0.2.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
10
- stackit_core-0.2.0.dist-info/RECORD,,