Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix improper handling of rejected identity verification #4

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Crashing when received unexpected error from an identity provider
- Exiting the wallet after accepting an identity verification error
- Incorrect environment name in a private key export file for Mainnet
- Improper handling of rejected identity verification when setting up a new wallet

### Changed
- Suggest running a recovery when facing account or identity creation errors
Expand Down
33 changes: 7 additions & 26 deletions app/src/main/java/com/concordium/wallet/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import com.concordium.wallet.ui.auth.login.AuthLoginActivity
import com.concordium.wallet.ui.base.BaseActivity
import com.concordium.wallet.ui.common.delegates.AuthDelegate
import com.concordium.wallet.ui.common.delegates.AuthDelegateImpl
import com.concordium.wallet.ui.common.identity.IdentityErrorDialogHelper
import com.concordium.wallet.ui.common.delegates.IdentityStatusDelegate
import com.concordium.wallet.ui.common.delegates.IdentityStatusDelegateImpl
import com.concordium.wallet.ui.identity.identityproviderlist.IdentityProviderListActivity
import com.concordium.wallet.ui.more.import.ImportActivity
import com.concordium.wallet.ui.more.moreoverview.MoreOverviewFragment
Expand All @@ -23,12 +24,11 @@ import com.concordium.wallet.ui.walletconnect.WalletConnectView
import com.concordium.wallet.ui.walletconnect.WalletConnectViewModel
import com.concordium.wallet.ui.welcome.WelcomeActivity
import com.concordium.wallet.ui.welcome.WelcomePromoActivity
import com.concordium.wallet.uicore.dialog.CustomDialogFragment
import com.concordium.wallet.uicore.dialog.Dialogs
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class MainActivity : BaseActivity(R.layout.activity_main, R.string.accounts_overview_title),
Dialogs.DialogFragmentListener, AuthDelegate by AuthDelegateImpl() {
AuthDelegate by AuthDelegateImpl(),
IdentityStatusDelegate by IdentityStatusDelegateImpl() {

companion object {
const val EXTRA_CREATE_FIRST_IDENTITY = "EXTRA_CREATE_FIRST_IDENTITY"
Expand Down Expand Up @@ -102,6 +102,7 @@ class MainActivity : BaseActivity(R.layout.activity_main, R.string.accounts_over
}

viewModel.startIdentityUpdate()
startCheckForPendingIdentity(this, null, false) {}
}
}
}
Expand All @@ -125,14 +126,6 @@ class MainActivity : BaseActivity(R.layout.activity_main, R.string.accounts_over
handlePossibleWalletConnectUri(newIntent)
}

override fun onDialogResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == RequestCodes.REQUEST_IDENTITY_ERROR_DIALOG) {
if (resultCode == Dialogs.POSITIVE) {
gotoIdentityProviderList()
}
}
}

//endregion

//region Initialize
Expand All @@ -154,16 +147,6 @@ class MainActivity : BaseActivity(R.layout.activity_main, R.string.accounts_over
replaceFragment(state)
}

viewModel.identityErrorLiveData.observe(this) { data ->
data?.let {
IdentityErrorDialogHelper.showIdentityError(this, dialogs, data)
}
}

viewModel.newFinalizedAccountLiveData.observe(this) {
CustomDialogFragment.newAccountFinalizedDialog(this)
}

walletConnectViewModel = ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application)
Expand Down Expand Up @@ -278,10 +261,8 @@ class MainActivity : BaseActivity(R.layout.activity_main, R.string.accounts_over
}

private fun gotoIdentityProviderList() {
viewModel.identityErrorLiveData.value?.let {
val intent = Intent(this, IdentityProviderListActivity::class.java)
startActivity(intent)
}
val intent = Intent(this, IdentityProviderListActivity::class.java)
startActivity(intent)
}

