-
Notifications
You must be signed in to change notification settings - Fork 5
Add configurable token providers #346
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
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
Some comments aren't visible on the classic Files Changed page.
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/incognia/api/clients/AutoRefreshTokenProvider.java
|
Contributor
Author
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. Basically the previous TokenProvider with a new name |
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,46 @@ | ||
| package com.incognia.api.clients; | ||
|
|
||
| import com.incognia.common.Token; | ||
| import com.incognia.common.exceptions.IncogniaException; | ||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| public class AutoRefreshTokenProvider implements TokenProvider { | ||
| private static final int TOKEN_REFRESH_BEFORE_SECONDS = 10; | ||
|
|
||
| private final ReentrantLock lock = new ReentrantLock(); | ||
| private final TokenRequester tokenRequester; | ||
| private volatile Token token; | ||
|
|
||
| public AutoRefreshTokenProvider( | ||
| String clientId, String clientSecret, NetworkingClient networkingClient) { | ||
| this.tokenRequester = new TokenRequester(clientId, clientSecret, networkingClient); | ||
| } | ||
|
|
||
| @Override | ||
| public Token getToken() throws IncogniaException { | ||
| refreshTokenIfNeeded(); | ||
| return token; | ||
| } | ||
|
|
||
| private boolean needsRefresh(Token token) { | ||
| return token == null | ||
| || Instant.now().until(token.getExpiresAt(), ChronoUnit.SECONDS) | ||
| <= TOKEN_REFRESH_BEFORE_SECONDS; | ||
| } | ||
|
|
||
| private void refreshTokenIfNeeded() throws IncogniaException { | ||
| Token currentToken = token; | ||
| if (needsRefresh(currentToken)) { | ||
| lock.lock(); | ||
| try { | ||
| if (needsRefresh(token)) { | ||
| token = tokenRequester.requestToken(); | ||
| } | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/incognia/api/clients/ManualRefreshTokenProvider.java
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,72 @@ | ||
| package com.incognia.api.clients; | ||
|
|
||
| import com.incognia.common.Token; | ||
| import com.incognia.common.exceptions.IncogniaException; | ||
| import com.incognia.common.exceptions.TokenExpiredException; | ||
| import com.incognia.common.exceptions.TokenNotFoundException; | ||
| import com.incognia.common.utils.Asserts; | ||
| import com.incognia.common.utils.CustomOptions; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import okhttp3.ConnectionPool; | ||
| import okhttp3.OkHttpClient; | ||
|
|
||
| public class ManualRefreshTokenProvider implements TokenProvider { | ||
| private static final String API_URL = "https://api.incognia.com"; | ||
|
|
||
| private final ReentrantLock lock = new ReentrantLock(); | ||
| private final TokenRequester tokenRequester; | ||
| private volatile Token token; | ||
|
|
||
| public ManualRefreshTokenProvider(String clientId, String clientSecret) { | ||
| this(clientId, clientSecret, CustomOptions.builder().build()); | ||
| } | ||
|
|
||
| public ManualRefreshTokenProvider(String clientId, String clientSecret, CustomOptions options) { | ||
| this(clientId, clientSecret, createNetworkingClient(options)); | ||
| } | ||
|
|
||
| ManualRefreshTokenProvider( | ||
| String clientId, String clientSecret, NetworkingClient networkingClient) { | ||
| Asserts.assertNotEmpty(clientId, "client id"); | ||
| Asserts.assertNotEmpty(clientSecret, "client secret"); | ||
| Asserts.assertNotNull(networkingClient, "networking client"); | ||
| this.tokenRequester = new TokenRequester(clientId, clientSecret, networkingClient); | ||
| } | ||
|
figueredo marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public Token getToken() throws IncogniaException { | ||
| Token currentToken = token; | ||
| if (currentToken == null) { | ||
| throw new TokenNotFoundException(); | ||
| } | ||
|
|
||
| if (currentToken.isExpired()) { | ||
| throw new TokenExpiredException(); | ||
| } | ||
|
|
||
| return currentToken; | ||
| } | ||
|
|
||
| public Token refresh() throws IncogniaException { | ||
| lock.lock(); | ||
| try { | ||
| token = tokenRequester.requestToken(); | ||
| return token; | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| private static NetworkingClient createNetworkingClient(CustomOptions options) { | ||
| Asserts.assertNotNull(options, "custom options"); | ||
| OkHttpClient httpClient = | ||
| new OkHttpClient.Builder() | ||
| .callTimeout(options.getTimeoutMillis(), TimeUnit.MILLISECONDS) | ||
| .connectionPool( | ||
| new ConnectionPool( | ||
| options.getMaxConnections(), options.getKeepAliveSeconds(), TimeUnit.SECONDS)) | ||
| .build(); | ||
| return new NetworkingClient(httpClient, API_URL); | ||
|
figueredo marked this conversation as resolved.
|
||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.