|
34 | 34 |
|
35 | 35 | # For App authentication, time remaining before token expiration to request a new one |
36 | 36 | ACCESS_TOKEN_REFRESH_THRESHOLD_SECONDS = 20 |
37 | | -TOKEN_REFRESH_THRESHOLD_TIMEDELTA = timedelta( |
38 | | - seconds=ACCESS_TOKEN_REFRESH_THRESHOLD_SECONDS |
39 | | -) |
| 37 | +TOKEN_REFRESH_THRESHOLD_TIMEDELTA = timedelta(seconds=ACCESS_TOKEN_REFRESH_THRESHOLD_SECONDS) |
40 | 38 |
|
41 | 39 |
|
42 | 40 | class Auth(abc.ABC): |
@@ -91,11 +89,7 @@ def token_type(self) -> str: |
91 | 89 |
|
92 | 90 | @property |
93 | 91 | def token(self) -> str: |
94 | | - return ( |
95 | | - base64.b64encode(f"{self.login}:{self.password}".encode()) |
96 | | - .decode("utf-8") |
97 | | - .replace("\n", "") |
98 | | - ) |
| 92 | + return base64.b64encode(f"{self.login}:{self.password}".encode()).decode("utf-8").replace("\n", "") |
99 | 93 |
|
100 | 94 |
|
101 | 95 | class Token(Auth): |
@@ -192,19 +186,15 @@ def create_jwt(self, expiration=None) -> str: |
192 | 186 | """ |
193 | 187 | if expiration is not None: |
194 | 188 | assert isinstance(expiration, int), expiration |
195 | | - assert ( |
196 | | - Consts.MIN_JWT_EXPIRY <= expiration <= Consts.MAX_JWT_EXPIRY |
197 | | - ), expiration |
| 189 | + assert Consts.MIN_JWT_EXPIRY <= expiration <= Consts.MAX_JWT_EXPIRY, expiration |
198 | 190 |
|
199 | 191 | now = int(time.time()) |
200 | 192 | payload = { |
201 | 193 | "iat": now + self._jwt_issued_at, |
202 | 194 | "exp": now + (expiration if expiration is not None else self._jwt_expiry), |
203 | 195 | "iss": self._app_id, |
204 | 196 | } |
205 | | - encrypted = jwt.encode( |
206 | | - payload, key=self.private_key, algorithm=self._jwt_algorithm |
207 | | - ) |
| 197 | + encrypted = jwt.encode(payload, key=self.private_key, algorithm=self._jwt_algorithm) |
208 | 198 |
|
209 | 199 | if isinstance(encrypted, bytes): |
210 | 200 | return encrypted.decode("utf-8") |
@@ -251,9 +241,7 @@ def __init__( |
251 | 241 |
|
252 | 242 | assert isinstance(app_auth, AppAuth), app_auth |
253 | 243 | assert isinstance(installation_id, int), installation_id |
254 | | - assert token_permissions is None or isinstance( |
255 | | - token_permissions, dict |
256 | | - ), token_permissions |
| 244 | + assert token_permissions is None or isinstance(token_permissions, dict), token_permissions |
257 | 245 |
|
258 | 246 | self._app_auth = app_auth |
259 | 247 | self._installation_id = installation_id |
@@ -303,16 +291,11 @@ def token(self) -> str: |
303 | 291 | @property |
304 | 292 | def _is_expired(self) -> bool: |
305 | 293 | assert self.__installation_authorization is not None |
306 | | - token_expires_at = ( |
307 | | - self.__installation_authorization.expires_at |
308 | | - - TOKEN_REFRESH_THRESHOLD_TIMEDELTA |
309 | | - ) |
| 294 | + token_expires_at = self.__installation_authorization.expires_at - TOKEN_REFRESH_THRESHOLD_TIMEDELTA |
310 | 295 | return token_expires_at < datetime.now(timezone.utc) |
311 | 296 |
|
312 | 297 | def _get_installation_authorization(self) -> InstallationAuthorization: |
313 | | - assert ( |
314 | | - self.__integration is not None |
315 | | - ), "Method withRequester(Requester) must be called first" |
| 298 | + assert self.__integration is not None, "Method withRequester(Requester) must be called first" |
316 | 299 | return self.__integration.get_access_token( |
317 | 300 | self._installation_id, |
318 | 301 | permissions=self._token_permissions, |
@@ -413,22 +396,13 @@ def withRequester(self, requester: Requester) -> "AppUserAuth": |
413 | 396 |
|
414 | 397 | @property |
415 | 398 | def _is_expired(self) -> bool: |
416 | | - return self._expires_at is not None and self._expires_at < datetime.now( |
417 | | - timezone.utc |
418 | | - ) |
| 399 | + return self._expires_at is not None and self._expires_at < datetime.now(timezone.utc) |
419 | 400 |
|
420 | 401 | def _refresh(self): |
421 | 402 | if self._refresh_token is None: |
422 | | - raise RuntimeError( |
423 | | - "Cannot refresh expired token because no refresh token has been provided" |
424 | | - ) |
425 | | - if ( |
426 | | - self._refresh_expires_at is not None |
427 | | - and self._refresh_expires_at < datetime.now(timezone.utc) |
428 | | - ): |
429 | | - raise RuntimeError( |
430 | | - "Cannot refresh expired token because refresh token also expired" |
431 | | - ) |
| 403 | + raise RuntimeError("Cannot refresh expired token because no refresh token has been provided") |
| 404 | + if self._refresh_expires_at is not None and self._refresh_expires_at < datetime.now(timezone.utc): |
| 405 | + raise RuntimeError("Cannot refresh expired token because refresh token also expired") |
432 | 406 |
|
433 | 407 | # refresh token |
434 | 408 | token = self.__app.refresh_access_token(self._refresh_token) |
|
0 commit comments