private fun goToFirstIdentityCreation() {
Expand Down
15 changes: 0 additions & 15 deletions app/src/main/java/com/concordium/wallet/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import com.concordium.wallet.data.room.Account
import com.concordium.wallet.data.room.Identity
import com.concordium.wallet.data.room.WalletDatabase
import com.concordium.wallet.ui.common.identity.IdentityUpdater
import com.concordium.wallet.ui.identity.identityconfirmed.IdentityErrorData
import com.concordium.wallet.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainViewModel(application: Application) : AndroidViewModel(application) {
Expand All @@ -32,10 +30,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {

var databaseVersionAllowed = true

private val _newFinalizedAccountLiveData = MutableLiveData<String>()
val newFinalizedAccountLiveData: LiveData<String>
get() = _newFinalizedAccountLiveData

private val _titleLiveData = MutableLiveData<String>()
val titleLiveData: LiveData<String>
get() = _titleLiveData
Expand All @@ -44,10 +38,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val stateLiveData: LiveData<State>
get() = _stateLiveData

private val _identityErrorLiveData = MutableLiveData<IdentityErrorData>()
val identityErrorLiveData: LiveData<IdentityErrorData>
get() = _identityErrorLiveData

val canAcceptImportFiles: Boolean
get() = App.appCore.session.isAccountsBackupPossible()

Expand Down Expand Up @@ -101,18 +91,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
fun startIdentityUpdate() {
val updateListener = object : IdentityUpdater.UpdateListener {
override fun onError(identity: Identity, account: Account?) {
viewModelScope.launch(Dispatchers.IO) {
val isFirst = isFirst(identityRepository.getCount())
_identityErrorLiveData.postValue(IdentityErrorData(identity, account, isFirst))
}
}

override fun onDone() {
}

override fun onNewAccountFinalized(accountName: String) {
viewModelScope.launch {
_newFinalizedAccountLiveData.postValue(accountName)
App.appCore.session.setAccountsBackedUp(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.concordium.wallet.ui.base.BaseFragment
import com.concordium.wallet.ui.cis2.SendTokenActivity
import com.concordium.wallet.ui.identity.identityproviderlist.IdentityProviderListActivity
import com.concordium.wallet.ui.more.export.ExportActivity
import com.concordium.wallet.uicore.dialog.CustomDialogFragment
import com.concordium.wallet.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -133,10 +132,6 @@ class AccountsOverviewFragment : BaseFragment() {
}
})

viewModel.newFinalizedAccountLiveData.observe(viewLifecycleOwner) {
CustomDialogFragment.newAccountFinalizedDialog(requireContext())
}

viewModel.stateLiveData.observe(viewLifecycleOwner) { state ->
when (state) {
AccountsOverviewViewModel.State.NO_IDENTITIES -> showStateNoIdentities()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.concordium.wallet.ui.common.delegates

import android.content.Intent
import androidx.core.app.ComponentActivity
import com.concordium.wallet.App
import com.concordium.wallet.R
import com.concordium.wallet.data.IdentityRepository
import com.concordium.wallet.data.model.IdentityStatus
import com.concordium.wallet.data.room.Identity
import com.concordium.wallet.data.room.WalletDatabase
import com.concordium.wallet.ui.MainViewModel
import com.concordium.wallet.ui.base.BaseActivity
import com.concordium.wallet.ui.identity.identityconfirmed.IdentityConfirmedActivity
import com.concordium.wallet.ui.identity.identityproviderlist.IdentityProviderListActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import java.util.Timer
import java.util.TimerTask
import kotlin.concurrent.schedule

interface IdentityStatusDelegate {
Expand All @@ -24,12 +30,23 @@ interface IdentityStatusDelegate {
statusChanged: (Identity) -> Unit
)

fun identityDone(
activity: ComponentActivity,
identity: Identity,
statusChanged: (Identity) -> Unit
)

fun identityError(
activity: ComponentActivity,
identity: Identity,
statusChanged: (Identity) -> Unit
)

fun stopCheckForPendingIdentity()
}

class IdentityStatusDelegateImpl : IdentityStatusDelegate {
private var job: Job? = null
private var timerTask: TimerTask? = null
private var showForFirstIdentity = false

override fun startCheckForPendingIdentity(
Expand All @@ -39,7 +56,6 @@ class IdentityStatusDelegateImpl : IdentityStatusDelegate {
statusChanged: (Identity) -> Unit
) {
this.showForFirstIdentity = showForFirstIdentity

if (activity == null || activity.isFinishing || activity.isDestroyed)
return
if (App.appCore.newIdentities.isNotEmpty()) {
Expand All @@ -51,33 +67,38 @@ class IdentityStatusDelegateImpl : IdentityStatusDelegate {
WalletDatabase.getDatabase(activity).identityDao()
)
val identity = identityRepository.findById(newIdentity.key)
if (isActive) {
identity?.let {
activity.runOnUiThread {
if ((activity as BaseActivity).isActive) {
statusChanged(identity)
identity?.let {
activity.runOnUiThread {
if ((activity as BaseActivity).isActive) {
when (identity.status) {
IdentityStatus.DONE -> identityDone(
activity,
identity,
statusChanged
)

IdentityStatus.ERROR -> identityError(
activity,
identity,
statusChanged
)
}
}
startCheckForPendingIdentity(
activity,
specificIdentityId,
showForFirstIdentity,
statusChanged
)
}

delay(1000)

startCheckForPendingIdentity(
activity,
specificIdentityId,
showForFirstIdentity,
statusChanged
)
}
delay(1000)
}
}
}
}
} else {
// TODO refactor to use one way of scheduling and cancellation.
// Weird... Seems like there could be a race
// between the timer and the delayed coroutine from above.
timerTask = Timer().schedule(1000) {
Timer().schedule(1000) {
startCheckForPendingIdentity(
activity,
specificIdentityId,
Expand All @@ -90,6 +111,109 @@ class IdentityStatusDelegateImpl : IdentityStatusDelegate {

override fun stopCheckForPendingIdentity() {
job?.cancel()
timerTask?.cancel()
}

override fun identityDone(
activity: ComponentActivity,
identity: Identity,
statusChanged: (Identity) -> Unit
) {
if (App.appCore.newIdentities[identity.id] == null)
return
App.appCore.newIdentities.remove(identity.id)

if (showForFirstIdentity) {
statusChanged(identity)
return
}

val builder = MaterialAlertDialogBuilder(activity)
builder.setTitle(R.string.identities_overview_identity_verified_title)
builder.setMessage(
activity.getString(
R.string.identities_overview_identity_verified_message,
identity.name
)
)
builder.setPositiveButton(activity.getString(R.string.identities_overview_identity_create_account_now)) { dialog, _ ->
dialog.dismiss()
val intent = Intent(activity, IdentityConfirmedActivity::class.java)
intent.putExtra(IdentityConfirmedActivity.EXTRA_IDENTITY, identity)
intent.putExtra(IdentityConfirmedActivity.SHOW_FOR_CREATE_ACCOUNT, true)
activity.startActivity(intent)
EventBus.getDefault().post(MainViewModel.State.AccountOverview)
}
builder.setNegativeButton(activity.getString(R.string.identities_overview_identity_later)) { dialog, _ ->
dialog.dismiss()
}
val dialog = builder.create()
dialog.setCanceledOnTouchOutside(false)
statusChanged(identity)
dialog.show()
}

override fun identityError(
activity: ComponentActivity,
identity: Identity,
statusChanged: (Identity) -> Unit
) {
if (App.appCore.newIdentities[identity.id] == null)
return
App.appCore.newIdentities.remove(identity.id)

val builder = MaterialAlertDialogBuilder(activity)
builder.setTitle(R.string.identities_overview_identity_rejected_title)

if (showForFirstIdentity)
identityErrorFirstIdentity(activity, identity, builder)
else
identityErrorNextIdentity(activity, identity, builder, statusChanged)

val dialog = builder.create()
dialog.setCanceledOnTouchOutside(false)
statusChanged(identity)
dialog.show()
}

private fun identityErrorNextIdentity(
activity: ComponentActivity,
identity: Identity,
builder: MaterialAlertDialogBuilder,
statusChanged: (Identity) -> Unit
) {
builder.setMessage(
activity.getString(
R.string.identities_overview_identity_rejected_text,
"${identity.name}\n${identity.detail ?: ""}"
)
)
builder.setPositiveButton(activity.getString(R.string.identities_overview_identity_request_another)) { dialog, _ ->
dialog.dismiss()
activity.startActivity(Intent(activity, IdentityProviderListActivity::class.java))
}
builder.setNegativeButton(activity.getString(R.string.identities_overview_identity_later)) { dialog, _ ->
dialog.dismiss()
statusChanged(identity)
}
}

private fun identityErrorFirstIdentity(
activity: ComponentActivity,
identity: Identity,
builder: MaterialAlertDialogBuilder
) {
builder.setMessage(
activity.getString(
R.string.identities_overview_identity_rejected_first_text,
"${identity.name}\n${identity.detail ?: ""}"
)
)
builder.setPositiveButton(activity.getString(R.string.identities_overview_identity_make_new)) { dialog, _ ->
dialog.dismiss()
activity.finish()
val intent = Intent(activity, IdentityProviderListActivity::class.java)
intent.putExtra(IdentityProviderListActivity.SHOW_FOR_FIRST_IDENTITY, true)
activity.startActivity(intent)
}
}
}
Loading
Loading