@@ -18,7 +18,6 @@ import kotlin.coroutines.coroutineContext
1818import kotlin.time.Duration.Companion.milliseconds
1919import kotlin.time.Duration.Companion.seconds
2020import kotlin.time.ExperimentalTime
21- import kotlinx.coroutines.awaitCancellation
2221import kotlinx.coroutines.delay
2322import kotlinx.coroutines.flow.Flow
2423import 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