One wrong-PIN limit for every PIN entry. Ported from release-0.108 (0.108.105)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -60,11 +60,24 @@ internal object AfterDarkLock {
|
||||
fun setNightly(context: Context, on: Boolean) { nightly = on; prefs(context).edit().putBoolean("nightly_lock", on).apply() }
|
||||
private fun prefs(context: Context) = context.getSharedPreferences("orbit_lock", Context.MODE_PRIVATE)
|
||||
|
||||
fun check(context: Context, pin: String): Boolean {
|
||||
/**
|
||||
* Every PIN check, wherever it's entered (the TV, a phone, a room asking to join), goes through here and its wrong-PIN
|
||||
* limit (8 November 2026, audit S3): after [PIN_FREE_MISSES] wrong PINs a wait, doubling with each further miss. During
|
||||
* the wait no PIN is checked, not even the right one. The count survives restarts; the PIN itself is never stored.
|
||||
*/
|
||||
fun check(context: Context, pin: String, nowMs: Long = System.currentTimeMillis()): Boolean {
|
||||
val p = prefs(context)
|
||||
val before = PinLimit(p.getInt("pin_misses", 0), p.getLong("pin_wait_until", 0L))
|
||||
if (before.waiting(nowMs)) return false
|
||||
val salt = p.getString("after_dark_salt", null) ?: return false
|
||||
return p.getString("after_dark_pin", null) == pinHash(salt, pin)
|
||||
val right = p.getString("after_dark_pin", null) == pinHash(salt, pin)
|
||||
val after = before.after(right, nowMs)
|
||||
if (after != before) p.edit().putInt("pin_misses", after.misses).putLong("pin_wait_until", after.waitUntilMs).apply()
|
||||
return right
|
||||
}
|
||||
/** What to say after a refused PIN: how long to wait, when there's a wait. */
|
||||
fun refusal(context: Context, wrong: String = "That's not the PIN", nowMs: Long = System.currentTimeMillis()): String =
|
||||
prefs(context).let { p -> PinLimit(p.getInt("pin_misses", 0), p.getLong("pin_wait_until", 0L)) }.waitText(nowMs) ?: wrong
|
||||
fun set(context: Context, pin: String) {
|
||||
val salt = java.util.UUID.randomUUID().toString()
|
||||
prefs(context).edit().putString("after_dark_salt", salt).putString("after_dark_pin", pinHash(salt, pin)).putLong("pin_changed_at", System.currentTimeMillis()).apply()
|
||||
@@ -86,6 +99,26 @@ internal object AfterDarkLock {
|
||||
fun lock() { unlockedState = false; unlockedAt = 0L; store?.edit()?.remove("unlocked_at")?.apply() }
|
||||
}
|
||||
|
||||
/** Wrong PINs allowed before the first wait. */
|
||||
internal const val PIN_FREE_MISSES = 5
|
||||
internal const val PIN_FIRST_WAIT_MS = 60_000L
|
||||
internal const val PIN_LONGEST_WAIT_MS = 30 * 60_000L
|
||||
|
||||
/** The wrong-PIN count and any wait it has caused. Pure, so it can be tested without Android. */
|
||||
internal data class PinLimit(val misses: Int = 0, val waitUntilMs: Long = 0L) {
|
||||
fun waiting(nowMs: Long) = nowMs < waitUntilMs
|
||||
/** After a PIN checked outside a wait: the right one clears it; the fifth wrong one and each after it sets a longer wait. */
|
||||
fun after(right: Boolean, nowMs: Long): PinLimit {
|
||||
if (right) return PinLimit()
|
||||
val n = misses + 1
|
||||
if (n < PIN_FREE_MISSES) return PinLimit(n, 0L)
|
||||
val wait = (PIN_FIRST_WAIT_MS shl (n - PIN_FREE_MISSES).coerceAtMost(10)).coerceAtMost(PIN_LONGEST_WAIT_MS)
|
||||
return PinLimit(n, nowMs + wait)
|
||||
}
|
||||
fun waitText(nowMs: Long): String? = if (!waiting(nowMs)) null else
|
||||
((waitUntilMs - nowMs + 59_999) / 60_000).let { m -> "Too many tries. Try again in $m minute${if (m == 1L) "" else "s"}." }
|
||||
}
|
||||
|
||||
/** The hour the optional nightly lock falls, 3am. */
|
||||
internal const val NIGHTLY_LOCK_HOUR = 3
|
||||
|
||||
@@ -181,10 +214,10 @@ internal fun LockDetail(firstSwitch: FocusRequester, modifier: Modifier) {
|
||||
}
|
||||
}
|
||||
LockStep.Unlock -> PinEntry("Enter the PIN", problem, onDone = { pin ->
|
||||
if (AfterDarkLock.unlock(context, pin)) { step = LockStep.None; note = if (AfterDarkLock.nightly) "Unlocked until 3am." else "Unlocked until you lock it." } else problem = "That's not the PIN"
|
||||
if (AfterDarkLock.unlock(context, pin)) { step = LockStep.None; note = if (AfterDarkLock.nightly) "Unlocked until 3am." else "Unlocked until you lock it." } else problem = AfterDarkLock.refusal(context)
|
||||
}, onCancel = { step = LockStep.None })
|
||||
LockStep.CheckForChange, LockStep.CheckForOff -> PinEntry("Enter the current PIN", problem, onDone = { pin ->
|
||||
if (!AfterDarkLock.check(context, pin)) problem = "That's not the PIN"
|
||||
if (!AfterDarkLock.check(context, pin)) problem = AfterDarkLock.refusal(context)
|
||||
else if (step == LockStep.CheckForOff) { AfterDarkLock.clear(context); step = LockStep.None; note = "The lock is off." }
|
||||
else begin(LockStep.NewPin)
|
||||
}, onCancel = { step = LockStep.None })
|
||||
|
||||
@@ -237,7 +237,7 @@ internal object OrbitCompanion {
|
||||
if (pin.isNullOrEmpty()) return "PIN"
|
||||
if (context == null || !AfterDarkLock.unlock(context, pin)) {
|
||||
if (++pinMisses >= 3) { pinMisses = 0; pinWaitUntil = nowMs + 5 * 60_000L }
|
||||
return "That's not the PIN."
|
||||
return context?.let { AfterDarkLock.refusal(it, "That's not the PIN.") } ?: "That's not the PIN."
|
||||
}
|
||||
pinMisses = 0
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ internal fun WhosWatching(onClose: () -> Unit) {
|
||||
ProfileAvatar(p.picture, 90.dp)
|
||||
Text(p.name, color = Ink, fontSize = 20.sp, fontWeight = FontWeight.SemiBold)
|
||||
PinEntry("Enter the PIN", problem, onDone = { pin ->
|
||||
if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p } else problem = "That's not the PIN"
|
||||
if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p } else problem = AfterDarkLock.refusal(context)
|
||||
}, onCancel = { asking = null })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ internal fun RoomKnockOverlay() {
|
||||
BackHandler { if (asking) asking = false else { offer.refuse(); PhoneLink.offerRoom(null) } }
|
||||
OverlayCard(Navy.copy(alpha = .55f)) {
|
||||
if (asking) { PinEntry("Enter the Adult PIN to allow it", problem, onDone = { pin ->
|
||||
if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = "That PIN didn't match."
|
||||
if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = AfterDarkLock.refusal(context, "That PIN didn't match.")
|
||||
}, onCancel = { asking = false }); return@OverlayCard }
|
||||
val first = remember { FocusRequester() }
|
||||
Column(Modifier.width(560.dp).background(Panel, RoundedCornerShape(14.dp)).padding(22.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
@@ -200,7 +200,7 @@ internal fun AddRoomPanel(onClose: () -> Unit, modifier: Modifier = Modifier) {
|
||||
}
|
||||
BackHandler { onClose() }
|
||||
if (asking) { PinEntry("Enter the Adult PIN to allow it", problem, onDone = { pin ->
|
||||
if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = "That PIN didn't match."
|
||||
if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = AfterDarkLock.refusal(context, "That PIN didn't match.")
|
||||
}, onCancel = { asking = false }); return }
|
||||
val first = remember { FocusRequester() }
|
||||
Column(modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
/** 8 November 2026 (audit S3): one wrong-PIN limit for every place a PIN is entered. */
|
||||
class PinLimitTest {
|
||||
private val now = 1_794_000_000_000L
|
||||
private val minute = 60_000L
|
||||
private fun misses(n: Int, at: Long = now) = (1..n).fold(PinLimit()) { l, _ -> l.after(false, at) }
|
||||
|
||||
@Test fun fourWrongPinsCostNothing() {
|
||||
val l = misses(4)
|
||||
assertEquals(4, l.misses); assertFalse(l.waiting(now))
|
||||
}
|
||||
@Test fun theFifthWrongPinStartsAOneMinuteWait() {
|
||||
val l = misses(5)
|
||||
assertTrue(l.waiting(now)); assertTrue(l.waiting(now + minute - 1)); assertFalse(l.waiting(now + minute))
|
||||
assertEquals("Too many tries. Try again in 1 minute.", l.waitText(now))
|
||||
}
|
||||
@Test fun eachFurtherMissDoublesTheWaitUpToHalfAnHour() {
|
||||
assertEquals(now + 2 * minute, misses(6).waitUntilMs)
|
||||
assertEquals(now + 4 * minute, misses(7).waitUntilMs)
|
||||
assertEquals(now + 16 * minute, misses(9).waitUntilMs)
|
||||
assertEquals(now + 30 * minute, misses(10).waitUntilMs)
|
||||
assertEquals(now + 30 * minute, misses(60).waitUntilMs)
|
||||
assertEquals("Too many tries. Try again in 30 minutes.", misses(10).waitText(now))
|
||||
}
|
||||
@Test fun theRightPinAfterTheWaitClearsEverything() {
|
||||
val cleared = misses(7).after(true, now + 10 * minute)
|
||||
assertEquals(PinLimit(), cleared); assertNull(cleared.waitText(now))
|
||||
}
|
||||
@Test fun everyPinCheckGoesThroughTheLimit() {
|
||||
// Only AfterDarkLock.check compares against the stored PIN; every screen and the phone server call it.
|
||||
val sources = File("src/main/java/tv/orbit/classic").listFiles()!!.filter { it.extension == "kt" }
|
||||
val comparing = sources.filter { f -> f.readText().contains("== pinHash(") }
|
||||
assertEquals(listOf("AfterDarkLock.kt"), comparing.map { it.name })
|
||||
val reading = sources.filter { f -> f.name != "AfterDarkLock.kt" && f.name != "Rooms.kt" && f.readText().contains("\"after_dark_pin\"") }
|
||||
assertTrue(reading.map { it.name }.toString(), reading.isEmpty())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user