-
Notifications
You must be signed in to change notification settings - Fork 659
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
[FC] Handle process kills after returning from browsers in AuthSessions #6853
Changes from 6 commits
6df95df
e0985db
e238a25
ccfd628
99fd905
43ff797
cce5162
c9b74e9
3c87131
005f09f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,16 @@ import com.airbnb.mvrx.Async | |
import com.airbnb.mvrx.Fail | ||
import com.airbnb.mvrx.Loading | ||
import com.airbnb.mvrx.MavericksState | ||
import com.airbnb.mvrx.PersistState | ||
import com.airbnb.mvrx.Success | ||
import com.airbnb.mvrx.Uninitialized | ||
import com.stripe.android.financialconnections.model.DataAccessNotice | ||
import com.stripe.android.financialconnections.model.FinancialConnectionsAuthorizationSession | ||
import com.stripe.android.financialconnections.model.FinancialConnectionsInstitution | ||
|
||
internal data class PartnerAuthState( | ||
@PersistState | ||
val firstInit: Boolean = true, | ||
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. Instead of persisting 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. I like that better actually! Even if we can't "fetch the auth session by id" I find it more readable as well - c9b74e9 |
||
val payload: Async<Payload> = Uninitialized, | ||
val viewEffect: ViewEffect? = null, | ||
val authenticationStatus: Async<String> = Uninitialized | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
package com.stripe.android.financialconnections.features.partnerauth | ||
|
||
import android.webkit.URLUtil | ||
import com.airbnb.mvrx.Async | ||
import com.airbnb.mvrx.Fail | ||
import com.airbnb.mvrx.Loading | ||
import com.airbnb.mvrx.MavericksViewModel | ||
import com.airbnb.mvrx.MavericksViewModelFactory | ||
import com.airbnb.mvrx.Success | ||
import com.airbnb.mvrx.Uninitialized | ||
import com.airbnb.mvrx.ViewModelContext | ||
import com.stripe.android.core.Logger | ||
|
@@ -24,7 +22,7 @@ import com.stripe.android.financialconnections.domain.GoNext | |
import com.stripe.android.financialconnections.domain.PollAuthorizationSessionOAuthResults | ||
import com.stripe.android.financialconnections.domain.PostAuthSessionEvent | ||
import com.stripe.android.financialconnections.domain.PostAuthorizationSession | ||
import com.stripe.android.financialconnections.exception.WebAuthFlowCancelledException | ||
import com.stripe.android.financialconnections.exception.WebAuthFlowFailedException | ||
import com.stripe.android.financialconnections.features.partnerauth.PartnerAuthState.Payload | ||
import com.stripe.android.financialconnections.features.partnerauth.PartnerAuthState.ViewEffect.OpenBottomSheet | ||
import com.stripe.android.financialconnections.features.partnerauth.PartnerAuthState.ViewEffect.OpenPartnerAuth | ||
|
@@ -33,12 +31,14 @@ import com.stripe.android.financialconnections.model.FinancialConnectionsSession | |
import com.stripe.android.financialconnections.model.FinancialConnectionsSessionManifest.Pane | ||
import com.stripe.android.financialconnections.navigation.NavigationDirections | ||
import com.stripe.android.financialconnections.navigation.NavigationManager | ||
import com.stripe.android.financialconnections.presentation.WebAuthFlowState | ||
import com.stripe.android.financialconnections.ui.FinancialConnectionsSheetNativeActivity | ||
import com.stripe.android.financialconnections.utils.UriUtils | ||
import kotlinx.coroutines.launch | ||
import java.util.Date | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
@Suppress("LongParameterList") | ||
internal class PartnerAuthViewModel @Inject constructor( | ||
private val completeAuthorizationSession: CompleteAuthorizationSession, | ||
|
@@ -58,8 +58,34 @@ internal class PartnerAuthViewModel @Inject constructor( | |
|
||
init { | ||
logErrors() | ||
observePayload() | ||
createAuthSession() | ||
withState { | ||
if (it.firstInit) { | ||
setState { copy(firstInit = false) } | ||
launchBrowserIfNonOauth() | ||
createAuthSession() | ||
} else { | ||
restoreAuthSession() | ||
} | ||
} | ||
} | ||
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. We don't want to create a new session if coming from a process kill. We'll restore the session from the manifest instead. |
||
|
||
private fun restoreAuthSession() { | ||
suspend { | ||
// if coming from a process kill, there should be a session | ||
// re-fetch the manifest and use its active auth session instead of creating a new one | ||
val manifest: FinancialConnectionsSessionManifest = getManifest() | ||
val authSession = manifest.activeAuthSession?.also { | ||
logger.debug("Restoring auth session ${it.id}") | ||
} ?: createAuthorizationSession( | ||
institution = requireNotNull(manifest.activeInstitution), | ||
allowManualEntry = manifest.allowManualEntry | ||
) | ||
Payload( | ||
authSession = authSession, | ||
institution = requireNotNull(manifest.activeInstitution), | ||
isStripeDirect = manifest.isStripeDirect ?: false | ||
) | ||
}.execute { copy(payload = it) } | ||
} | ||
|
||
private fun createAuthSession() { | ||
|
@@ -70,6 +96,7 @@ internal class PartnerAuthViewModel @Inject constructor( | |
institution = requireNotNull(manifest.activeInstitution), | ||
allowManualEntry = manifest.allowManualEntry | ||
) | ||
logger.debug("Created auth session ${authSession.id}") | ||
Payload( | ||
authSession = authSession, | ||
institution = requireNotNull(manifest.activeInstitution), | ||
|
@@ -85,7 +112,7 @@ internal class PartnerAuthViewModel @Inject constructor( | |
}.execute { copy(payload = it) } | ||
} | ||
|
||
private fun observePayload() { | ||
private fun launchBrowserIfNonOauth() { | ||
onAsync( | ||
asyncProp = PartnerAuthState::payload, | ||
onSuccess = { | ||
|
@@ -134,27 +161,25 @@ internal class PartnerAuthViewModel @Inject constructor( | |
} | ||
|
||
fun onWebAuthFlowFinished( | ||
webStatus: Async<String> | ||
webStatus: WebAuthFlowState | ||
) { | ||
logger.debug("Web AuthFlow status received $webStatus") | ||
viewModelScope.launch { | ||
when (webStatus) { | ||
is Uninitialized -> {} | ||
is Loading -> setState { copy(authenticationStatus = Loading()) } | ||
is Success -> completeAuthorizationSession() | ||
is Fail -> { | ||
when (val error = webStatus.error) { | ||
is WebAuthFlowCancelledException -> onAuthCancelled() | ||
else -> onAuthFailed(error) | ||
} | ||
} | ||
WebAuthFlowState.Canceled -> onAuthCancelled() | ||
is WebAuthFlowState.Failed -> onAuthFailed(webStatus.message, webStatus.reason) | ||
WebAuthFlowState.InProgress -> setState { copy(authenticationStatus = Loading()) } | ||
is WebAuthFlowState.Success -> completeAuthorizationSession() | ||
WebAuthFlowState.Uninitialized -> {} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun onAuthFailed( | ||
error: Throwable | ||
message: String, | ||
reason: String? | ||
) { | ||
val error = WebAuthFlowFailedException(message, reason) | ||
kotlin.runCatching { | ||
logger.debug("Auth failed, cancelling AuthSession") | ||
val authSession = getManifest().activeAuthSession | ||
|
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.
Small change on the Playground in the example app to make it work across process kills -> we override a deeplink based on a shared preference for local testing. We were clearing them on Activity's onDestroy to prevent it from leaking to the official examples just in case.
Process kills kill the activity triggering the preference clear. Moved the clear to the launcher activity instead.