summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/totp/UriTotpFinder.kt31
-rw-r--r--app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt6
2 files changed, 20 insertions, 17 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/totp/UriTotpFinder.kt b/app/src/main/java/dev/msfjarvis/aps/util/totp/UriTotpFinder.kt
index fa8481a9..a420fe5d 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/totp/UriTotpFinder.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/totp/UriTotpFinder.kt
@@ -24,32 +24,29 @@ class UriTotpFinder @Inject constructor() : TotpFinder {
}
override fun findDigits(content: String): String {
- content.split("\n".toRegex()).forEach { line ->
- if (line.startsWith(TOTP_FIELDS[0]) && Uri.parse(line).getQueryParameter("digits") != null) {
- return Uri.parse(line).getQueryParameter("digits")!!
- }
- }
- return "6"
+ return getQueryParameter(content, "digits") ?: "6"
}
override fun findPeriod(content: String): Long {
- content.split("\n".toRegex()).forEach { line ->
- if (line.startsWith(TOTP_FIELDS[0]) && Uri.parse(line).getQueryParameter("period") != null) {
- val period = Uri.parse(line).getQueryParameter("period")!!.toLongOrNull()
- if (period != null && period > 0) return period
- }
- }
- return 30
+ return getQueryParameter(content, "period")?.toLongOrNull() ?: 30
}
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 ->
- if (line.startsWith(TOTP_FIELDS[0]) && Uri.parse(line).getQueryParameter("algorithm") != null
- ) {
- return Uri.parse(line).getQueryParameter("algorithm")!!
+ val uri = Uri.parse(line)
+ if (line.startsWith(TOTP_FIELDS[0]) && uri.getQueryParameter(parameterName) != null) {
+ return uri.getQueryParameter(parameterName)
}
}
- return "sha1"
+ return null
}
companion object {
diff --git a/app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt b/app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt
index f04913a1..37cbef2f 100644
--- a/app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt
+++ b/app/src/test/java/dev/msfjarvis/aps/util/totp/UriTotpFinderTest.kt
@@ -45,6 +45,12 @@ class UriTotpFinderTest {
assertEquals("SHA256", totpFinder.findAlgorithm(PASS_FILE_CONTENT))
}
+ @Test
+ fun findIssuer() {
+ assertEquals("ACME Co", totpFinder.findIssuer(TOTP_URI))
+ assertEquals("ACME Co", totpFinder.findIssuer(PASS_FILE_CONTENT))
+ }
+
companion object {
const val TOTP_URI =