diff options
author | Joris Minjat <joris@minjat.eu> | 2020-10-23 18:13:37 +0200 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-10-23 21:51:26 +0530 |
commit | 53b9a807a19d62f11190f5fbe3405a870e16874d (patch) | |
tree | cce3c484e45c01439d874ca2ddb2fc4654527b7d | |
parent | 2b25171bf28fdc18f9154e24d87d8cedb90815d8 (diff) |
Minimal support for MIME secret encoding (#1170)
Co-authored-by: Fabian Henneke <FabianHenneke@users.noreply.github.com>
(cherry picked from commit 38ece10e0dfdc907089b7ddc4e6c9d17e69013f3)
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt | 26 | ||||
-rw-r--r-- | app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt | 9 |
2 files changed, 28 insertions, 7 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt b/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt index 556b2c87..a143fcb9 100644 --- a/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt +++ b/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt @@ -31,9 +31,9 @@ class PasswordEntry(content: String, private val totpFinder: TotpFinder = UriTot constructor(os: ByteArrayOutputStream) : this(os.toString("UTF-8"), UriTotpFinder()) init { - val passContent = content.split("\n".toRegex(), 2).toTypedArray() - password = if (UriTotpFinder.TOTP_FIELDS.any { passContent[0].startsWith(it) }) "" else passContent[0] - extraContent = findExtraContent(passContent) + val (foundPassword, passContent) = findAndStripPassword(content.split("\n".toRegex())) + password = foundPassword + extraContent = passContent.joinToString("\n") username = findUsername() digits = findOtpDigits(content) totpSecret = findTotpSecret(content) @@ -86,10 +86,16 @@ class PasswordEntry(content: String, private val totpFinder: TotpFinder = UriTot return null } - private fun findExtraContent(passContent: Array<String>) = when { - password.isEmpty() && passContent[0].isNotEmpty() -> passContent[0] - passContent.size > 1 -> passContent[1] - else -> "" + private fun findAndStripPassword(passContent: List<String>): Pair<String, List<String>> { + if (UriTotpFinder.TOTP_FIELDS.any { passContent[0].startsWith(it) }) return Pair("", passContent) + for (line in passContent) { + for (prefix in PASSWORD_FIELDS) { + if (line.startsWith(prefix, ignoreCase = true)) { + return Pair(line.substring(prefix.length).trimStart(), passContent.minus(line)) + } + } + } + return Pair(passContent[0], passContent.minus(passContent[0])) } private fun findTotpSecret(decryptedContent: String): String? { @@ -121,5 +127,11 @@ class PasswordEntry(content: String, private val totpFinder: TotpFinder = UriTot "id:", "identity:" ) + + val PASSWORD_FIELDS = arrayOf( + "password:", + "secret:", + "pass:", + ) } } diff --git a/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt b/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt index 6eecfe45..04c606e8 100644 --- a/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt +++ b/app/src/test/java/com/zeapo/pwdstore/model/PasswordEntryTest.kt @@ -28,6 +28,12 @@ class PasswordEntryTest { assertEquals("", makeEntry("\nblubb").password) assertEquals("", makeEntry("\n").password) assertEquals("", makeEntry("").password) + for (field in PasswordEntry.PASSWORD_FIELDS) { + assertEquals("fooooo", makeEntry("\n$field fooooo").password) + assertEquals("fooooo", makeEntry("\n${field.toUpperCase()} fooooo").password) + assertEquals("fooooo", makeEntry("GOPASS-SECRET-1.0\n$field fooooo").password) + assertEquals("fooooo", makeEntry("someFirstLine\nUsername: bar\n$field fooooo").password) + } } @Test fun testGetExtraContent() { @@ -37,6 +43,9 @@ class PasswordEntryTest { assertEquals("", makeEntry("fooooo").extraContent) assertEquals("blubb\n", makeEntry("\nblubb\n").extraContent) assertEquals("blubb", makeEntry("\nblubb").extraContent) + assertEquals("blubb", makeEntry("blubb\npassword: foo").extraContent) + assertEquals("blubb", makeEntry("password: foo\nblubb").extraContent) + assertEquals("blubb\nusername: bar", makeEntry("blubb\npassword: foo\nusername: bar").extraContent) assertEquals("", makeEntry("\n").extraContent) assertEquals("", makeEntry("").extraContent) } |