@@ -18,7 +18,6 @@ import kotlin.coroutines.coroutineContext
18
18
import kotlin.time.Duration.Companion.milliseconds
19
19
import kotlin.time.Duration.Companion.seconds
20
20
import kotlin.time.ExperimentalTime
21
- import kotlinx.coroutines.awaitCancellation
22
21
import kotlinx.coroutines.delay
23
22
import kotlinx.coroutines.flow.Flow
24
23
import kotlinx.coroutines.flow.flow
@@ -60,15 +59,12 @@ constructor(
60
59
* collection to check if it is valid to collect this [Flow].
61
60
*/
62
61
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)
72
68
}
73
69
74
70
/* * Obtain the [Totp.value] for this [PasswordEntry] at the current time. */
@@ -187,10 +183,10 @@ constructor(
187
183
val totpAlgorithm = totpFinder.findAlgorithm(content)
188
184
val issuer = totpFinder.findIssuer(content)
189
185
val millis = clock.millis()
190
- val remainingTime = (totpPeriod - ((millis / ONE_SECOND ) % totpPeriod)).seconds
186
+ val remainingTime = (totpPeriod - ((millis / THOUSAND_MILLIS ) % totpPeriod)).seconds
191
187
Otp .calculateCode(
192
188
totpSecret!! ,
193
- millis / (ONE_SECOND * totpPeriod),
189
+ millis / (THOUSAND_MILLIS * totpPeriod),
194
190
totpAlgorithm,
195
191
digits,
196
192
issuer
@@ -232,6 +228,6 @@ constructor(
232
228
" secret:" ,
233
229
" pass:" ,
234
230
)
235
- private const val ONE_SECOND = 1000
231
+ private const val THOUSAND_MILLIS = 1000L
236
232
}
237
233
}
0 commit comments