diff --git a/base/src/main/java/com/twofasapp/base/AuthTracker.kt b/base/src/main/java/com/twofasapp/base/AuthTracker.kt index 3324cccf..42fce7cb 100644 --- a/base/src/main/java/com/twofasapp/base/AuthTracker.kt +++ b/base/src/main/java/com/twofasapp/base/AuthTracker.kt @@ -25,6 +25,10 @@ class AuthTracker( reset() } + fun onBrowserExtRequest() { + reset() + } + fun onAuthenticateScreen() { reset() } diff --git a/core/otp/src/main/java/com/twofasapp/otp/OtpAuthenticator.kt b/core/otp/src/main/java/com/twofasapp/otp/OtpAuthenticator.kt index d50b0228..320c5209 100755 --- a/core/otp/src/main/java/com/twofasapp/otp/OtpAuthenticator.kt +++ b/core/otp/src/main/java/com/twofasapp/otp/OtpAuthenticator.kt @@ -33,7 +33,6 @@ class OtpAuthenticator { private const val HmacSha384 = "HmacSHA384" private const val HmacSha512 = "HmacSHA512" private val keyModulus = mapOf( - 5 to 10.0.pow(5.toDouble()).toLong(), 6 to 10.0.pow(6.toDouble()).toLong(), 7 to 10.0.pow(7.toDouble()).toLong(), 8 to 10.0.pow(8.toDouble()).toLong(), diff --git a/data/services/src/main/java/com/twofasapp/data/services/mapper/ServiceMapper.kt b/data/services/src/main/java/com/twofasapp/data/services/mapper/ServiceMapper.kt index 9ec35670..d8245e48 100644 --- a/data/services/src/main/java/com/twofasapp/data/services/mapper/ServiceMapper.kt +++ b/data/services/src/main/java/com/twofasapp/data/services/mapper/ServiceMapper.kt @@ -134,7 +134,7 @@ internal fun BackupService.asDomain( secret = secret, name = name, info = otp.account ?: otp.label, - authType = otp.tokenType?.let { enumValueOrNull(it) } ?: Service.AuthType.TOTP, + authType = otp.tokenType?.let { enumValueOf(it) } ?: Service.AuthType.TOTP, link = otp.link, issuer = otp.issuer, period = otp.period, diff --git a/feature/browserext/build.gradle.kts b/feature/browserext/build.gradle.kts index a0c1d30c..9b580d07 100644 --- a/feature/browserext/build.gradle.kts +++ b/feature/browserext/build.gradle.kts @@ -11,6 +11,8 @@ android { } dependencies { + implementation(project(":base")) + implementation(project(":prefs")) implementation(project(":core:common")) implementation(project(":core:android")) implementation(project(":core:locale")) diff --git a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestActivity.kt b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestActivity.kt index e8e70375..690677de 100644 --- a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestActivity.kt +++ b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestActivity.kt @@ -3,15 +3,21 @@ package com.twofasapp.feature.browserext.ui.request import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import com.twofasapp.base.AuthTracker +import com.twofasapp.base.lifecycle.AuthAware +import com.twofasapp.base.lifecycle.AuthLifecycle import com.twofasapp.data.session.SettingsRepository import com.twofasapp.designsystem.MainAppTheme import com.twofasapp.designsystem.activity.ActivityHelper import com.twofasapp.feature.browserext.notification.BrowserExtRequestPayload +import org.koin.android.ext.android.get import org.koin.android.ext.android.inject +import org.koin.core.parameter.parametersOf -class BrowserExtRequestActivity : ComponentActivity() { +class BrowserExtRequestActivity : ComponentActivity(), AuthAware { private val settingsRepository: SettingsRepository by inject() + private val authTracker: AuthTracker by inject() override fun onCreate(savedInstanceState: Bundle?) { ActivityHelper.onCreate( @@ -23,10 +29,22 @@ class BrowserExtRequestActivity : ComponentActivity() { val payload = intent.getParcelableExtra(BrowserExtRequestPayload.Key)!! + authTracker.onBrowserExtRequest() + + lifecycle.addObserver( + AuthLifecycle( + authTracker = get(), + navigator = get { parametersOf(this) }, + authAware = this as? AuthAware + ) + ) + setContent { MainAppTheme { BrowserExtRequestScreen(payload = payload) } } } + + override fun onAuthenticated() = Unit } \ No newline at end of file diff --git a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestScreen.kt b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestScreen.kt index 0a5acbe4..d9b406cf 100644 --- a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestScreen.kt +++ b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/request/BrowserExtRequestScreen.kt @@ -24,8 +24,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.core.app.NotificationManagerCompat import androidx.lifecycle.compose.collectAsStateWithLifecycle -import com.twofasapp.feature.browserext.notification.BrowserExtRequestPayload -import com.twofasapp.feature.browserext.notification.BrowserExtRequestReceiver +import androidx.lifecycle.lifecycleScope import com.twofasapp.common.domain.Service import com.twofasapp.designsystem.TwTheme import com.twofasapp.designsystem.common.TwSwitch @@ -33,7 +32,10 @@ import com.twofasapp.designsystem.common.TwTopAppBar import com.twofasapp.designsystem.ktx.currentActivity import com.twofasapp.designsystem.service.DsServiceSimple import com.twofasapp.designsystem.service.asState +import com.twofasapp.feature.browserext.notification.BrowserExtRequestPayload +import com.twofasapp.feature.browserext.notification.BrowserExtRequestReceiver import com.twofasapp.locale.TwLocale +import kotlinx.coroutines.launch import org.koin.androidx.compose.koinViewModel @Composable @@ -52,22 +54,24 @@ internal fun BrowserExtRequestScreen( uiState = uiState, onSaveMyChoiceToggle = { viewModel.toggleSaveMyChoice() }, onServiceClick = { service -> - viewModel.assignDomain(service) + activity.lifecycleScope.launch { + viewModel.assignDomain(service) - // Clear notification - NotificationManagerCompat - .from(activity) - .cancel(null, payload.requestId.hashCode()) + // Clear notification + NotificationManagerCompat + .from(activity) + .cancel(null, payload.requestId.hashCode()) - // Launch broadcast - val intent = Intent(activity, BrowserExtRequestReceiver::class.java) - .apply { - action = BrowserExtRequestReceiver.ACTION - putExtra(BrowserExtRequestPayload.Key, payload.copy(serviceId = service.id)) - } + // Launch broadcast + val intent = Intent(activity, BrowserExtRequestReceiver::class.java) + .apply { + action = BrowserExtRequestReceiver.ACTION + putExtra(BrowserExtRequestPayload.Key, payload.copy(serviceId = service.id)) + } - activity.sendBroadcast(intent) - activity.finish() + activity.finish() + activity.sendBroadcast(intent) + } } ) } diff --git a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/requestapprove/BrowserExtRequestApproveActivity.kt b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/requestapprove/BrowserExtRequestApproveActivity.kt index 8005680d..ef54151b 100644 --- a/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/requestapprove/BrowserExtRequestApproveActivity.kt +++ b/feature/browserext/src/main/java/com/twofasapp/feature/browserext/ui/requestapprove/BrowserExtRequestApproveActivity.kt @@ -2,18 +2,21 @@ package com.twofasapp.feature.browserext.ui.requestapprove import android.os.Bundle import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent -import androidx.compose.material3.Surface +import com.twofasapp.base.AuthTracker +import com.twofasapp.base.lifecycle.AuthAware +import com.twofasapp.base.lifecycle.AuthLifecycle import com.twofasapp.data.session.SettingsRepository -import com.twofasapp.designsystem.MainAppTheme import com.twofasapp.designsystem.activity.ActivityHelper import com.twofasapp.feature.browserext.notification.BrowserExtRequestPayload import com.twofasapp.feature.browserext.notification.BrowserExtRequestReceiver +import org.koin.android.ext.android.get import org.koin.android.ext.android.inject +import org.koin.core.parameter.parametersOf -class BrowserExtRequestApproveActivity : ComponentActivity() { +class BrowserExtRequestApproveActivity : ComponentActivity(), AuthAware { private val settingsRepository: SettingsRepository by inject() + private val authTracker: AuthTracker by inject() override fun onCreate(savedInstanceState: Bundle?) { ActivityHelper.onCreate( @@ -22,15 +25,28 @@ class BrowserExtRequestApproveActivity : ComponentActivity() { allowScreenshots = settingsRepository.getAppSettings().allowScreenshots, ) super.onCreate(savedInstanceState) + val payload = intent.getParcelableExtra(BrowserExtRequestPayload.Key)!! - setContent { - MainAppTheme { - Surface { - sendBroadcast(BrowserExtRequestReceiver.createIntent(this, payload)) - finish() - } - } + if (payload.action == BrowserExtRequestPayload.Action.Deny) { + sendBroadcast(BrowserExtRequestReceiver.createIntent(this, payload)) + finish() + } else { + authTracker.onBrowserExtRequest() + + lifecycle.addObserver( + AuthLifecycle( + authTracker = get(), + navigator = get { parametersOf(this) }, + authAware = this as? AuthAware + ) + ) } } + + override fun onAuthenticated() { + val payload = intent.getParcelableExtra(BrowserExtRequestPayload.Key)!! + sendBroadcast(BrowserExtRequestReceiver.createIntent(this, payload)) + finish() + } }