mirror of
https://github.com/bitwarden/android.git
synced 2026-06-06 14:28:45 -05:00
Add WebAuthCallbackActivity to handle hCaptcha callbacks (#705)
This commit is contained in:
committed by
Álison Fernandes
parent
49ff8a761d
commit
e3547f4e13
@@ -5,7 +5,6 @@ import android.os.Parcelable
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.getCaptchaCallbackTokenResult
|
||||
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
||||
@@ -50,39 +49,34 @@ class MainViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun handleFirstIntentReceived(action: MainAction.ReceiveFirstIntent) {
|
||||
val shareData = intentManager.getShareDataFromIntent(action.intent)
|
||||
when {
|
||||
shareData != null -> {
|
||||
authRepository.specialCircumstance =
|
||||
UserState.SpecialCircumstance.ShareNewSend(
|
||||
data = shareData,
|
||||
shouldFinishWhenComplete = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
handleIntent(
|
||||
intent = action.intent,
|
||||
isFirstIntent = true,
|
||||
)
|
||||
}
|
||||
|
||||
private fun handleNewIntentReceived(action: MainAction.ReceiveNewIntent) {
|
||||
val captchaCallbackTokenResult = action.intent.getCaptchaCallbackTokenResult()
|
||||
val shareData = intentManager.getShareDataFromIntent(action.intent)
|
||||
when {
|
||||
captchaCallbackTokenResult != null -> {
|
||||
authRepository.setCaptchaCallbackTokenResult(
|
||||
tokenResult = captchaCallbackTokenResult,
|
||||
)
|
||||
}
|
||||
handleIntent(
|
||||
intent = action.intent,
|
||||
isFirstIntent = false,
|
||||
)
|
||||
}
|
||||
|
||||
private fun handleIntent(
|
||||
intent: Intent,
|
||||
isFirstIntent: Boolean,
|
||||
) {
|
||||
val shareData = intentManager.getShareDataFromIntent(intent)
|
||||
when {
|
||||
shareData != null -> {
|
||||
authRepository.specialCircumstance =
|
||||
UserState.SpecialCircumstance.ShareNewSend(
|
||||
data = shareData,
|
||||
// Allow users back into the already-running app when completing the
|
||||
// Send task.
|
||||
shouldFinishWhenComplete = false,
|
||||
// Send task when this is not the first intent.
|
||||
shouldFinishWhenComplete = isFirstIntent,
|
||||
)
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.x8bit.bitwarden
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
/**
|
||||
* An activity to receive callbacks from Custom Chrome tabs or other web-auth related flows such
|
||||
* the current state of the task holding the [MainActivity] can remain undisturbed.
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class WebAuthCallbackActivity : AppCompatActivity() {
|
||||
|
||||
private val webAuthCallbackViewModel: WebAuthCallbackViewModel by viewModels()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
webAuthCallbackViewModel.trySendAction(
|
||||
WebAuthCallbackAction.IntentReceive(intent = intent),
|
||||
)
|
||||
|
||||
val intent = Intent(this, MainActivity::class.java)
|
||||
.apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
|
||||
}
|
||||
startActivity(intent)
|
||||
finishAffinity()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.x8bit.bitwarden
|
||||
|
||||
import android.content.Intent
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.getCaptchaCallbackTokenResult
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* A view model that handles logic for the [WebAuthCallbackActivity].
|
||||
*/
|
||||
@HiltViewModel
|
||||
class WebAuthCallbackViewModel @Inject constructor(
|
||||
private val authRepository: AuthRepository,
|
||||
) : BaseViewModel<Unit, Unit, WebAuthCallbackAction>(Unit) {
|
||||
override fun handleAction(action: WebAuthCallbackAction) {
|
||||
when (action) {
|
||||
is WebAuthCallbackAction.IntentReceive -> handleIntentReceived(action)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleIntentReceived(action: WebAuthCallbackAction.IntentReceive) {
|
||||
val captchaCallbackTokenResult = action.intent.getCaptchaCallbackTokenResult()
|
||||
when {
|
||||
captchaCallbackTokenResult != null -> {
|
||||
authRepository.setCaptchaCallbackTokenResult(
|
||||
tokenResult = captchaCallbackTokenResult,
|
||||
)
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions for the [WebAuthCallbackViewModel].
|
||||
*/
|
||||
sealed class WebAuthCallbackAction {
|
||||
/**
|
||||
* Receive Intent by the application.
|
||||
*/
|
||||
data class IntentReceive(val intent: Intent) : WebAuthCallbackAction()
|
||||
}
|
||||
Reference in New Issue
Block a user