-
Notifications
You must be signed in to change notification settings - Fork 7
Adding access token refresh mechanism #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexbourret
wants to merge
10
commits into
master
Choose a base branch
from
feature/access-token-refresh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0d51df5
Adding access token refresh mechanism
alexbourret 5b10f73
use updatable access token for SSO with DSS 14.5+
alexbourret 2e0adb5
Merge branch 'master' into feature/access-token-refresh
alexbourret 21eb731
v1.4.0
alexbourret acbc12a
update changelog
alexbourret dad8d8a
renaming
alexbourret 092cdbc
renaming + bug fix
alexbourret fc65a2c
unit test for jwt decoding
alexbourret 780e2d4
two parameters for FreshToken
alexbourret 7ab7fd1
small refacto
alexbourret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from safe_logger import SafeLogger | ||
| from dss_constants import DSSConstants | ||
| import time | ||
|
|
||
| logger = SafeLogger("sharepoint-online plugin FreshToken", DSSConstants.SECRET_PARAMETERS_KEYS) | ||
| TOKEN_VALIDITY_SAFETY_MARGIN_SECONDS = 60 | ||
|
|
||
|
|
||
| class FreshToken(): | ||
| def __init__(self, token_refresh_method=None, access_token=None): | ||
| logger.info("FreshToken init") | ||
| if access_token: | ||
| logger.info("Permanent access token provided") | ||
| self.current_token = access_token | ||
| self.token_refresh_method = self._default_refresh_method | ||
| self.token_renewal_time = None | ||
| if token_refresh_method is not None: | ||
| logger.info("Using refresh method") | ||
| self.token_refresh_method = token_refresh_method | ||
| self.refresh_token() | ||
|
|
||
| def _default_refresh_method(self): | ||
| return self.current_token | ||
|
|
||
| def token_needs_renewal(self): | ||
| if self.token_renewal_time is None: | ||
| return True | ||
| epoch_time_now = int(time.time()) | ||
| return self.token_renewal_time <= epoch_time_now | ||
|
|
||
| def refresh_token(self): | ||
| self.current_token = self.token_refresh_method() | ||
| decoded_jwt = decode_jwt(self.current_token) | ||
| self.token_renewal_time = decoded_jwt.get("exp", None) | ||
| if isinstance(self.token_renewal_time, int): | ||
| self.token_renewal_time = self.token_renewal_time - TOKEN_VALIDITY_SAFETY_MARGIN_SECONDS | ||
| logger.info("The token is valid until {}".format(self.token_renewal_time)) | ||
|
|
||
| @property | ||
| def access_token(self): | ||
| if not self.token_needs_renewal(): | ||
| logger.info("Token reaching its time limit, refreshing it...") | ||
| self.refresh_token() | ||
| return self.current_token | ||
|
|
||
|
|
||
| def decode_jwt(jwt_token): | ||
| try: | ||
| import base64 | ||
| import json | ||
| sub_tokens = jwt_token.split('.') | ||
| if len(sub_tokens) < 2: | ||
| logger.error("JWT format is wrong") | ||
| return {} | ||
| token_payload = sub_tokens[1] | ||
| padded_token = token_payload + "=" * (-len(token_payload) % 4) | ||
| decoded_token = base64.urlsafe_b64decode(padded_token.encode('utf-8')) | ||
| json_token = json.loads(decoded_token) | ||
| return json_token | ||
| except Exception as error: | ||
| logger.error("Could not decode JWT token ({})".format(error)) | ||
| return {} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✍️ Could you put a comment here to explain what these two cases are here and when each one will happen? Also maybe mention where
__credentialsis populated and what kind of structure should be in there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the convention that was chosen by Platform. I'll pm you the doc's PR on the subject...