summaryrefslogtreecommitdiff
path: root/format-common-impl/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-10-21 21:36:27 +0530
committerGitHub <noreply@github.com>2022-10-21 21:36:27 +0530
commitcdf0f30c61e55fc94524c2fd3f07ffda367555f1 (patch)
treef1d4a029c78105560947def5a3d5c423f4b096d0 /format-common-impl/src/main
parentdf764932f7fdddea9cea5937c6053a95797d35df (diff)
Refactor `format-common` module (#2196)
* fix: touch up `PasswordEntryTest` KDoc * feat: add format-common-impl module * refactor: switch app to format-common-impl * refactor: move `format-common` tests to `format-common-impl` * feat: add a test for Steam OTP
Diffstat (limited to 'format-common-impl/src/main')
-rw-r--r--format-common-impl/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt52
1 files changed, 52 insertions, 0 deletions
diff --git a/format-common-impl/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt b/format-common-impl/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt
new file mode 100644
index 00000000..741a21a7
--- /dev/null
+++ b/format-common-impl/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt
@@ -0,0 +1,52 @@
+package app.passwordstore.util.totp
+
+import android.net.Uri
+import javax.inject.Inject
+
+/** [Uri] backed TOTP URL parser. */
+public class UriTotpFinder @Inject constructor() : TotpFinder {
+
+ private companion object {
+ private const val DEFAULT_TOTP_PERIOD = 30L
+ }
+
+ override fun findSecret(content: String): String? {
+ content.split("\n".toRegex()).forEach { line ->
+ if (line.startsWith(TotpFinder.TOTP_FIELDS[0])) {
+ return Uri.parse(line).getQueryParameter("secret")
+ }
+ if (line.startsWith(TotpFinder.TOTP_FIELDS[1], ignoreCase = true)) {
+ return line.split(": *".toRegex(), 2).toTypedArray()[1]
+ }
+ }
+ return null
+ }
+
+ override fun findDigits(content: String): String {
+ return getQueryParameter(content, "digits") ?: "6"
+ }
+
+ override fun findPeriod(content: String): Long {
+ return getQueryParameter(content, "period")?.toLongOrNull() ?: DEFAULT_TOTP_PERIOD
+ }
+
+ override fun findAlgorithm(content: String): String {
+ return getQueryParameter(content, "algorithm") ?: "sha1"
+ }
+
+ override fun findIssuer(content: String): String? {
+ return getQueryParameter(content, "issuer") ?: Uri.parse(content).authority
+ }
+
+ private fun getQueryParameter(content: String, parameterName: String): String? {
+ content.split("\n".toRegex()).forEach { line ->
+ val uri = Uri.parse(line)
+ if (
+ line.startsWith(TotpFinder.TOTP_FIELDS[0]) && uri.getQueryParameter(parameterName) != null
+ ) {
+ return uri.getQueryParameter(parameterName)
+ }
+ }
+ return null
+ }
+}