-
Notifications
You must be signed in to change notification settings - Fork 0
Handle expired signature GraphQL errors with ApolloAuthInterceptor #115
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
AndrewCheung360
wants to merge
3
commits into
main
Choose a base branch
from
fix/signature-expired-retry
base: main
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e0b9803
Handle expired signature GraphQL errors with ApolloAuthInterceptor
AndrewCheung360 1588a95
Address PR feedback: add Mutex for thread-safety and improve logging
AndrewCheung360 db8e2fc
Fix ApolloAuthInterceptor to correctly inject token and handle retries
AndrewCheung360 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
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/cornellappdev/uplift/data/auth/ApolloAuthInterceptor.kt
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,76 @@ | ||
| package com.cornellappdev.uplift.data.auth | ||
|
|
||
| import com.apollographql.apollo.ApolloClient | ||
| import com.apollographql.apollo.api.ApolloRequest | ||
| import com.apollographql.apollo.api.ApolloResponse | ||
| import com.apollographql.apollo.api.ExecutionContext | ||
| import com.apollographql.apollo.api.Operation | ||
| import com.apollographql.apollo.interceptor.ApolloInterceptor | ||
| import com.apollographql.apollo.interceptor.ApolloInterceptorChain | ||
| import com.cornellappdev.uplift.RefreshAccessTokenMutation | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.emitAll | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.flow | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
| import javax.inject.Singleton | ||
|
|
||
| /** | ||
| * Execution context to track retries. | ||
| */ | ||
| internal class RetryContext(val retryCount: Int) : ExecutionContext.Element { | ||
| override val key: ExecutionContext.Key<*> = Key | ||
|
|
||
| companion object Key : ExecutionContext.Key<RetryContext> | ||
| } | ||
|
|
||
| /** | ||
| * An Apollo Interceptor that handles token expiration errors that return as 200 OK with | ||
| * GraphQL errors (specifically "Signature has expired"). | ||
| */ | ||
| @Singleton | ||
| class ApolloAuthInterceptor @Inject constructor( | ||
| private val tokenManager: TokenManager, | ||
| private val sessionManager: SessionManager, | ||
| @Named("refresh") private val refreshClient: ApolloClient | ||
| ) : ApolloInterceptor { | ||
| override fun <D : Operation.Data> intercept( | ||
| request: ApolloRequest<D>, | ||
| chain: ApolloInterceptorChain | ||
| ): Flow<ApolloResponse<D>> = flow { | ||
| val response = chain.proceed(request).first() | ||
|
|
||
| val retryCount = request.executionContext[RetryContext]?.retryCount ?: 0 | ||
| // TODO: replace string check with explicit error codes if backend implements | ||
| if (response.errors?.any { it.message.contains("Signature has expired") } == true && retryCount < 1) { | ||
| val refreshToken = tokenManager.getRefreshToken() | ||
| if (refreshToken != null) { | ||
| try { | ||
| val mutationResponse = refreshClient.mutation(RefreshAccessTokenMutation()) | ||
| .addHttpHeader("Authorization", "Bearer $refreshToken") | ||
| .execute() | ||
|
|
||
| val newAccessToken = mutationResponse.data?.refreshAccessToken?.newAccessToken | ||
| if (newAccessToken != null) { | ||
| tokenManager.saveTokens(newAccessToken, refreshToken) | ||
| // Retry the request with the new token | ||
| val newRequest = request.newBuilder() | ||
| .addExecutionContext(RetryContext(retryCount + 1)) | ||
| .build() | ||
| emitAll(chain.proceed(newRequest)) | ||
| return@flow | ||
| } else { | ||
| sessionManager.logout() | ||
| } | ||
| } catch (e: Exception) { | ||
| sessionManager.logout() | ||
| } | ||
| } else { | ||
| sessionManager.logout() | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| emit(response) | ||
| } | ||
| } | ||
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.
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.