-
Notifications
You must be signed in to change notification settings - Fork 3
[ZL-874] Manually refresh and revoke tokens #39
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5aa7db2
Allow client to manually refresh its token
njbbaer 18e169a
Test manual token refresh
njbbaer 5422b72
Update version, changelog, and readme
njbbaer 008b527
Revert version change and mark changelog entry unreleased
njbbaer 4de40fa
Allow client to manually revoke its token
njbbaer a1e6c32
Address code review comments
njbbaer 2389880
Fix doc comments
njbbaer 723879d
Respond with correct error type for bad api version
njbbaer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,48 +38,72 @@ def initialize(client_id:, client_secret:, store:, options: {}) | |
| @store = store | ||
| end | ||
|
|
||
| # @raise [OAuthError] if a token cannot be refreshed. | ||
| def refresh | ||
| token = fetch_token | ||
|
|
||
| begin | ||
| new_token = @credentials.refresh( | ||
| token: token.access_token, | ||
| refresh: token.refresh_token, | ||
| ) | ||
|
|
||
| Util.log_info("Token Refresh Successful", store: store) | ||
| store.save(new_token) | ||
| rescue RuntimeError | ||
| Util.log_error("Token Refresh Failed", store: store) | ||
| raise Procore::OAuthError.new( | ||
| "Unable to refresh the access token. Perhaps the Procore API is " \ | ||
| "down or your access token store is misconfigured. Either " \ | ||
| "way, you should clear the store and prompt the user to sign in " \ | ||
| "again.", | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| # @raise [OAuthError] if a token cannot be revoked. | ||
| def revoke | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you want to add a comment above # @raise [OAuthError] if a token cannot be revoked. |
||
| token = fetch_token | ||
|
|
||
| begin | ||
| @credentials.revoke(token: token) | ||
| Util.log_info("Token Revocation Successful", store: store) | ||
| rescue RuntimeError | ||
| Util.log_error("Token Revocation Failed", store: store) | ||
| raise Procore::OAuthError.new( | ||
| "Unable to revoke the access token. Perhaps the Procore API is " \ | ||
| "down or your access token store is misconfigured. Either " \ | ||
| "way, you should clear the store and prompt the user to sign in " \ | ||
| "again.", | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def base_api_path | ||
| "#{options[:host]}" | ||
| end | ||
|
|
||
| # @raise [OAuthError] if the store does not have a token stored in it prior | ||
| # to making a request. | ||
| # @raise [OAuthError] if a token cannot be refreshed. | ||
| # @raise [OAuthError] if incorrect credentials have been supplied. | ||
| def access_token | ||
| # @raise [OAuthError] if the store does not have a token stored. | ||
| def fetch_token | ||
| token = store.fetch | ||
|
|
||
| if token.nil? || token.invalid? | ||
| raise Procore::MissingTokenError.new( | ||
| "Unable to retreive an access token from the store. Double check " \ | ||
| "your store configuration and make sure to correctly store a token " \ | ||
| "before attempting to make API requests", | ||
| ) | ||
| end | ||
| token | ||
| end | ||
|
|
||
| def access_token | ||
| token = fetch_token | ||
| if token.expired? | ||
| Util.log_info("Token Expired", store: store) | ||
| begin | ||
| token = @credentials.refresh( | ||
| token: token.access_token, | ||
| refresh: token.refresh_token, | ||
| ) | ||
|
|
||
| Util.log_info("Token Refresh Successful", store: store) | ||
| store.save(token) | ||
| rescue RuntimeError | ||
| Util.log_error("Token Refresh Failed", store: store) | ||
| raise Procore::OAuthError.new( | ||
| "Unable to refresh the access token. Perhaps the Procore API is " \ | ||
| "down or the your access token store is misconfigured. Either " \ | ||
| "way, you should clear the store and prompt the user to sign in " \ | ||
| "again.", | ||
| ) | ||
| end | ||
| refresh | ||
| end | ||
|
|
||
| token.access_token | ||
| end | ||
| end | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.