mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 14:57:41 -05:00
BIT-764: Chrome Custom Tabs for hcpatcha verification (#105)
This commit is contained in:
committed by
Álison Fernandes
parent
5a2a2f93f3
commit
635cc120c9
@@ -13,9 +13,9 @@ private const val CAPTCHA_HOST: String = "captcha-callback"
|
||||
private const val CALLBACK_URI = "bitwarden://$CAPTCHA_HOST"
|
||||
|
||||
/**
|
||||
* Generates an [Intent] to display a CAPTCHA challenge for Bitwarden authentication.
|
||||
* Generates a [Uri] to display a CAPTCHA challenge for Bitwarden authentication.
|
||||
*/
|
||||
fun LoginResult.CaptchaRequired.generateIntentForCaptcha(): Intent {
|
||||
fun LoginResult.CaptchaRequired.generateUriForCaptcha(): Uri {
|
||||
val json = buildJsonObject {
|
||||
put(key = "siteKey", value = captchaId)
|
||||
put(key = "locale", value = Locale.getDefault().toString())
|
||||
@@ -32,7 +32,7 @@ fun LoginResult.CaptchaRequired.generateIntentForCaptcha(): Intent {
|
||||
val parentParam = URLEncoder.encode(CALLBACK_URI, "UTF-8")
|
||||
val url = "https://vault.bitwarden.com/captcha-mobile-connector.html" +
|
||||
"?data=$base64Data&parent=$parentParam&v=1"
|
||||
return Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||
return Uri.parse(url)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,7 +49,9 @@ fun LoginScreen(
|
||||
EventsEffect(viewModel = viewModel) { event ->
|
||||
when (event) {
|
||||
LoginEvent.NavigateBack -> onNavigateBack()
|
||||
is LoginEvent.NavigateToCaptcha -> intentHandler.startActivity(intent = event.intent)
|
||||
is LoginEvent.NavigateToCaptcha -> {
|
||||
intentHandler.startCustomTabsActivity(uri = event.uri)
|
||||
}
|
||||
is LoginEvent.ShowToast -> {
|
||||
Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
package com.x8bit.bitwarden.ui.auth.feature.login
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Parcelable
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.LoginResult
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.util.CaptchaCallbackTokenResult
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.util.generateIntentForCaptcha
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.util.generateUriForCaptcha
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
@@ -84,7 +84,7 @@ class LoginViewModel @Inject constructor(
|
||||
mutableStateFlow.update { it.copy(loadingDialogState = LoadingDialogState.Hidden) }
|
||||
sendEvent(
|
||||
event = LoginEvent.NavigateToCaptcha(
|
||||
intent = loginResult.generateIntentForCaptcha(),
|
||||
uri = loginResult.generateUriForCaptcha(),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -201,7 +201,7 @@ sealed class LoginEvent {
|
||||
/**
|
||||
* Navigates to the captcha verification screen.
|
||||
*/
|
||||
data class NavigateToCaptcha(val intent: Intent) : LoginEvent()
|
||||
data class NavigateToCaptcha(val uri: Uri) : LoginEvent()
|
||||
|
||||
/**
|
||||
* Shows a toast with the given [message].
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.x8bit.bitwarden.ui.platform.base.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
|
||||
/**
|
||||
* A utility class for simplifying the handling of Android Intents within a given context.
|
||||
@@ -14,4 +16,14 @@ class IntentHandler(private val context: Context) {
|
||||
fun startActivity(intent: Intent) {
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a Custom Tabs Activity using the provided [Uri].
|
||||
*/
|
||||
fun startCustomTabsActivity(uri: Uri) {
|
||||
CustomTabsIntent
|
||||
.Builder()
|
||||
.build()
|
||||
.launchUrl(context, uri)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user