-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: session refresh loop if expired token is passed in headers #73
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
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 |
---|---|---|
|
@@ -34,7 +34,10 @@ public class SuperTokensInterceptor implements Interceptor { | |
private static final Object refreshTokenLock = new Object(); | ||
private static final ReentrantReadWriteLock refreshAPILock = new ReentrantReadWriteLock(); | ||
|
||
private Request removeAuthHeaderIfMatchesLocalToken(Request request, Request.Builder builder, Context context) { | ||
|
||
// Returns true authorization header in the provided request matches the current local access token. | ||
// This is used to decide whether the authorization header should be removed before making the request. | ||
private boolean shouldRemoveAuthHeader(Request request, Context context) { | ||
String originalHeader = request.header("Authorization"); | ||
|
||
if (originalHeader == null) { | ||
|
@@ -46,18 +49,20 @@ private Request removeAuthHeaderIfMatchesLocalToken(Request request, Request.Bui | |
String refreshToken = Utils.getTokenForHeaderAuth(Utils.TokenType.REFRESH, context); | ||
|
||
if (accessToken != null && refreshToken != null && originalHeader.equals("Bearer " + accessToken)) { | ||
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. shuold use ignore case? 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. done |
||
builder.removeHeader("Authorization"); | ||
builder.removeHeader("authorization"); | ||
return true; | ||
} | ||
} | ||
|
||
return builder.build(); | ||
return false; | ||
} | ||
|
||
private static Request setAuthorizationHeaderIfRequired(Request.Builder builder, Context context, boolean addRefreshToken) { | ||
Map<String, String> headersToSet = Utils.getAuthorizationHeaderIfRequired(addRefreshToken, context); | ||
for (Map.Entry<String, String> entry: headersToSet.entrySet()) { | ||
builder.header(entry.getKey(), entry.getValue()); | ||
|
||
private static Request setAuthorizationHeaderIfRequired(Request request, Request.Builder builder, Context context, boolean addRefreshToken) { | ||
String authHeader = Utils.getAuthorizationHeaderIfExists(addRefreshToken, context); | ||
boolean hasExistingAuthHeader = request.header("Authorization") != null || request.header("authorization") != null; | ||
|
||
if (authHeader != null && !hasExistingAuthHeader) { | ||
builder.header("Authorization", authHeader); | ||
} | ||
|
||
return builder.build(); | ||
|
@@ -98,6 +103,7 @@ public Response intercept(@NotNull Chain chain) throws IOException { | |
} | ||
|
||
try { | ||
boolean wasAuthHeaderRemovedInitially = false; | ||
int sessionRefreshAttempts = 0; | ||
while (true) { | ||
Request.Builder requestBuilder = chain.request().newBuilder(); | ||
|
@@ -120,8 +126,19 @@ public Response intercept(@NotNull Chain chain) throws IOException { | |
request = request.newBuilder().header("rid", "anti-csrf").build(); | ||
} | ||
|
||
request = removeAuthHeaderIfMatchesLocalToken(request, request.newBuilder(), applicationContext); | ||
request = setAuthorizationHeaderIfRequired(request.newBuilder(), applicationContext, false); | ||
// Check if the Authorization header should be removed | ||
// This is necessary to ensure that if the auth header was removed initially, | ||
// it remains removed in subsequent retries even if the token has changed. | ||
if (wasAuthHeaderRemovedInitially || shouldRemoveAuthHeader(request, applicationContext)) { | ||
Request.Builder builder = request.newBuilder(); | ||
builder.removeHeader("Authorization"); | ||
builder.removeHeader("authorization"); | ||
request = builder.build(); | ||
|
||
wasAuthHeaderRemovedInitially = true; | ||
} | ||
|
||
request = setAuthorizationHeaderIfRequired(request, request.newBuilder(), applicationContext, false); | ||
|
||
response = makeRequest(chain, request); | ||
Utils.saveTokenFromHeaders(response, applicationContext); | ||
|
@@ -207,6 +224,8 @@ private static Utils.Unauthorised onUnauthorisedResponse(Utils.LocalSessionState | |
Request.Builder refreshRequestBuilder = new Request.Builder(); | ||
refreshRequestBuilder.url(SuperTokens.refreshTokenUrl); | ||
refreshRequestBuilder.method("POST", new FormBody.Builder().build()); | ||
|
||
Request refreshRequest = refreshRequestBuilder.build(); | ||
|
||
if (preRequestLocalSessionState.status == Utils.LocalSessionStateStatus.EXISTS) { | ||
String antiCSRFToken = AntiCSRF.getToken(applicationContext, preRequestLocalSessionState.lastAccessTokenUpdate); | ||
|
@@ -220,7 +239,7 @@ private static Utils.Unauthorised onUnauthorisedResponse(Utils.LocalSessionState | |
refreshRequestBuilder.header("fdi-version", Utils.join(Version.supported_fdi, ",")); | ||
refreshRequestBuilder.header("st-auth-mode", SuperTokens.config.tokenTransferMethod); | ||
|
||
refreshRequestBuilder = setAuthorizationHeaderIfRequired(refreshRequestBuilder, applicationContext, true).newBuilder(); | ||
refreshRequestBuilder = setAuthorizationHeaderIfRequired(refreshRequest, refreshRequestBuilder, applicationContext, true).newBuilder(); | ||
|
||
Map<String, String> customRefreshHeaders = SuperTokens.config.customHeaderMapper.getRequestHeaders(CustomHeaderProvider.RequestType.REFRESH); | ||
if (customRefreshHeaders != null) { | ||
|
@@ -229,7 +248,7 @@ private static Utils.Unauthorised onUnauthorisedResponse(Utils.LocalSessionState | |
} | ||
} | ||
|
||
Request refreshRequest = refreshRequestBuilder.build(); | ||
refreshRequest = refreshRequestBuilder.build(); | ||
refreshResponse = makeRequest(chain, refreshRequest); | ||
|
||
Utils.saveTokenFromHeaders(refreshResponse, applicationContext); | ||
|
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
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.
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.
we should check with ignore case?
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.
done