-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
30 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,7 @@ def __init__(self, token_manager: TokenManager): | |
self.logger = logging.getLogger(__name__) | ||
|
||
def get_auth_string(self) -> str: | ||
if self.token is None: | ||
self.fetch_token() | ||
self.fetch_token() | ||
return f"Bearer {self.token}" | ||
|
||
def requires_authentication(self) -> bool: | ||
|
@@ -28,15 +27,23 @@ def requires_authentication(self) -> bool: | |
def fetch_token(self): | ||
self.logger.info("New token fetched for accessing organization API") | ||
if self.token is None or self.token == "" or self.is_token_expired(self.token): | ||
with self.lock: | ||
with self.lock: | ||
if self.token is None or self.token == "" or self.is_token_expired(self.token): | ||
self.token = self.token_manager.fetch_access_token() | ||
|
||
def is_token_expired(self, token): | ||
print(f'token is {token}') | ||
decoded_jwt = jwt.decode(token, options={"verify_signature": True}, algorithms=["RS256"]) | ||
expires_at = decoded_jwt.get("exp") | ||
# Add a buffer of 30 seconds | ||
buffer_seconds = 30 | ||
buffer_expires_at = expires_at - buffer_seconds | ||
return buffer_expires_at < datetime.datetime.now().timestamp() | ||
try: | ||
decoded = jwt.decode(token, options={"verify_signature": False}) | ||
exp = decoded.get('exp') | ||
|
||
if exp is None: | ||
return True # No expiration time present, consider it expired | ||
Check failure Code scanning / SonarCloud JWT should be signed and verified
<!--SONAR_ISSUE_KEY:AZJhAlA8yje6SmAfcsn--->Don't use a JWT token without verifying its signature. <p>See more on <a href="https://sonarcloud.io/project/issues?id=twilio_twilio-python&issues=AZJhAlA8yje6SmAfcsn-&open=AZJhAlA8yje6SmAfcsn-&branch=asabu_Python_changes">SonarCloud</a></p>
Check failure Code scanning / SonarCloud JWT should be signed and verified High
Don't use a JWT token without verifying its signature. See more on SonarCloud
|
||
|
||
# Check if the expiration time has passed | ||
return datetime.fromtimestamp(exp) < datetime.utcnow() | ||
|
||
except jwt.DecodeError: | ||
return True # Token is invalid | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters