Skip to content

Commit

Permalink
Make issuer required and account name optional (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintPatrck authored Apr 25, 2024
1 parent cb158b0 commit 1f0881f
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 90 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
Expand Up @@ -139,17 +139,17 @@ fun EditItemScreen(
.imePadding()
.padding(innerPadding),
viewState = viewState,
onAccountNameTextChange = remember(viewModel) {
onIssuerNameTextChange = remember(viewModel) {
{
viewModel.trySendAction(
EditItemAction.AccountNameTextChange(it)
EditItemAction.IssuerNameTextChange(it)
)
}
},
onIssuerTextChange = remember(viewModel) {
onUsernameTextChange = remember(viewModel) {
{
viewModel.trySendAction(
EditItemAction.IssuerTextChange(it)
EditItemAction.UsernameTextChange(it)
)
}
},
Expand Down Expand Up @@ -211,8 +211,8 @@ fun EditItemScreen(
fun EditItemContent(
modifier: Modifier = Modifier,
viewState: EditItemState.ViewState.Content,
onAccountNameTextChange: (String) -> Unit = {},
onIssuerTextChange: (String) -> Unit = {},
onIssuerNameTextChange: (String) -> Unit = {},
onUsernameTextChange: (String) -> Unit = {},
onTypeOptionClicked: (AuthenticatorItemType) -> Unit = {},
onTotpCodeTextChange: (String) -> Unit = {},
onAlgorithmOptionClicked: (AuthenticatorItemAlgorithm) -> Unit = {},
Expand All @@ -237,8 +237,8 @@ fun EditItemContent(
.fillMaxSize()
.padding(horizontal = 16.dp),
label = stringResource(id = R.string.name),
value = viewState.itemData.accountName,
onValueChange = onAccountNameTextChange,
value = viewState.itemData.issuer,
onValueChange = onIssuerNameTextChange,
singleLine = true,
)
}
Expand All @@ -256,20 +256,18 @@ fun EditItemContent(
)
}

viewState.itemData.issuer?.let { issuer ->
item {
Spacer(modifier = Modifier.height(8.dp))
BitwardenTextField(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
label = stringResource(id = R.string.account_info),
value = issuer,
onValueChange = onIssuerTextChange,
label = stringResource(id = R.string.username),
value = viewState.itemData.username.orEmpty(),
onValueChange = onUsernameTextChange,
singleLine = true,
)
}
}
}

Spacer(modifier = Modifier.height(8.dp))
Expand Down Expand Up @@ -483,7 +481,7 @@ private fun EditItemContentExpandedOptionsPreview() {
refreshPeriod = AuthenticatorRefreshPeriodOption.THIRTY,
totpCode = "123456",
type = AuthenticatorItemType.TOTP,
accountName = "account name",
username = "account name",
issuer = "issuer",
algorithm = AuthenticatorItemAlgorithm.SHA1,
digits = VerificationCodeDigitsOption.SIX
Expand All @@ -502,7 +500,7 @@ private fun EditItemContentCollapsedOptionsPreview() {
refreshPeriod = AuthenticatorRefreshPeriodOption.THIRTY,
totpCode = "123456",
type = AuthenticatorItemType.TOTP,
accountName = "account name",
username = "account name",
issuer = "issuer",
algorithm = AuthenticatorItemAlgorithm.SHA1,
digits = VerificationCodeDigitsOption.SIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class EditItemViewModel @Inject constructor(
is EditItemAction.AlgorithmOptionClick -> handleAlgorithmOptionClick(action)
is EditItemAction.CancelClick -> handleCancelClick()
is EditItemAction.TypeOptionClick -> handleTypeOptionClick(action)
is EditItemAction.AccountNameTextChange -> handleAccountNameTextChange(action)
is EditItemAction.IssuerTextChange -> handleIssuerTextChange(action)
is EditItemAction.IssuerNameTextChange -> handleIssuerNameTextChange(action)
is EditItemAction.UsernameTextChange -> handleIssuerTextChange(action)
is EditItemAction.RefreshPeriodOptionClick -> handlePeriodTextChange(action)
is EditItemAction.TotpCodeTextChange -> handleTotpCodeTextChange(action)
is EditItemAction.NumberOfDigitsOptionClick -> handleNumberOfDigitsOptionChange(action)
Expand All @@ -76,7 +76,7 @@ class EditItemViewModel @Inject constructor(
}

private fun handleSaveClick() = onContent { content ->
if (content.itemData.accountName.isBlank()) {
if (content.itemData.issuer.isBlank()) {
mutableStateFlow.update {
it.copy(
dialog = EditItemState.DialogState.Generic(
Expand Down Expand Up @@ -110,12 +110,12 @@ class EditItemViewModel @Inject constructor(
AuthenticatorItemEntity(
id = state.itemId,
key = content.itemData.totpCode.trim(),
accountName = content.itemData.accountName.trim(),
accountName = content.itemData.username?.trim(),
type = content.itemData.type,
algorithm = content.itemData.algorithm,
period = content.itemData.refreshPeriod.seconds,
digits = content.itemData.digits.length,
issuer = content.itemData.issuer?.trim(),
issuer = content.itemData.issuer.trim(),
)
)
trySendAction(EditItemAction.Internal.UpdateItemResult(result))
Expand All @@ -130,18 +130,18 @@ class EditItemViewModel @Inject constructor(
}
}

private fun handleAccountNameTextChange(action: EditItemAction.AccountNameTextChange) {
private fun handleIssuerNameTextChange(action: EditItemAction.IssuerNameTextChange) {
updateItemData { currentItemData ->
currentItemData.copy(
accountName = action.accountName
issuer = action.issuerName
)
}
}

private fun handleIssuerTextChange(action: EditItemAction.IssuerTextChange) {
private fun handleIssuerTextChange(action: EditItemAction.UsernameTextChange) {
updateItemData { currentItemData ->
currentItemData.copy(
issuer = action.issue
username = action.username
)
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ class EditItemViewModel @Inject constructor(
?: AuthenticatorRefreshPeriodOption.THIRTY,
totpCode = key,
type = type,
accountName = accountName,
username = accountName,
issuer = issuer,
algorithm = algorithm,
digits = VerificationCodeDigitsOption.fromIntOrNull(digits)
Expand Down Expand Up @@ -435,12 +435,12 @@ sealed class EditItemAction {
/**
* The user has changed the account name text.
*/
data class AccountNameTextChange(val accountName: String) : EditItemAction()
data class IssuerNameTextChange(val issuerName: String) : EditItemAction()

/**
* The user has changed the issue text.
*/
data class IssuerTextChange(val issue: String) : EditItemAction()
data class UsernameTextChange(val username: String) : EditItemAction()

/**
* The user has selected an Item Type option.
Expand Down
Loading

0 comments on commit 1f0881f

Please sign in to comment.