Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit b84f7cc

Browse files
committed
refactor(format-common): obviate the constant value in PasswordEntry
1 parent 93d51f0 commit b84f7cc

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

format-common/src/main/kotlin/app/passwordstore/data/passfile/PasswordEntry.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import kotlin.coroutines.coroutineContext
1818
import kotlin.time.Duration.Companion.milliseconds
1919
import kotlin.time.Duration.Companion.seconds
2020
import kotlin.time.ExperimentalTime
21-
import kotlinx.coroutines.awaitCancellation
2221
import kotlinx.coroutines.delay
2322
import kotlinx.coroutines.flow.Flow
2423
import kotlinx.coroutines.flow.flow
@@ -60,15 +59,12 @@ constructor(
6059
* collection to check if it is valid to collect this [Flow].
6160
*/
6261
public val totp: Flow<Totp> = flow {
63-
if (totpSecret != null) {
64-
do {
65-
val otp = calculateTotp()
66-
emit(otp)
67-
delay(ONE_SECOND.milliseconds)
68-
} while (coroutineContext.isActive)
69-
} else {
70-
awaitCancellation()
71-
}
62+
require(totpSecret != null) { "Cannot collect this flow without a TOTP secret" }
63+
do {
64+
val otp = calculateTotp()
65+
emit(otp)
66+
delay(THOUSAND_MILLIS.milliseconds)
67+
} while (coroutineContext.isActive)
7268
}
7369

7470
/** Obtain the [Totp.value] for this [PasswordEntry] at the current time. */
@@ -187,10 +183,10 @@ constructor(
187183
val totpAlgorithm = totpFinder.findAlgorithm(content)
188184
val issuer = totpFinder.findIssuer(content)
189185
val millis = clock.millis()
190-
val remainingTime = (totpPeriod - ((millis / ONE_SECOND) % totpPeriod)).seconds
186+
val remainingTime = (totpPeriod - ((millis / THOUSAND_MILLIS) % totpPeriod)).seconds
191187
Otp.calculateCode(
192188
totpSecret!!,
193-
millis / (ONE_SECOND * totpPeriod),
189+
millis / (THOUSAND_MILLIS * totpPeriod),
194190
totpAlgorithm,
195191
digits,
196192
issuer
@@ -232,6 +228,6 @@ constructor(
232228
"secret:",
233229
"pass:",
234230
)
235-
private const val ONE_SECOND = 1000
231+
private const val THOUSAND_MILLIS = 1000L
236232
}
237233
}

0 commit comments

Comments
 (0)