Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplelogin alias generation only generate random words instead the domain name #13024

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<vault-cipher-form-generator
[type]="params.type"
[uri]="cipher.login.uris[0]?.uri"
(valueGenerated)="onValueGenerated($event)"
></vault-cipher-form-generator>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CommonModule } from "@angular/common";
import { Component, Inject } from "@angular/core";

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { ButtonModule, DialogService } from "@bitwarden/components";
import { CipherFormGeneratorComponent } from "@bitwarden/vault";

Expand All @@ -15,6 +16,7 @@ import { PopupPageComponent } from "../../../../../platform/popup/layout/popup-p

export interface GeneratorDialogParams {
type: "password" | "username";
cipher?: CipherView;
audreyality marked this conversation as resolved.
Show resolved Hide resolved
}

export interface GeneratorDialogResult {
Expand Down Expand Up @@ -60,11 +62,15 @@ export class VaultGeneratorDialogComponent {
*/
protected generatedValue: string = "";

protected cipher: CipherView;

constructor(
@Inject(DIALOG_DATA) protected params: GeneratorDialogParams,
private dialogRef: DialogRef<GeneratorDialogResult>,
private i18nService: I18nService,
) {}
) {
this.cipher = params.cipher;
}

/**
* Close the dialog without selecting a value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Overlay } from "@angular/cdk/overlay";
import { inject, Injectable } from "@angular/core";
import { firstValueFrom } from "rxjs";

import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { DialogService } from "@bitwarden/components";
import { CipherFormGenerationService } from "@bitwarden/vault";

Expand All @@ -28,9 +29,9 @@ export class BrowserCipherFormGenerationService implements CipherFormGenerationS
return result.generatedValue;
}

async generateUsername(): Promise<string> {
async generateUsername(cipher: CipherView): Promise<string> {
const dialogRef = VaultGeneratorDialogComponent.open(this.dialogService, this.overlay, {
data: { type: "username" },
data: { type: "username", cipher },
});

const result = await firstValueFrom(dialogRef.closed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ng-container bitDialogContent>
<vault-cipher-form-generator
[type]="params.type"
[uri]="cipher.login.uris[0]?.uri"
(valueGenerated)="onValueGenerated($event)"
disableMargin
></vault-cipher-form-generator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { CommonModule } from "@angular/common";
import { Component, Inject } from "@angular/core";

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { ButtonModule, DialogModule, DialogService } from "@bitwarden/components";
import { CipherFormGeneratorComponent } from "@bitwarden/vault";

export interface WebVaultGeneratorDialogParams {
type: "password" | "username";
cipher?: CipherView;
}

export interface WebVaultGeneratorDialogResult {
Expand Down Expand Up @@ -48,11 +50,15 @@ export class WebVaultGeneratorDialogComponent {
*/
protected generatedValue: string = "";

protected cipher: CipherView;

constructor(
@Inject(DIALOG_DATA) protected params: WebVaultGeneratorDialogParams,
private dialogRef: DialogRef<WebVaultGeneratorDialogResult>,
private i18nService: I18nService,
) {}
) {
this.cipher = params.cipher;
}

