Skip to content

Commit

Permalink
Prepares integrity manager on first sync call
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Dec 23, 2024
1 parent 5c4c9af commit f49fde3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions financial-connections/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api project(":stripe-core")
api project(":stripe-ui-core")
api project(":payments-model")
implementation project(":stripe-attestation")

implementation libs.androidx.activity
implementation libs.androidx.annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffe
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenAuthFlowWithUrl
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenNativeAuthFlow
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewModel.Companion.QUERY_PARAM_PAYMENT_METHOD
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsTracker
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.ErrorCode
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Metadata
Expand Down Expand Up @@ -58,6 +59,7 @@ import com.stripe.android.financialconnections.navigation.topappbar.TopAppBarSta
import com.stripe.android.financialconnections.presentation.FinancialConnectionsViewModel
import com.stripe.android.financialconnections.ui.FinancialConnectionsSheetNativeActivity
import com.stripe.android.financialconnections.utils.parcelable
import com.stripe.attestation.IntegrityRequestManager
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -68,6 +70,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
@Named(APPLICATION_ID) private val applicationId: String,
savedStateHandle: SavedStateHandle,
private val getOrFetchSync: GetOrFetchSync,
private val integrityRequestManager: IntegrityRequestManager,
private val fetchFinancialConnectionsSession: FetchFinancialConnectionsSession,
private val fetchFinancialConnectionsSessionForToken: FetchFinancialConnectionsSessionForToken,
private val logger: Logger,
Expand Down Expand Up @@ -111,6 +114,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
private fun fetchManifest() {
viewModelScope.launch {
kotlin.runCatching {
prepareStandardRequestManager()
getOrFetchSync(refetchCondition = Always)
}.onFailure {
finishWithResult(stateFlow.value, Failed(it))
Expand All @@ -120,6 +124,20 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
}
}

private suspend fun prepareStandardRequestManager(): Boolean {
val result = runCatching { integrityRequestManager.prepare() }
result.exceptionOrNull()?.let {
analyticsTracker.track(
FinancialConnectionsAnalyticsEvent.Error(
extraMessage = "Failed to warm up the IntegrityStandardRequestManager",
pane = Pane.CONSENT,
exception = it
)
)
}
return result.isSuccess
}

/**
* Builds the ChromeCustomTab intent to launch the hosted auth flow and launches it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.stripe.android.financialconnections.utils.Experiment.CONNECTIONS_CONS
import com.stripe.android.financialconnections.utils.error
import com.stripe.android.financialconnections.utils.experimentAssignment
import com.stripe.android.financialconnections.utils.trackExposure
import com.stripe.attestation.IntegrityRequestManager
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand All @@ -44,6 +45,7 @@ internal class ConsentViewModel @AssistedInject constructor(
@Assisted initialState: ConsentState,
nativeAuthFlowCoordinator: NativeAuthFlowCoordinator,
private val acceptConsent: AcceptConsent,
private val integrityRequestManager: IntegrityRequestManager,
private val getOrFetchSync: GetOrFetchSync,
private val navigationManager: NavigationManager,
private val eventTracker: FinancialConnectionsAnalyticsTracker,
Expand All @@ -53,6 +55,11 @@ internal class ConsentViewModel @AssistedInject constructor(
) : FinancialConnectionsViewModel<ConsentState>(initialState, nativeAuthFlowCoordinator) {

init {
viewModelScope.launch {
integrityRequestManager.requestToken()
.onSuccess { logger.debug("Integrity token: $it") }
.onFailure { logger.error("Failed to get integrity token", it) }
}
logErrors()
suspend {
val sync = getOrFetchSync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class IntegrityStandardRequestManager(
private var integrityTokenProvider: StandardIntegrityTokenProvider? = null

override suspend fun prepare(): Result<Unit> = runCatching {
if (integrityTokenProvider != null) {
return Result.success(Unit)
}

val finishedTask: Task<StandardIntegrityTokenProvider> = standardIntegrityManager
.prepareIntegrityToken(
PrepareIntegrityTokenRequest.builder()
Expand Down

0 comments on commit f49fde3

Please sign in to comment.