Skip to content

Commit

Permalink
[PM-13723] track history in generator components (#11673)
Browse files Browse the repository at this point in the history
* add history support to generator components
* increase generator history length
  • Loading branch information
audreyality authored Oct 23, 2024
1 parent a2a15d4 commit d5643f4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isUsernameAlgorithm,
toCredentialGeneratorConfiguration,
} from "@bitwarden/generator-core";
import { GeneratorHistoryService } from "@bitwarden/generator-history";

// constants used to identify navigation selections that are not
// generator algorithms
Expand All @@ -51,6 +52,7 @@ const NONE_SELECTED = "none";
export class CredentialGeneratorComponent implements OnInit, OnDestroy {
constructor(
private generatorService: CredentialGeneratorService,
private generatorHistoryService: GeneratorHistoryService,
private toastService: ToastService,
private logService: LogService,
private i18nService: I18nService,
Expand Down Expand Up @@ -182,9 +184,16 @@ export class CredentialGeneratorComponent implements OnInit, OnDestroy {
// continue with origin stream
return generator;
}),
withLatestFrom(this.userId$),
takeUntil(this.destroyed),
)
.subscribe((generated) => {
.subscribe(([generated, userId]) => {
this.generatorHistoryService
.track(userId, generated.credential, generated.category, generated.generationDate)
.catch((e: unknown) => {
this.logService.error(e);
});

// update subjects within the angular zone so that the
// template bindings refresh immediately
this.zone.run(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { coerceBooleanProperty } from "@angular/cdk/coercion";
import { Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output } from "@angular/core";
import {
BehaviorSubject,
catchError,
distinctUntilChanged,
filter,
map,
Expand All @@ -14,7 +15,9 @@ import {

import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { UserId } from "@bitwarden/common/types/guid";
import { ToastService } from "@bitwarden/components";
import { Option } from "@bitwarden/components/src/select/option";
import {
CredentialGeneratorService,
Expand All @@ -25,6 +28,7 @@ import {
isPasswordAlgorithm,
AlgorithmInfo,
} from "@bitwarden/generator-core";
import { GeneratorHistoryService } from "@bitwarden/generator-history";

/** Options group for passwords */
@Component({
Expand All @@ -34,6 +38,9 @@ import {
export class PasswordGeneratorComponent implements OnInit, OnDestroy {
constructor(
private generatorService: CredentialGeneratorService,
private generatorHistoryService: GeneratorHistoryService,
private toastService: ToastService,
private logService: LogService,
private i18nService: I18nService,
private accountService: AccountService,
private zone: NgZone,
Expand Down Expand Up @@ -109,10 +116,32 @@ export class PasswordGeneratorComponent implements OnInit, OnDestroy {
// wire up the generator
this.algorithm$
.pipe(
filter((algorithm) => !!algorithm),
switchMap((algorithm) => this.typeToGenerator$(algorithm.id)),
catchError((error: unknown, generator) => {
if (typeof error === "string") {
this.toastService.showToast({
message: error,
variant: "error",
title: "",
});
} else {
this.logService.error(error);
}

// continue with origin stream
return generator;
}),
withLatestFrom(this.userId$),
takeUntil(this.destroyed),
)
.subscribe((generated) => {
.subscribe(([generated, userId]) => {
this.generatorHistoryService
.track(userId, generated.credential, generated.category, generated.generationDate)
.catch((e: unknown) => {
this.logService.error(e);
});

// update subjects within the angular zone so that the
// template bindings refresh immediately
this.zone.run(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
isUsernameAlgorithm,
toCredentialGeneratorConfiguration,
} from "@bitwarden/generator-core";
import { GeneratorHistoryService } from "@bitwarden/generator-history";

// constants used to identify navigation selections that are not
// generator algorithms
Expand All @@ -57,6 +58,7 @@ export class UsernameGeneratorComponent implements OnInit, OnDestroy {
*/
constructor(
private generatorService: CredentialGeneratorService,
private generatorHistoryService: GeneratorHistoryService,
private toastService: ToastService,
private logService: LogService,
private i18nService: I18nService,
Expand Down Expand Up @@ -153,9 +155,16 @@ export class UsernameGeneratorComponent implements OnInit, OnDestroy {
// continue with origin stream
return generator;
}),
withLatestFrom(this.userId$),
takeUntil(this.destroyed),
)
.subscribe((generated) => {
.subscribe(([generated, userId]) => {
this.generatorHistoryService
.track(userId, generated.credential, generated.category, generated.generationDate)
.catch((e: unknown) => {
this.logService.error(e);
});

// update subjects within the angular zone so that the
// template bindings refresh immediately
this.zone.run(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Jsonify } from "type-fest";

import { GeneratorCategory } from "./options";
import { CredentialAlgorithm } from "@bitwarden/generator-core";

/** A credential generation result */
export class GeneratedCredential {
Expand All @@ -14,7 +14,7 @@ export class GeneratedCredential {
*/
constructor(
readonly credential: string,
readonly category: GeneratorCategory,
readonly category: CredentialAlgorithm,
generationDate: Date | number,
) {
if (typeof generationDate === "number") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Observable } from "rxjs";

import { UserId } from "@bitwarden/common/types/guid";
import { CredentialAlgorithm } from "@bitwarden/generator-core";

import { GeneratedCredential } from "./generated-credential";
import { GeneratorCategory } from "./options";

/** Tracks the history of password generations.
* Each user gets their own store.
Expand All @@ -27,7 +27,7 @@ export abstract class GeneratorHistoryService {
track: (
userId: UserId,
credential: string,
category: GeneratorCategory,
category: CredentialAlgorithm,
date?: Date,
) => Promise<GeneratedCredential | null>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { PaddedDataPacker } from "@bitwarden/common/tools/state/padded-data-pack
import { SecretState } from "@bitwarden/common/tools/state/secret-state";
import { UserKeyEncryptor } from "@bitwarden/common/tools/state/user-key-encryptor";
import { UserId } from "@bitwarden/common/types/guid";
import { CredentialAlgorithm } from "@bitwarden/generator-core";

import { GeneratedCredential } from "./generated-credential";
import { GeneratorHistoryService } from "./generator-history.abstraction";
import { GENERATOR_HISTORY, GENERATOR_HISTORY_BUFFER } from "./key-definitions";
import { LegacyPasswordHistoryDecryptor } from "./legacy-password-history-decryptor";
import { GeneratorCategory, HistoryServiceOptions } from "./options";
import { HistoryServiceOptions } from "./options";

const OPTIONS_FRAME_SIZE = 2048;

Expand All @@ -25,15 +26,20 @@ export class LocalGeneratorHistoryService extends GeneratorHistoryService {
private readonly encryptService: EncryptService,
private readonly keyService: CryptoService,
private readonly stateProvider: StateProvider,
private readonly options: HistoryServiceOptions = { maxTotal: 100 },
private readonly options: HistoryServiceOptions = { maxTotal: 200 },
) {
super();
}

private _credentialStates = new Map<UserId, SingleUserState<GeneratedCredential[]>>();

/** {@link GeneratorHistoryService.track} */
track = async (userId: UserId, credential: string, category: GeneratorCategory, date?: Date) => {
track = async (
userId: UserId,
credential: string,
category: CredentialAlgorithm,
date?: Date,
) => {
const state = this.getCredentialState(userId);
let result: GeneratedCredential = null;

Expand Down

0 comments on commit d5643f4

Please sign in to comment.