[PM-46038] feat(unlock): Unlock PIN without clicking button (#24304)

* Update sdk-internal to 0.2.0-main.1202

* Implement PIN length storage in JsWasmStateBridge

sdk-internal 0.2.0-main.1202 adds required set/get/clear_pin_length members to WasmStateBridge (bitwarden/sdk-internal#1708). The SDK stores the PIN's character count unencrypted so a lock screen can read it while locked, so it is backed by a new PIN_DISK state key that clears on logout.

* Try the PIN as soon as it is fully typed

Unlocking with a PIN needed a press of Unlock, or Enter, after typing
it. The SDK now stores the PIN's length through the state bridge, and
the lock screen submits the PIN as soon as that many characters are
typed. A wrong PIN of that length counts as an attempt, as if Unlock
was pressed.

The form's submit handler restarted on every submit rather than
ignoring one while the last ran, so the automatic try plus an Enter
cost two of the five allowed attempts. A submit while the PIN is being
tried is now ignored.

The length is stored unencrypted, since the lock screen reads it while
locked.

---------

Co-authored-by: bw-ghapp[bot] <178206702+bw-ghapp[bot]@users.noreply.github.com>
This commit is contained in:
Bernd Schoolmann
2026-11-12 08:29:07 +09:00
committed by GitHub
parent 237e30308c
commit 752719b07c
5 changed files with 105 additions and 2 deletions

View File

@@ -54,6 +54,13 @@ export abstract class PinServiceAbstraction {
*/
abstract logout(userId: UserId): Promise<void>;
/**
* Gets the number of characters in the user's PIN, readable while locked.
* @returns The length, or undefined when no PIN is set, or for a PIN set before its length was
* stored that hasn't unlocked since.
*/
abstract getPinLength(userId: UserId): Promise<number | undefined>;
/**
* Verifies the validity of the provided PIN for the user.
* @returns A promise resolving to true if the PIN is valid, false otherwise.

View File

@@ -62,6 +62,13 @@ export class PinService implements PinServiceAbstraction {
});
}
@assertParametersNonNull()
async getPinLength(userId: UserId): Promise<number | undefined> {
return await this.withPinSettingsClient(userId, async (client) => {
return await client.get_pin_length();
});
}
@assertParametersNonNull()
async validatePin(pin: string, userId: UserId): Promise<boolean> {
return await this.withPinSettingsClient(userId, async (client) => {

View File

@@ -60,10 +60,12 @@
<!-- PIN Unlock -->
<ng-container *ngIf="unlockOptions.pin.enabled && activeUnlockOption === UnlockOption.Pin">
<form [bitSubmit]="submit" [formGroup]="formGroup">
<form [bitSubmit]="submit" [formGroup]="formGroup" #pinForm>
<bit-form-field>
<bit-label>{{ "pin" | i18n }}</bit-label>
<input
#pinInput
(input)="tryPinWhenComplete(pinInput.value, pinForm)"
type="password"
formControlName="pin"
bitInput

View File

@@ -16,6 +16,7 @@ import { ClientType, DeviceType } from "@bitwarden/common/enums";
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/key-management/device-trust/abstractions/device-trust.service.abstraction";
import { EncryptedMigrator } from "@bitwarden/common/key-management/encrypted-migrator/encrypted-migrator.abstraction";
import { InternalMasterPasswordServiceAbstraction } from "@bitwarden/common/key-management/master-password/abstractions/master-password.service.abstraction";
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
@@ -88,6 +89,7 @@ describe("LockComponent", () => {
const mockUnlockService = mock<UnlockService>();
const mockWebAuthnPrfUnlockService = mock<WebAuthnPrfUnlockService>();
const mockEncryptedMigrator = mock<EncryptedMigrator>();
const mockPinService = mock<PinServiceAbstraction>();
const mockActivatedRoute = {
snapshot: {
paramMap: {
@@ -157,6 +159,7 @@ describe("LockComponent", () => {
{ provide: WebAuthnPrfUnlockService, useValue: mockWebAuthnPrfUnlockService },
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
{ provide: EncryptedMigrator, useValue: mockEncryptedMigrator },
{ provide: PinServiceAbstraction, useValue: mockPinService },
],
})
.overrideProvider(DialogService, { useValue: mockDialogService })
@@ -166,6 +169,56 @@ describe("LockComponent", () => {
component = fixture.componentInstance;
});
describe("PIN length", () => {
const form = () => ({ requestSubmit: jest.fn() }) as unknown as HTMLFormElement;
beforeEach(() => {
component.activeAccount = { id: userId } as any;
mockPinService.getPinLength.mockResolvedValue(4);
});
it("tries the PIN once it has the stored PIN length", async () => {
const pinForm = form();
await component["tryPinWhenComplete"]("1234", pinForm);
expect(pinForm.requestSubmit).toHaveBeenCalled();
});
it("waits while the PIN is shorter or longer than the stored length", async () => {
const pinForm = form();
await component["tryPinWhenComplete"]("123", pinForm);
await component["tryPinWhenComplete"]("12345", pinForm);
expect(pinForm.requestSubmit).not.toHaveBeenCalled();
});
it("waits for the Unlock button when no length is stored", async () => {
mockPinService.getPinLength.mockResolvedValue(undefined);
const pinForm = form();
await component["tryPinWhenComplete"]("1234", pinForm);
expect(pinForm.requestSubmit).not.toHaveBeenCalled();
});
it("tries a PIN once when it is submitted again while unlocking, e.g. by Enter", async () => {
component.formGroup = new FormBuilder().group({ pin: ["1234"] });
let finishUnlock!: () => void;
mockUnlockService.unlockWithPin.mockReturnValue(
new Promise<void>((resolve) => (finishUnlock = resolve)),
);
const first = component["unlockViaPin"]();
const second = component["unlockViaPin"]();
finishUnlock();
await Promise.all([first, second]);
expect(mockUnlockService.unlockWithPin).toHaveBeenCalledTimes(1);
});
});
describe("unlocked$", () => {
const unlockOptions = {
masterPassword: { enabled: true },

View File

@@ -1,5 +1,5 @@
import { CommonModule } from "@angular/common";
import { Component, NgZone, OnDestroy, OnInit } from "@angular/core";
import { Component, inject, NgZone, OnDestroy, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from "@angular/forms";
import { Router, ActivatedRoute } from "@angular/router";
import {
@@ -26,6 +26,7 @@ import { ClientType, DeviceType } from "@bitwarden/common/enums";
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/key-management/device-trust/abstractions/device-trust.service.abstraction";
import { EncryptedMigrator } from "@bitwarden/common/key-management/encrypted-migrator/encrypted-migrator.abstraction";
import { InternalMasterPasswordServiceAbstraction } from "@bitwarden/common/key-management/master-password/abstractions/master-password.service.abstraction";
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
@@ -138,6 +139,8 @@ export class LockComponent implements OnInit, OnDestroy {
}
private invalidPinAttempts = 0;
private unlockingViaPin = false;
private readonly pinService = inject(PinServiceAbstraction);
biometricUnlockBtnText?: string;
@@ -472,6 +475,23 @@ export class LockComponent implements OnInit, OnDestroy {
);
}
/**
* Tries the PIN as soon as it has as many characters as the PIN length the SDK stored, as if the
* user pressed Unlock: a wrong PIN counts as an attempt like any other.
*/
protected async tryPinWhenComplete(pin: string, form: HTMLFormElement): Promise<void> {
if (this.activeAccount == null) {
return;
}
const pinLength = await this.pinService.getPinLength(this.activeAccount.id);
if (pinLength == null || pin.length !== pinLength) {
return;
}
form.requestSubmit();
}
togglePassword() {
this.showPassword = !this.showPassword;
}
@@ -490,6 +510,20 @@ export class LockComponent implements OnInit, OnDestroy {
}
private async unlockViaPin() {
// The PIN is tried as soon as it is typed; an Enter right after must not try it again.
if (this.unlockingViaPin) {
return;
}
this.unlockingViaPin = true;
try {
await this.tryPin();
} finally {
this.unlockingViaPin = false;
}
}
private async tryPin() {
if (!this.validatePin() || this.formGroup == null || this.activeAccount == null) {
return;
}