INTEGRATION.md

8.8 KB · raw

rendered · source

1# E1 — zxcvbn Strength Meter — Integration23Self-contained, dependency-free strength scorer + reusable 4-segment bar.4**No gradle dependency added.** Pure-Kotlin heuristic (length + character-class5entropy estimate − sequence/repeat/common-password penalties), documented inline6in `PasswordStrength.kt`.78## Files to drop in910| Draft | Destination |11|-------|-------------|12| `PasswordStrength.kt` | `app/src/main/java/com/peachpasswords/ui/security/PasswordStrength.kt` |13| `StrengthBar.kt`      | `app/src/main/java/com/peachpasswords/ui/security/StrengthBar.kt` |14| `PasswordStrengthTest.kt` | `app/src/test/java/com/peachpasswords/ui/security/PasswordStrengthTest.kt` |1516New package: `com.peachpasswords.ui.security`. Public API:1718```kotlin19fun scorePassword(pw: String): Int          // 0..420fun strengthLabel(score: Int): String       // "very weak".."strong"21@Composable fun StrengthBar(score: Int, modifier: Modifier = Modifier)22@Composable fun strengthColor(score: Int): Color   // 0 breach,1-2 amber,3 health,4 accent23```2425No ViewModel, repository, or nav changes required.2627## Behaviour notes / risks2829- **Stricter than the old Export scorer.** The previous `scorePassword` in30  ExportScreen was length+class only, so `"password"` scored ~2. The shared31  scorer forces common passwords and single-repeat strings to **0**. This is the32  intended improvement, but it means the Export gate (`score >= 3`) now rejects a33  few inputs it used to accept. Confirmed still-passing for real backup passwords34  (e.g. `Password123!`, `xK9#mQ2vL8wR` → 4).35- **Amber token.** `PeachColors` has no amber; `StrengthBar.kt` defines a local36  `StrengthAmber = Color(0xCCE0A040)` (documented). If the orchestrator prefers a37  theme token, add one to `PeachColors` and swap the reference.38- The 62-test suite is unaffected; `PasswordStrengthTest` adds 12 new tests. The39  existing `ExportRoundTripTest` does not assert on scoring.4041---4243## 1. CreateVault strength bar + gate (LockScreen / LockViewModel)4445### LockViewModel.kt4647Add the import:4849```kotlin50import com.peachpasswords.ui.security.scorePassword51```5253There is a module-level constant `MIN_PASSWORD_LENGTH = 12`. Add a strength54gate alongside it:5556```kotlin57const val MIN_PASSWORD_LENGTH = 125859/** CreateVault is blocked below "fair" (zxcvbn-style score 2). */60const val MIN_CREATE_STRENGTH = 261```6263**Anchor** — the existing `create()` validation block:6465```kotlin66        when {67            password.length < MIN_PASSWORD_LENGTH ->68                return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }69            password != confirm ->70                return _state.update { it.copy(error = "passwords do not match") }71        }72```7374Replace with (adds the strength branch — order: length first, then strength,75then match):7677```kotlin78        when {79            password.length < MIN_PASSWORD_LENGTH ->80                return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }81            scorePassword(password) < MIN_CREATE_STRENGTH ->82                return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") }83            password != confirm ->84                return _state.update { it.copy(error = "passwords do not match") }85        }86```8788> The view also disables the button below, so this branch is a backstop for the89> IME "Go" path.9091### LockScreen.kt9293Add imports:9495```kotlin96import com.peachpasswords.ui.security.MIN_CREATE_STRENGTH      // optional, if importing the const97import com.peachpasswords.ui.security.StrengthBar98import com.peachpasswords.ui.security.strengthColor99import com.peachpasswords.ui.security.strengthLabel100import com.peachpasswords.ui.security.scorePassword101```102103(If `MIN_CREATE_STRENGTH` lives in `LockViewModel`'s package — same package as104LockScreen — no import is needed for it.)105106Compute the score once in the composable body, near the `password`/`confirm`107state:108109```kotlin110    var password by remember { mutableStateOf("") }111    var confirm by remember { mutableStateOf("") }112    val createScore = remember(password) { scorePassword(password) }   // ADD113```114115**Anchor** — the create-mode confirm field block, ending at its closing brace:116117```kotlin118                if (!state.hasVault) {119                    Spacer(Modifier.height(16.dp))120                    FieldLabel("confirm password")121                    Spacer(Modifier.height(4.dp))122                    UnderlineField(123                        value = confirm,124                        onChange = { confirm = it; vm.clearError() },125                        placeholder = "••••••••••",126                        textStyle = mono18Spaced(),127                        imeAction = ImeAction.Go,128                        onImeAction = { vm.create(password, confirm) },129                    )130                }131```132133Insert the strength bar + label **inside** the `if (!state.hasVault)` block,134after the confirm `UnderlineField` (so the meter sits under both inputs):135136```kotlin137                if (!state.hasVault) {138                    Spacer(Modifier.height(16.dp))139                    FieldLabel("confirm password")140                    Spacer(Modifier.height(4.dp))141                    UnderlineField(142                        value = confirm,143                        onChange = { confirm = it; vm.clearError() },144                        placeholder = "••••••••••",145                        textStyle = mono18Spaced(),146                        imeAction = ImeAction.Go,147                        onImeAction = { vm.create(password, confirm) },148                    )149150                    if (password.isNotEmpty()) {                                   // ADD ▼151                        Spacer(Modifier.height(14.dp))152                        StrengthBar(score = createScore)153                        Spacer(Modifier.height(6.dp))154                        Text(155                            text = "strength: ${strengthLabel(createScore)}",156                            color = strengthColor(createScore),157                            style = PeachType.labelMono,158                            modifier = Modifier.fillMaxWidth(),159                            textAlign = TextAlign.Center,160                        )161                    }                                                              // ADD ▲162                }163```164165**Gate the button.** Anchor — the create-mode AccentButton:166167```kotlin168                AccentButton(169                    label = if (state.hasVault) "unlock" else "create vault",170                    enabled = !state.loading,171                    onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },172                )173```174175Change `enabled` so create mode also requires `score >= 2` (unlock mode keeps the176old behaviour):177178```kotlin179                AccentButton(180                    label = if (state.hasVault) "unlock" else "create vault",181                    enabled = !state.loading &&182                        (state.hasVault || createScore >= MIN_CREATE_STRENGTH),183                    onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },184                )185```186187> `MIN_CREATE_STRENGTH` is in the `com.peachpasswords.ui.security` package above;188> if you'd rather not import it, inline the literal `2` or move the const into189> `LockViewModel.kt` (same package as LockScreen, no import needed).190191---192193## 2. Swap ExportScreen to the shared scorer194195`ExportScreen.kt` currently has its own `scorePassword` / `strengthLabel`196(lines ~64-98) and a private `StrengthBar` composable (~348-364). Replace all197three with the shared ones.198199### Delete the local scorer block200201Remove the entire commented "Password strength scorer" section — both functions:202203```kotlin204internal fun scorePassword(pw: String): Int { ... }205internal fun strengthLabel(score: Int): String = when (score) { ... }206```207208### Delete the local StrengthBar composable209210Remove:211212```kotlin213@Composable214private fun StrengthBar(score: Int, accent: Color) {215    // Four segments; filled count == score. ...216}217```218219### Add imports220221```kotlin222import com.peachpasswords.ui.security.StrengthBar223import com.peachpasswords.ui.security.scorePassword224import com.peachpasswords.ui.security.strengthLabel225```226227### Fix the one call site that passes `accent`228229The shared `StrengthBar` takes only `(score, modifier)` — it reads accent from230`LocalPeachAccent` itself. Anchor:231232```kotlin233        Spacer(Modifier.size(16.dp))234        StrengthBar(score = score, accent = accent)235```236237Change to:238239```kotlin240        Spacer(Modifier.size(16.dp))241        StrengthBar(score = score)242```243244`PeachScriptExportView` already has `val accent = LocalPeachAccent.current` and245still uses it for the "strength: …" label and buttons, so leave that line. The246gate (`val strongEnough = score >= 3`) is unchanged and works with the new247scorer.