refactor(format-common): obviate the constant value in PasswordEntry

This commit is contained in:
Harsh Shandilya 2022-12-19 15:20:00 +05:30
parent 93d51f0412
commit b84f7ccd07
No known key found for this signature in database

View file

@ -18,7 +18,6 @@ import kotlin.coroutines.coroutineContext
import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime import kotlin.time.ExperimentalTime
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
@ -60,15 +59,12 @@ constructor(
* collection to check if it is valid to collect this [Flow]. * collection to check if it is valid to collect this [Flow].
*/ */
public val totp: Flow<Totp> = flow { public val totp: Flow<Totp> = flow {
if (totpSecret != null) { require(totpSecret != null) { "Cannot collect this flow without a TOTP secret" }
do { do {
val otp = calculateTotp() val otp = calculateTotp()
emit(otp) emit(otp)
delay(ONE_SECOND.milliseconds) delay(THOUSAND_MILLIS.milliseconds)
} while (coroutineContext.isActive) } while (coroutineContext.isActive)
} else {
awaitCancellation()
}
} }
/** Obtain the [Totp.value] for this [PasswordEntry] at the current time. */ /** Obtain the [Totp.value] for this [PasswordEntry] at the current time. */
@ -187,10 +183,10 @@ constructor(
val totpAlgorithm = totpFinder.findAlgorithm(content) val totpAlgorithm = totpFinder.findAlgorithm(content)
val issuer = totpFinder.findIssuer(content) val issuer = totpFinder.findIssuer(content)
val millis = clock.millis() val millis = clock.millis()
val remainingTime = (totpPeriod - ((millis / ONE_SECOND) % totpPeriod)).seconds val remainingTime = (totpPeriod - ((millis / THOUSAND_MILLIS) % totpPeriod)).seconds
Otp.calculateCode( Otp.calculateCode(
totpSecret!!, totpSecret!!,
millis / (ONE_SECOND * totpPeriod), millis / (THOUSAND_MILLIS * totpPeriod),
totpAlgorithm, totpAlgorithm,
digits, digits,
issuer issuer
@ -232,6 +228,6 @@ constructor(
"secret:", "secret:",
"pass:", "pass:",
) )
private const val ONE_SECOND = 1000 private const val THOUSAND_MILLIS = 1000L
} }
} }