summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorJoris Minjat <joris@minjat.eu>2020-10-23 18:13:37 +0200
committerGitHub <noreply@github.com>2020-10-23 21:43:37 +0530
commit38ece10e0dfdc907089b7ddc4e6c9d17e69013f3 (patch)
tree0d4e0b22ca945d66080b1ecb51e9dc155b9d3ffa /app/src/main/java
parent66b31f1432a1405143323f92ca7e33d6c98ee0a2 (diff)
Minimal support for MIME secret encoding (#1170)
Co-authored-by: Fabian Henneke <FabianHenneke@users.noreply.github.com>
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt26
1 files changed, 19 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:",
+ )
}
}