Skip to content

Commit

Permalink
Attestation example.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Aug 21, 2024
1 parent ac2d867 commit a1febb9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.coroutines.resume
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class IntegrityStandardRequestManager(
// TODO figure out GCP.
private val cloudProjectNumber: Long = 123456789L,
private val cloudProjectNumber: Long = 161414635700,
private val buildRequestIdentifier: BuildRequestIdentifier = BuildRequestIdentifier(),
appContext: Context
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ internal interface FinancialConnectionsSheetSharedModule {
context: Application
): IntegrityStandardRequestManager {
return IntegrityStandardRequestManager(
cloudProjectNumber = 123456789L,
buildRequestIdentifier = BuildRequestIdentifier(),
appContext = context
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.stripe.android.core.Logger
import com.stripe.android.financialconnections.FinancialConnections
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.ConsentAgree
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.PaneLoaded
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsTracker
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Name
import com.stripe.android.financialconnections.analytics.logError
import com.stripe.android.financialconnections.di.FinancialConnectionsSheetNativeComponent
import com.stripe.android.financialconnections.domain.AcceptConsent
Expand All @@ -26,14 +23,14 @@ import com.stripe.android.financialconnections.navigation.Destination.Companion.
import com.stripe.android.financialconnections.navigation.Destination.ManualEntry
import com.stripe.android.financialconnections.navigation.Destination.NetworkingLinkLoginWarmup
import com.stripe.android.financialconnections.navigation.NavigationManager
import com.stripe.android.financialconnections.navigation.destination
import com.stripe.android.financialconnections.navigation.topappbar.TopAppBarStateUpdate
import com.stripe.android.financialconnections.presentation.FinancialConnectionsViewModel
import com.stripe.android.financialconnections.ui.HandleClickableUrl
import com.stripe.android.financialconnections.utils.Experiment.CONNECTIONS_CONSENT_COMBINED_LOGO
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.IntegrityStandardRequestManager
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand All @@ -48,6 +45,7 @@ internal class ConsentViewModel @AssistedInject constructor(
private val navigationManager: NavigationManager,
private val eventTracker: FinancialConnectionsAnalyticsTracker,
private val handleClickableUrl: HandleClickableUrl,
private val standardRequestManager: IntegrityStandardRequestManager,
private val logger: Logger,
private val presentSheet: PresentSheet,
) : FinancialConnectionsViewModel<ConsentState>(initialState, nativeAuthFlowCoordinator) {
Expand Down Expand Up @@ -95,13 +93,24 @@ internal class ConsentViewModel @AssistedInject constructor(
}

fun onContinueClick() {
suspend {
eventTracker.track(ConsentAgree)
val updatedManifest: FinancialConnectionsSessionManifest = acceptConsent()
FinancialConnections.emitEvent(Name.CONSENT_ACQUIRED)
navigationManager.tryNavigateTo(updatedManifest.nextPane.destination(referrer = Pane.CONSENT))
updatedManifest
}.execute { copy(acceptConsent = it) }
viewModelScope.launch {
val token: Result<String> = standardRequestManager.requestToken("random_token")
runCatching {
verifyIntegrity(token.getOrThrow(), "com.random")
}.onFailure {
logger.error("Error verifying integrity", it)
}.onSuccess {
logger.info("Integrity verified successfully")
}
}

// suspend {
// eventTracker.track(ConsentAgree)
// val updatedManifest: FinancialConnectionsSessionManifest = acceptConsent()
// FinancialConnections.emitEvent(Name.CONSENT_ACQUIRED)
// navigationManager.tryNavigateTo(updatedManifest.nextPane.destination(referrer = Pane.CONSENT))
// updatedManifest
// }.execute { copy(acceptConsent = it) }
}

fun onClickableTextClick(uri: String) = viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.stripe.android.financialconnections.features.consent

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder


suspend fun verifyIntegrity(integrityToken: String, packageName: String) = withContext(Dispatchers.IO) {
val url = URL("https://attestation-android.glitch.me/verify-integrity")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
connection.setRequestProperty("Accept", "application/json")
connection.doOutput = true
connection.connectTimeout = 15000
connection.readTimeout = 15000

// Constructing URL-encoded form data
val requestBody = "integrityToken=${URLEncoder.encode(integrityToken, "UTF-8")}" +
"&packageName=${URLEncoder.encode(packageName, "UTF-8")}"

println("Request Payload: $requestBody") // Log the URL-encoded form data

connection.outputStream.use { os ->
OutputStreamWriter(os, "UTF-8").use { writer ->
writer.write(requestBody)
writer.flush()
}
}

val responseCode = connection.responseCode
val responseMessage: String

if (responseCode == HttpURLConnection.HTTP_OK) {
responseMessage = connection.inputStream.bufferedReader().use { it.readText() }
} else {
responseMessage = connection.errorStream?.bufferedReader()?.use {
it.readText()
} ?: "Error occurred with response code: $responseCode"
println("Error response code: $responseCode - $responseMessage")
}

connection.disconnect()

println("Response Message: $responseMessage")

responseMessage
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private class FinancialConnectionsManifestRepositoryImpl(
)
)

val token: Result<String> = standardRequestManager.requestToken(financialConnectionsRequest)
// val token: Result<String> = standardRequestManager.requestToken(financialConnectionsRequest)

return requestExecutor.execute(
financialConnectionsRequest,
Expand Down

0 comments on commit a1febb9

Please sign in to comment.