|
| 1 | +package com.bitwarden.authenticator.data.platform.manager.imports.parsers |
| 2 | + |
| 3 | +import android.net.Uri |
| 4 | +import com.bitwarden.authenticator.data.authenticator.datasource.disk.entity.AuthenticatorItemAlgorithm |
| 5 | +import com.bitwarden.authenticator.data.authenticator.datasource.disk.entity.AuthenticatorItemEntity |
| 6 | +import com.bitwarden.authenticator.data.authenticator.datasource.disk.entity.AuthenticatorItemType |
| 7 | +import com.bitwarden.authenticator.data.authenticator.manager.TotpCodeManager |
| 8 | +import com.bitwarden.authenticator.data.authenticator.manager.model.ExportJsonData |
| 9 | +import com.bitwarden.authenticator.data.platform.manager.imports.model.ImportFileFormat |
| 10 | +import com.bitwarden.authenticator.data.platform.util.asFailure |
| 11 | +import com.bitwarden.authenticator.data.platform.util.asSuccess |
| 12 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 13 | +import kotlinx.serialization.json.Json |
| 14 | +import kotlinx.serialization.json.decodeFromStream |
| 15 | +import java.io.ByteArrayInputStream |
| 16 | + |
| 17 | +class BitwardenExportParser( |
| 18 | + private val fileFormat: ImportFileFormat, |
| 19 | +) : ExportParser { |
| 20 | + override fun parse(byteArray: ByteArray): Result<List<AuthenticatorItemEntity>> { |
| 21 | + return when (fileFormat) { |
| 22 | + ImportFileFormat.BITWARDEN_JSON -> importJsonFile(byteArray) |
| 23 | + else -> IllegalArgumentException("Unsupported file format.").asFailure() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @OptIn(ExperimentalSerializationApi::class) |
| 28 | + private fun importJsonFile(byteArray: ByteArray): Result<List<AuthenticatorItemEntity>> { |
| 29 | + val importJson = Json { |
| 30 | + ignoreUnknownKeys = true |
| 31 | + isLenient = true |
| 32 | + explicitNulls = false |
| 33 | + } |
| 34 | + |
| 35 | + return importJson |
| 36 | + .decodeFromStream<ExportJsonData>(ByteArrayInputStream(byteArray)) |
| 37 | + .asSuccess() |
| 38 | + .map { exportData -> |
| 39 | + exportData |
| 40 | + .items |
| 41 | + .toAuthenticatorItemEntities() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private fun List<ExportJsonData.ExportItem>.toAuthenticatorItemEntities() = |
| 46 | + map { it.toAuthenticatorItemEntity() } |
| 47 | + |
| 48 | + private fun ExportJsonData.ExportItem.toAuthenticatorItemEntity(): AuthenticatorItemEntity { |
| 49 | + val otpString = login.totp |
| 50 | + val otpUri = Uri.parse(otpString) |
| 51 | + val type = if (otpString.startsWith(TotpCodeManager.TOTP_CODE_PREFIX)) { |
| 52 | + AuthenticatorItemType.TOTP |
| 53 | + } else if (otpString.startsWith(TotpCodeManager.STEAM_CODE_PREFIX)) { |
| 54 | + AuthenticatorItemType.STEAM |
| 55 | + } else { |
| 56 | + throw IllegalArgumentException("Unsupported OTP type.") |
| 57 | + } |
| 58 | + |
| 59 | + val key = when (type) { |
| 60 | + AuthenticatorItemType.TOTP -> { |
| 61 | + requireNotNull(otpUri.getQueryParameter(TotpCodeManager.SECRET_PARAM)) |
| 62 | + } |
| 63 | + |
| 64 | + AuthenticatorItemType.STEAM -> { |
| 65 | + requireNotNull(otpUri.authority) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + val algorithm = otpUri.getQueryParameter(TotpCodeManager.ALGORITHM_PARAM) |
| 70 | + ?: TotpCodeManager.ALGORITHM_DEFAULT.name |
| 71 | + |
| 72 | + val period = otpUri.getQueryParameter(TotpCodeManager.PERIOD_PARAM) |
| 73 | + ?.toIntOrNull() |
| 74 | + ?: TotpCodeManager.PERIOD_SECONDS_DEFAULT |
| 75 | + |
| 76 | + val digits = when (type) { |
| 77 | + AuthenticatorItemType.TOTP -> { |
| 78 | + otpUri.getQueryParameter(TotpCodeManager.DIGITS_PARAM) |
| 79 | + ?.toIntOrNull() |
| 80 | + ?: TotpCodeManager.TOTP_DIGITS_DEFAULT |
| 81 | + } |
| 82 | + |
| 83 | + AuthenticatorItemType.STEAM -> { |
| 84 | + TotpCodeManager.STEAM_DIGITS_DEFAULT |
| 85 | + } |
| 86 | + } |
| 87 | + val issuer = otpUri.getQueryParameter(TotpCodeManager.ISSUER_PARAM) |
| 88 | + ?: name |
| 89 | + |
| 90 | + val label = when (type) { |
| 91 | + AuthenticatorItemType.TOTP -> { |
| 92 | + otpUri.pathSegments |
| 93 | + .firstOrNull() |
| 94 | + .orEmpty() |
| 95 | + .removePrefix("$issuer:") |
| 96 | + } |
| 97 | + |
| 98 | + AuthenticatorItemType.STEAM -> null |
| 99 | + } |
| 100 | + |
| 101 | + return AuthenticatorItemEntity( |
| 102 | + id = id, |
| 103 | + key = key, |
| 104 | + type = type, |
| 105 | + algorithm = algorithm.let { AuthenticatorItemAlgorithm.valueOf(it) }, |
| 106 | + period = period, |
| 107 | + digits = digits, |
| 108 | + issuer = issuer, |
| 109 | + accountName = label, |
| 110 | + favorite = favorite, |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
0 commit comments