Skip to content

Commit

Permalink
Merge branch 'main' into workflow/publish-to-google-play-store
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintPatrck authored Apr 27, 2024
2 parents 730a4db + dd4a0a5 commit e39c3bf
Show file tree
Hide file tree
Showing 44 changed files with 1,627 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "8724b95439edde85bd15e0bd2e02195e",
"identityHash": "480a4540e7704429515a28eb9c38ab14",
"entities": [
{
"tableName": "items",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `key` TEXT NOT NULL, `accountName` TEXT NOT NULL, `type` TEXT NOT NULL, `algorithm` TEXT NOT NULL, `period` INTEGER NOT NULL, `digits` INTEGER NOT NULL, `issuer` TEXT, `userId` TEXT, PRIMARY KEY(`id`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `key` TEXT NOT NULL, `type` TEXT NOT NULL, `algorithm` TEXT NOT NULL, `period` INTEGER NOT NULL, `digits` INTEGER NOT NULL, `issuer` TEXT NOT NULL, `userId` TEXT, `accountName` TEXT, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
Expand All @@ -20,12 +20,6 @@
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "accountName",
"columnName": "accountName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
Expand Down Expand Up @@ -54,13 +48,19 @@
"fieldPath": "issuer",
"columnName": "issuer",
"affinity": "TEXT",
"notNull": false
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "userId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "accountName",
"columnName": "accountName",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
Expand All @@ -76,7 +76,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8724b95439edde85bd15e0bd2e02195e')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '480a4540e7704429515a28eb9c38ab14')"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bitwarden.authenticator.data.authenticator.datasource.disk.entity

import android.net.Uri
import androidx.core.text.htmlEncode
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
Expand All @@ -16,9 +18,6 @@ data class AuthenticatorItemEntity(
@ColumnInfo(name = "key")
val key: String,

@ColumnInfo(name = "accountName")
val accountName: String,

@ColumnInfo(name = "type")
val type: AuthenticatorItemType = AuthenticatorItemType.TOTP,

Expand All @@ -32,8 +31,42 @@ data class AuthenticatorItemEntity(
val digits: Int = 6,

@ColumnInfo(name = "issuer")
val issuer: String? = null,
val issuer: String,

@ColumnInfo(name = "userId")
val userId: String? = null,
)

@ColumnInfo(name = "accountName")
val accountName: String? = null,
) {
fun toOtpAuthUriString(): String {
return when (type) {
AuthenticatorItemType.TOTP -> {
val label = if (accountName.isNullOrBlank()) {
issuer
} else {
"$issuer:$accountName"
}
Uri.Builder()
.scheme("otpauth")
.authority("totp")
.appendPath(label.htmlEncode())
.appendQueryParameter("secret", key)
.appendQueryParameter("algorithm", algorithm.name)
.appendQueryParameter("digits", digits.toString())
.appendQueryParameter("period", period.toString())
.appendQueryParameter("issuer", issuer)
.build()
.toString()
}

AuthenticatorItemType.STEAM -> {
if (key.startsWith("steam://")) {
key
} else {
"steam://$key"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TotpCodeManagerImpl @Inject constructor(

return mutableVerificationCodeStateFlowMap.getOrPut(itemEntity) {
flow<DataState<VerificationCodeItem?>> {
val totpCode = itemEntity.key
val totpCode = itemEntity.toOtpAuthUriString()

var item: VerificationCodeItem? = null
while (currentCoroutineContext().isActive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,10 @@ data class ExportJsonData(
* This model is loosely based off of Bitwarden's Cipher.Login JSON.
*
* @property totp OTP secret used to generate a verification code.
* @property issuer Optional issuer of the 2fa code.
* @property period Optional refresh period in seconds. Default is 30.
* @property digits Optional number of digits in the verification code. Default is 6
*/
@Serializable
data class ItemLoginData(
val totp: String,
val issuer: String?,
val period: Int,
val digits: Int,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class VerificationCodeItem(
val issuer: String?,
) {
/**
* The composite label of the authenticator item.
* The composite label of the authenticator item. Used for constructing an OTPAuth URI.
* ```
* label = issuer (“:” / “%3A”) *”%20” username
* ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class AuthenticatorRepositoryImpl @Inject constructor(

private suspend fun encodeVaultDataToCsv(fileUri: Uri): ExportDataResult {
val headerLine =
"folder,favorite,type,name,login_uri,login_totp,issuer,period,digits"
"folder,favorite,type,name,login_uri,login_totp"
val dataLines = authenticatorDiskSource
.getItems()
.firstOrNull()
Expand All @@ -250,7 +250,7 @@ class AuthenticatorRepositoryImpl @Inject constructor(
}

private fun AuthenticatorItemEntity.toCsvFormat() =
",,1,$accountName,,$key,$issuer,$period,$digits"
",,1,$issuer,,${toOtpAuthUriString()},$issuer,$period,$digits"

private suspend fun encodeVaultDataToJson(fileUri: Uri): ExportDataResult {
val dataString: String = Json.encodeToString(
Expand Down Expand Up @@ -281,14 +281,11 @@ class AuthenticatorRepositoryImpl @Inject constructor(
folderId = null,
organizationId = null,
collectionIds = null,
name = accountName,
name = issuer,
notes = null,
type = 1,
login = ExportJsonData.ExportItem.ItemLoginData(
totp = key,
issuer = issuer,
period = period,
digits = digits,
totp = toOtpAuthUriString(),
),
favorite = false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import com.bitwarden.authenticator.data.authenticator.datasource.disk.entity.Aut
*/
data class UpdateItemRequest(
val key: String,
val accountName: String,
val accountName: String?,
val type: AuthenticatorItemType,
val algorithm: AuthenticatorItemAlgorithm,
val period: Int,
val digits: Int,
val issuer: String?,
val issuer: String,
) {
/**
* The composite label of the authenticator item. Derived from combining [issuer] and [accountName]
* ```
* label = accountName /issuer (“:” / “%3A”) *”%20” accountName
* ```
*/
val label = if (issuer != null) {
"$issuer:$accountName"
val label = if (accountName.isNullOrBlank()) {
issuer
} else {
accountName
"$issuer:$accountName"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bitwarden.authenticator.ui.auth.unlock

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable

const val UNLOCK_ROUTE: String = "unlock"

/**
* Navigate to the unlock screen.
*/
fun NavController.navigateToUnlock(
navOptions: NavOptions? = null,
) {
navigate(route = UNLOCK_ROUTE, navOptions = navOptions)
}

/**
* Add the unlock screen to the nav graph.
*/
fun NavGraphBuilder.unlockDestination(
onUnlocked: () -> Unit,
) {
composable(route = UNLOCK_ROUTE) {
UnlockScreen(onUnlocked = onUnlocked)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.bitwarden.authenticator.ui.auth.unlock

import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bitwarden.authenticator.R
import com.bitwarden.authenticator.ui.platform.base.util.EventsEffect
import com.bitwarden.authenticator.ui.platform.base.util.asText
import com.bitwarden.authenticator.ui.platform.components.button.BitwardenOutlinedButton
import com.bitwarden.authenticator.ui.platform.components.dialog.BasicDialogState
import com.bitwarden.authenticator.ui.platform.components.dialog.BitwardenBasicDialog
import com.bitwarden.authenticator.ui.platform.components.dialog.BitwardenLoadingDialog
import com.bitwarden.authenticator.ui.platform.components.dialog.LoadingDialogState
import com.bitwarden.authenticator.ui.platform.components.scaffold.BitwardenScaffold
import com.bitwarden.authenticator.ui.platform.manager.biometrics.BiometricsManager
import com.bitwarden.authenticator.ui.platform.theme.LocalBiometricsManager

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UnlockScreen(
viewModel: UnlockViewModel = hiltViewModel(),
biometricsManager: BiometricsManager = LocalBiometricsManager.current,
onUnlocked: () -> Unit,
) {

val state by viewModel.stateFlow.collectAsStateWithLifecycle()
val context = LocalContext.current
val resources = context.resources
var showBiometricsPrompt by remember { mutableStateOf(true) }

EventsEffect(viewModel = viewModel) { event ->
when (event) {
is UnlockEvent.ShowToast -> {
Toast.makeText(context, event.message(resources), Toast.LENGTH_SHORT).show()
}

UnlockEvent.NavigateToItemListing -> onUnlocked()
}
}

when (val dialog = state.dialog) {
is UnlockState.Dialog.Error -> BitwardenBasicDialog(
visibilityState = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = dialog.message
),
onDismissRequest = remember(viewModel) {
{
viewModel.trySendAction(UnlockAction.DismissDialog)
}
},
)

UnlockState.Dialog.Loading -> BitwardenLoadingDialog(
visibilityState = LoadingDialogState.Shown(R.string.loading.asText())
)

null -> Unit
}

val onBiometricsUnlock: () -> Unit = remember(viewModel) {
{ viewModel.trySendAction(UnlockAction.BiometricsUnlock) }
}
val onBiometricsLockOut: () -> Unit = remember(viewModel) {
{ viewModel.trySendAction(UnlockAction.BiometricsLockout) }
}

if (showBiometricsPrompt) {
biometricsManager.promptBiometrics(
onSuccess = {
showBiometricsPrompt = false
onBiometricsUnlock()
},
onCancel = {
showBiometricsPrompt = false
},
onError = {
showBiometricsPrompt = false
},
onLockOut = {
showBiometricsPrompt = false
onBiometricsLockOut()
},
)
}

BitwardenScaffold(
modifier = Modifier
.fillMaxSize()
) { innerPadding ->
Box {
Column(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(id = R.drawable.ic_logo_horizontal),
contentDescription = stringResource(R.string.bitwarden_authenticator)
)
Spacer(modifier = Modifier.height(12.dp))
BitwardenOutlinedButton(
label = stringResource(id = R.string.use_biometrics_to_unlock),
onClick = {
biometricsManager.promptBiometrics(
onSuccess = onBiometricsUnlock,
onCancel = {
// no-op
},
onError = {
// no-op
},
onLockOut = onBiometricsLockOut,
)
},
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth(),
)
}
}
}
}
Loading

0 comments on commit e39c3bf

Please sign in to comment.