diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2023-08-10 03:21:47 +0530 |
|---|---|---|
| committer | Harsh Shandilya <me@msfjarvis.dev> | 2023-08-10 03:31:08 +0530 |
| commit | 959a56d7ffbc20db60721d7a09f399c8bdefe07e (patch) | |
| tree | 0d9b14e0bb770fd6da2e04295f2c22c087142439 /format-common/src/main/kotlin/app/passwordstore/util | |
| parent | efef72b6327c8e683c8844146e23d12104f12dd1 (diff) | |
| download | HardenedPass-959a56d7ffbc20db60721d7a09f399c8bdefe07e.tar.gz HardenedPass-959a56d7ffbc20db60721d7a09f399c8bdefe07e.zip | |
refactor: un-flatten module structure
Diffstat (limited to 'format-common/src/main/kotlin/app/passwordstore/util')
4 files changed, 0 insertions, 197 deletions
diff --git a/format-common/src/main/kotlin/app/passwordstore/util/time/UserClock.kt b/format-common/src/main/kotlin/app/passwordstore/util/time/UserClock.kt deleted file mode 100644 index 4ffeb6a6..00000000 --- a/format-common/src/main/kotlin/app/passwordstore/util/time/UserClock.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package app.passwordstore.util.time - -import java.time.Clock -import java.time.Instant -import java.time.ZoneId -import javax.inject.Inject - -/** - * A subclass of [Clock] that uses [Clock.systemDefaultZone] to get a clock that works for the - * user's current time zone. - */ -public open class UserClock @Inject constructor() : Clock() { - - private val clock = systemDefaultZone() - - override fun withZone(zone: ZoneId): Clock = clock.withZone(zone) - - override fun getZone(): ZoneId = clock.zone - - override fun instant(): Instant = clock.instant() -} diff --git a/format-common/src/main/kotlin/app/passwordstore/util/totp/Otp.kt b/format-common/src/main/kotlin/app/passwordstore/util/totp/Otp.kt deleted file mode 100644 index 10e771fe..00000000 --- a/format-common/src/main/kotlin/app/passwordstore/util/totp/Otp.kt +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package app.passwordstore.util.totp - -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Result -import com.github.michaelbull.result.runCatching -import java.nio.ByteBuffer -import java.util.Locale -import javax.crypto.Mac -import javax.crypto.spec.SecretKeySpec -import kotlin.experimental.and -import org.apache.commons.codec.binary.Base32 - -public object Otp { - - private val BASE_32 = Base32() - private val STEAM_ALPHABET = "23456789BCDFGHJKMNPQRTVWXY".toCharArray() - private const val BYTE_BUFFER_CAPACITY = 8 - private const val END_INDEX_OFFSET = 4 - private const val STEAM_GUARD_DIGITS = 5 - private const val MINIMUM_DIGITS = 6 - private const val MAXIMUM_DIGITS = 10 - private const val ALPHABET_LENGTH = 26 - private const val MOST_SIGNIFICANT_BYTE = 0x7f - - public fun calculateCode( - secret: String, - counter: Long, - algorithm: String, - digits: String, - issuer: String?, - ): Result<String, Throwable> = runCatching { - val algo = "Hmac${algorithm.uppercase(Locale.ROOT)}" - val decodedSecret = BASE_32.decode(secret) - val secretKey = SecretKeySpec(decodedSecret, algo) - val digest = - Mac.getInstance(algo).run { - init(secretKey) - doFinal(ByteBuffer.allocate(BYTE_BUFFER_CAPACITY).putLong(counter).array()) - } - // Least significant 4 bits are used as an offset into the digest. - val offset = (digest.last() and 0xf).toInt() - // Extract 32 bits at the offset and clear the most significant bit. - val code = digest.copyOfRange(offset, offset.plus(END_INDEX_OFFSET)) - code[0] = (MOST_SIGNIFICANT_BYTE and code[0].toInt()).toByte() - val codeInt = ByteBuffer.wrap(code).int - check(codeInt > 0) - // SteamGuard is a horrible OTP implementation that generates non-standard 5 digit OTPs as - // well - // as uses a custom character set. - if (digits == "s" || issuer == "Steam") { - var remainingCodeInt = codeInt - buildString { - repeat(STEAM_GUARD_DIGITS) { - append(STEAM_ALPHABET[remainingCodeInt % STEAM_ALPHABET.size]) - remainingCodeInt /= ALPHABET_LENGTH - } - } - } else { - // Base 10, 6 to 10 digits - val numDigits = digits.toIntOrNull() - when { - numDigits == null -> { - return Err(IllegalArgumentException("Digits specifier has to be either 's' or numeric")) - } - numDigits < MINIMUM_DIGITS -> { - return Err( - IllegalArgumentException("TOTP codes have to be at least $MINIMUM_DIGITS digits long") - ) - } - numDigits > MAXIMUM_DIGITS -> { - return Err( - IllegalArgumentException("TOTP codes can be at most $MAXIMUM_DIGITS digits long") - ) - } - else -> { - // 2^31 = 2_147_483_648, so we can extract at most 10 digits with the first one - // always being 0, 1, or 2. Pad with leading zeroes. - val codeStringBase10 = codeInt.toString(MAXIMUM_DIGITS).padStart(MAXIMUM_DIGITS, '0') - check(codeStringBase10.length == MAXIMUM_DIGITS) - codeStringBase10.takeLast(numDigits) - } - } - } - } -} diff --git a/format-common/src/main/kotlin/app/passwordstore/util/totp/TotpFinder.kt b/format-common/src/main/kotlin/app/passwordstore/util/totp/TotpFinder.kt deleted file mode 100644 index d53d76dc..00000000 --- a/format-common/src/main/kotlin/app/passwordstore/util/totp/TotpFinder.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package app.passwordstore.util.totp - -/** Defines a class that can extract relevant parts of a TOTP URL for use by the app. */ -public interface TotpFinder { - - /** Get the TOTP secret from the given extra content. */ - public fun findSecret(content: String): String? - - /** Get the number of digits required in the final OTP. */ - public fun findDigits(content: String): String - - /** Get the TOTP timeout period. */ - public fun findPeriod(content: String): Long - - /** Get the algorithm for the TOTP secret. */ - public fun findAlgorithm(content: String): String - - /** Get the issuer for the TOTP secret, if any. */ - public fun findIssuer(content: String): String? - - public companion object { - public val TOTP_FIELDS: Array<String> = arrayOf("otpauth://totp", "totp:") - } -} diff --git a/format-common/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt b/format-common/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt deleted file mode 100644 index bb97c90c..00000000 --- a/format-common/src/main/kotlin/app/passwordstore/util/totp/UriTotpFinder.kt +++ /dev/null @@ -1,52 +0,0 @@ -package app.passwordstore.util.totp - -import com.eygraber.uri.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 - } -} |
