mirror of
https://github.com/bitwarden/browser
synced 2025-12-29 14:43:31 +00:00
soft locking with protected pin
This commit is contained in:
@@ -19,6 +19,7 @@ export class ConstantsService {
|
||||
static readonly dontShowIdentitiesCurrentTab: string = 'dontShowIdentitiesCurrentTab';
|
||||
static readonly defaultUriMatch: string = 'defaultUriMatch';
|
||||
static readonly pinProtectedKey: string = 'pinProtectedKey';
|
||||
static readonly protectedPin: string = 'protectedPin';
|
||||
|
||||
readonly environmentUrlsKey: string = ConstantsService.environmentUrlsKey;
|
||||
readonly disableGaKey: string = ConstantsService.disableGaKey;
|
||||
@@ -39,4 +40,5 @@ export class ConstantsService {
|
||||
readonly dontShowIdentitiesCurrentTab: string = ConstantsService.dontShowIdentitiesCurrentTab;
|
||||
readonly defaultUriMatch: string = ConstantsService.defaultUriMatch;
|
||||
readonly pinProtectedKey: string = ConstantsService.pinProtectedKey;
|
||||
readonly protectedPin: string = ConstantsService.protectedPin;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import { SearchService } from '../abstractions/search.service';
|
||||
import { StorageService } from '../abstractions/storage.service';
|
||||
|
||||
export class LockService implements LockServiceAbstraction {
|
||||
pinLocked = false;
|
||||
|
||||
private inited = false;
|
||||
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
@@ -32,12 +34,24 @@ export class LockService implements LockServiceAbstraction {
|
||||
}
|
||||
}
|
||||
|
||||
async isLocked(): Promise<boolean> {
|
||||
if (this.pinLocked) {
|
||||
return true;
|
||||
}
|
||||
const hasKey = await this.cryptoService.hasKey();
|
||||
return !hasKey;
|
||||
}
|
||||
|
||||
async checkLock(): Promise<void> {
|
||||
if (this.platformUtilsService.isViewOpen()) {
|
||||
// Do not lock
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.pinLocked) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasKey = await this.cryptoService.hasKey();
|
||||
if (!hasKey) {
|
||||
// no key so no need to lock
|
||||
@@ -61,11 +75,19 @@ export class LockService implements LockServiceAbstraction {
|
||||
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
||||
if (diffSeconds >= lockOptionSeconds) {
|
||||
// need to lock now
|
||||
await this.lock();
|
||||
await this.lock(true);
|
||||
}
|
||||
}
|
||||
|
||||
async lock(): Promise<void> {
|
||||
async lock(allowSoftLock = false): Promise<void> {
|
||||
if (allowSoftLock) {
|
||||
const pinSet = await this.isPinLockSet();
|
||||
if (pinSet[0]) {
|
||||
await this.pinLock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
this.cryptoService.clearKey(),
|
||||
this.cryptoService.clearOrgKeys(true),
|
||||
@@ -88,8 +110,21 @@ export class LockService implements LockServiceAbstraction {
|
||||
await this.cryptoService.toggleKey();
|
||||
}
|
||||
|
||||
async isPinLockSet(): Promise<boolean> {
|
||||
async isPinLockSet(): Promise<[boolean, boolean]> {
|
||||
const protectedPin = await this.storageService.get<string>(ConstantsService.protectedPin);
|
||||
const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
|
||||
return pinProtectedKey != null;
|
||||
return [protectedPin != null, pinProtectedKey != null];
|
||||
}
|
||||
|
||||
clear(): Promise<any> {
|
||||
return this.storageService.remove(ConstantsService.protectedPin);
|
||||
}
|
||||
|
||||
private async pinLock(): Promise<void> {
|
||||
this.pinLocked = true;
|
||||
this.messagingService.send('locked');
|
||||
if (this.lockedCallback != null) {
|
||||
await this.lockedCallback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import { NotificationType } from '../enums/notificationType';
|
||||
|
||||
import { ApiService } from '../abstractions/api.service';
|
||||
import { AppIdService } from '../abstractions/appId.service';
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { EnvironmentService } from '../abstractions/environment.service';
|
||||
import { LockService } from '../abstractions/lock.service';
|
||||
import { NotificationsService as NotificationsServiceAbstraction } from '../abstractions/notifications.service';
|
||||
import { SyncService } from '../abstractions/sync.service';
|
||||
import { UserService } from '../abstractions/user.service';
|
||||
@@ -27,7 +27,7 @@ export class NotificationsService implements NotificationsServiceAbstraction {
|
||||
|
||||
constructor(private userService: UserService, private syncService: SyncService,
|
||||
private appIdService: AppIdService, private apiService: ApiService,
|
||||
private cryptoService: CryptoService, private logoutCallback: () => Promise<void>) { }
|
||||
private lockService: LockService, private logoutCallback: () => Promise<void>) { }
|
||||
|
||||
async init(environmentService: EnvironmentService): Promise<void> {
|
||||
this.inited = false;
|
||||
@@ -185,7 +185,7 @@ export class NotificationsService implements NotificationsServiceAbstraction {
|
||||
|
||||
private async isAuthedAndUnlocked() {
|
||||
if (await this.userService.isAuthenticated()) {
|
||||
return this.cryptoService.hasKey();
|
||||
return this.lockService.isLocked();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user