Fix BE request auth aware

This commit is contained in:
Rafał Kobyłko 2023-10-27 21:36:03 +02:00
parent 4fafd68950
commit 13670a6cbb
7 changed files with 72 additions and 29 deletions

View File

@ -25,6 +25,10 @@ class AuthTracker(
reset()
}
fun onBrowserExtRequest() {
reset()
}
fun onAuthenticateScreen() {
reset()
}

View File

@ -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(),

View File

@ -134,7 +134,7 @@ internal fun BackupService.asDomain(
secret = secret,
name = name,
info = otp.account ?: otp.label,
authType = otp.tokenType?.let { enumValueOrNull<Service.AuthType>(it) } ?: Service.AuthType.TOTP,
authType = otp.tokenType?.let { enumValueOf<Service.AuthType>(it) } ?: Service.AuthType.TOTP,
link = otp.link,
issuer = otp.issuer,
period = otp.period,

View File

@ -11,6 +11,8 @@ android {
}
dependencies {
implementation(project(":base"))
implementation(project(":prefs"))
implementation(project(":core:common"))
implementation(project(":core:android"))
implementation(project(":core:locale"))

View File

@ -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>(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
}

View File

@ -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)
}
}
)
}

View File

@ -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>(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>(BrowserExtRequestPayload.Key)!!
sendBroadcast(BrowserExtRequestReceiver.createIntent(this, payload))
finish()
}
}