/**
* Close the dialog without selecting a value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { inject, Injectable } from "@angular/core";
import { firstValueFrom } from "rxjs";

import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { DialogService } from "@bitwarden/components";
import { CipherFormGenerationService } from "@bitwarden/vault";

Expand All @@ -26,9 +27,9 @@ export class WebCipherFormGenerationService implements CipherFormGenerationServi
return result.generatedValue;
}

async generateUsername(): Promise<string> {
async generateUsername(cipher: CipherView): Promise<string> {
const dialogRef = WebVaultGeneratorDialogComponent.open(this.dialogService, {
data: { type: "username" },
data: { type: "username", cipher },
});

const result = await firstValueFrom(dialogRef.closed);
Expand Down
audreyality marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
// @ts-strict-ignore
import { LiveAnnouncer } from "@angular/cdk/a11y";
import { coerceBooleanProperty } from "@angular/cdk/coercion";
import { Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output } from "@angular/core";
import {
Component,
EventEmitter,
Input,
NgZone,
OnDestroy,
OnInit,
Output,
SimpleChanges,
OnChanges,
} from "@angular/core";
import { FormBuilder } from "@angular/forms";
import {
BehaviorSubject,
Expand Down Expand Up @@ -51,7 +61,7 @@ const NONE_SELECTED = "none";
selector: "tools-username-generator",
templateUrl: "username-generator.component.html",
})
export class UsernameGeneratorComponent implements OnInit, OnDestroy {
export class UsernameGeneratorComponent implements OnInit, OnDestroy, OnChanges {
/** Instantiates the username generator
* @param generatorService generates credentials; stores preferences
* @param i18nService localizes generator algorithm descriptions
Expand All @@ -77,6 +87,9 @@ export class UsernameGeneratorComponent implements OnInit, OnDestroy {
@Input()
userId: UserId | null;

@Input()
website: string | null = null;
audreyality marked this conversation as resolved.
Show resolved Hide resolved

/** Emits credentials created from a generation request. */
@Output()
readonly onGenerated = new EventEmitter<GeneratedCredential>();
Expand Down Expand Up @@ -341,6 +354,14 @@ export class UsernameGeneratorComponent implements OnInit, OnDestroy {
});
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.website && changes.website.currentValue !== changes.website.previousValue) {
// Update the website in the BehaviorSubject when the input changes
this.website = changes.website.currentValue;
audreyality marked this conversation as resolved.
Show resolved Hide resolved
this.generate$.next({ source: "websiteChange", website: this.website });
audreyality marked this conversation as resolved.
Show resolved Hide resolved
}
}

private typeToGenerator$(type: CredentialAlgorithm) {
const dependencies = {
on$: this.generate$,
Expand Down Expand Up @@ -435,7 +456,7 @@ export class UsernameGeneratorComponent implements OnInit, OnDestroy {
* origin in the debugger.
*/
protected async generate(requestor: string) {
this.generate$.next({ source: requestor });
this.generate$.next({ source: requestor, website: this.website });
}

private toOptions(algorithms: AlgorithmInfo[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
/**
* Service responsible for generating random passwords and usernames.
*/
Expand All @@ -10,5 +11,5 @@ export abstract class CipherFormGenerationService {
/**
* Generates a random username. Called when the user clicks the "Generate Username" button in the UI.
*/
abstract generateUsername(): Promise<string | null>;
abstract generateUsername(cipher: CipherView): Promise<string | null>;
}
2 changes: 2 additions & 0 deletions libs/vault/src/cipher-form/cipher-form-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export abstract class CipherFormContainer {
group: Exclude<CipherForm[K], undefined>,
): void;

abstract get getCipherView(): CipherView | null;
audreyality marked this conversation as resolved.
Show resolved Hide resolved

/**
* Method to update the cipherView with the new values. This method should be called by the child form components
* @param updateFn - A function that takes the current cipherView and returns the updated cipherView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class CipherFormComponent implements AfterViewInit, OnInit, OnChanges, Ci
*/
protected updatedCipherView: CipherView | null;

get getCipherView(): CipherView | null {
return this.updatedCipherView;
}

protected loading: boolean = true;

CipherType = CipherType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
></tools-password-generator>
<tools-username-generator
*ngIf="type === 'username'"
[website]="uri"
[disableMargin]="disableMargin"
(onGenerated)="onCredentialGenerated($event)"
(onAlgorithm)="algorithm($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class CipherFormGeneratorComponent {
@Input()
algorithm: (selected: AlgorithmInfo) => void;

@Input()
uri: string = "";

/**
* The type of generator form to show.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ export class LoginDetailsSectionComponent implements OnInit {
* TODO: Browser extension needs a means to cache the current form so values are not lost upon navigating to the generator.
*/
generateUsername = async () => {
const newUsername = await this.generationService.generateUsername();
const currentCipher = this.cipherFormContainer.getCipherView;
const newUsername = await this.generationService.generateUsername(currentCipher);
if (newUsername) {
this.loginDetailsForm.controls.username.patchValue(newUsername);
}
Expand Down
Loading