mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 06:49:07 -05:00
BIT-332: Launch web view for hcaptchca (#73)
This commit is contained in:
committed by
Álison Fernandes
parent
5fc78afa0a
commit
02a1445f39
@@ -0,0 +1,32 @@
|
||||
package com.x8bit.bitwarden.data.auth.datasource.network.util
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.LoginResult
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import java.util.Base64
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Generates an [Intent] to display a CAPTCHA challenge for Bitwarden authentication.
|
||||
*/
|
||||
fun LoginResult.CaptchaRequired.generateIntentForCaptcha(): Intent {
|
||||
val json = buildJsonObject {
|
||||
put(key = "siteKey", value = captchaId)
|
||||
put(key = "locale", value = Locale.getDefault().toString())
|
||||
put(key = "callbackUri", value = "bitwarden://captcha-callback")
|
||||
put(key = "captchaRequiredText", value = "Captcha required")
|
||||
}
|
||||
val base64Data = Base64
|
||||
.getEncoder()
|
||||
.encodeToString(
|
||||
json
|
||||
.toString()
|
||||
.toByteArray(Charsets.UTF_8),
|
||||
)
|
||||
val parentParam = "bitwarden%3A%2F%2Fcaptcha-callback"
|
||||
val url = "https://vault.bitwarden.com/captcha-mobile-connector.html" +
|
||||
"?data=$base64Data&parent=$parentParam&v=1"
|
||||
return Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -21,6 +22,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.EventsEffect
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.IntentHandler
|
||||
import com.x8bit.bitwarden.ui.platform.components.BitwardenTextField
|
||||
|
||||
/**
|
||||
@@ -31,11 +33,13 @@ import com.x8bit.bitwarden.ui.platform.components.BitwardenTextField
|
||||
fun LoginScreen(
|
||||
onNavigateToLanding: () -> Unit,
|
||||
viewModel: LoginViewModel = hiltViewModel(),
|
||||
intentHandler: IntentHandler = IntentHandler(context = LocalContext.current),
|
||||
) {
|
||||
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
|
||||
EventsEffect(viewModel = viewModel) { event ->
|
||||
when (event) {
|
||||
LoginEvent.NavigateToLanding -> onNavigateToLanding()
|
||||
is LoginEvent.NavigateToCaptcha -> intentHandler.startActivity(intent = event.intent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.login
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Parcelable
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.LoginResult
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.util.generateIntentForCaptcha
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -60,8 +62,13 @@ class LoginViewModel @Inject constructor(
|
||||
LoginResult.Error -> Unit
|
||||
// No action required on success, root nav will navigate to logged in state
|
||||
LoginResult.Success -> Unit
|
||||
// TODO: launch intent with captcha URL BIT-399
|
||||
is LoginResult.CaptchaRequired -> Unit
|
||||
is LoginResult.CaptchaRequired -> {
|
||||
sendEvent(
|
||||
event = LoginEvent.NavigateToCaptcha(
|
||||
intent = result.generateIntentForCaptcha(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,6 +104,11 @@ sealed class LoginEvent {
|
||||
* Navigates to the Landing screen.
|
||||
*/
|
||||
data object NavigateToLanding : LoginEvent()
|
||||
|
||||
/**
|
||||
* Navigates to the captcha verification screen.
|
||||
*/
|
||||
data class NavigateToCaptcha(val intent: Intent) : LoginEvent()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.x8bit.bitwarden.ui.platform.base.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
||||
/**
|
||||
* A utility class for simplifying the handling of Android Intents within a given context.
|
||||
*/
|
||||
class IntentHandler(private val context: Context) {
|
||||
|
||||
/**
|
||||
* Start an activity using the provided [Intent].
|
||||
*/
|
||||
fun startActivity(intent: Intent) {
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user