1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

[PM-3609] [Tech-Debt] Add types to password and username generator (#6090)

* Create and use GeneratorOptions

Selection between `password`and `username`

* Use PasswordGeneratorOptions

* Declare and use UsernameGeneratorOptions
This commit is contained in:
Daniel James Smith
2023-09-05 21:48:34 +02:00
committed by GitHub
parent b78d17aa62
commit 255a7381b3
10 changed files with 93 additions and 38 deletions

View File

@@ -0,0 +1,3 @@
export type GeneratorOptions = {
type?: "password" | "username";
};

View File

@@ -1,3 +1,4 @@
export { PasswordGeneratorOptions } from "./password-generator-options";
export { PasswordGenerationServiceAbstraction } from "./password-generation.service.abstraction";
export { PasswordGenerationService } from "./password-generation.service";
export { GeneratedPasswordHistory } from "./generated-password-history";

View File

@@ -1,2 +1,3 @@
export { UsernameGeneratorOptions } from "./username-generation-options";
export { UsernameGenerationServiceAbstraction } from "./username-generation.service.abstraction";
export { UsernameGenerationService } from "./username-generation.service";

View File

@@ -0,0 +1,19 @@
export type UsernameGeneratorOptions = {
type?: "word" | "subaddress" | "catchall" | "forwarded";
wordCapitalize?: boolean;
wordIncludeNumber?: boolean;
subaddressType?: "random" | "website-name";
subaddressEmail?: string;
catchallType?: "random" | "website-name";
catchallDomain?: string;
website?: string;
forwardedService?: string;
forwardedAnonAddyApiToken?: string;
forwardedAnonAddyDomain?: string;
forwardedDuckDuckGoToken?: string;
forwardedFirefoxApiToken?: string;
forwardedFastmailApiToken?: string;
forwardedForwardEmailApiToken?: string;
forwardedForwardEmailDomain?: string;
forwardedSimpleLoginApiKey?: string;
};

View File

@@ -1,9 +1,11 @@
import { UsernameGeneratorOptions } from "./username-generation-options";
export abstract class UsernameGenerationServiceAbstraction {
generateUsername: (options: any) => Promise<string>;
generateWord: (options: any) => Promise<string>;
generateSubaddress: (options: any) => Promise<string>;
generateCatchall: (options: any) => Promise<string>;
generateForwarded: (options: any) => Promise<string>;
getOptions: () => Promise<any>;
saveOptions: (options: any) => Promise<void>;
generateUsername: (options: UsernameGeneratorOptions) => Promise<string>;
generateWord: (options: UsernameGeneratorOptions) => Promise<string>;
generateSubaddress: (options: UsernameGeneratorOptions) => Promise<string>;
generateCatchall: (options: UsernameGeneratorOptions) => Promise<string>;
generateForwarded: (options: UsernameGeneratorOptions) => Promise<string>;
getOptions: () => Promise<UsernameGeneratorOptions>;
saveOptions: (options: UsernameGeneratorOptions) => Promise<void>;
}

View File

@@ -13,9 +13,10 @@ import {
ForwarderOptions,
SimpleLoginForwarder,
} from "./email-forwarders";
import { UsernameGeneratorOptions } from "./username-generation-options";
import { UsernameGenerationServiceAbstraction } from "./username-generation.service.abstraction";
const DefaultOptions = {
const DefaultOptions: UsernameGeneratorOptions = {
type: "word",
wordCapitalize: true,
wordIncludeNumber: true,
@@ -33,7 +34,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
private apiService: ApiService
) {}
generateUsername(options: any): Promise<string> {
generateUsername(options: UsernameGeneratorOptions): Promise<string> {
if (options.type === "catchall") {
return this.generateCatchall(options);
} else if (options.type === "subaddress") {
@@ -45,7 +46,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
}
}
async generateWord(options: any): Promise<string> {
async generateWord(options: UsernameGeneratorOptions): Promise<string> {
const o = Object.assign({}, DefaultOptions, options);
if (o.wordCapitalize == null) {
@@ -67,7 +68,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
return word;
}
async generateSubaddress(options: any): Promise<string> {
async generateSubaddress(options: UsernameGeneratorOptions): Promise<string> {
const o = Object.assign({}, DefaultOptions, options);
const subaddressEmail = o.subaddressEmail;
@@ -94,7 +95,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
return emailBeginning + "+" + subaddressString + "@" + emailEnding;
}
async generateCatchall(options: any): Promise<string> {
async generateCatchall(options: UsernameGeneratorOptions): Promise<string> {
const o = Object.assign({}, DefaultOptions, options);
if (o.catchallDomain == null || o.catchallDomain === "") {
@@ -113,7 +114,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
return startString + "@" + o.catchallDomain;
}
async generateForwarded(options: any): Promise<string> {
async generateForwarded(options: UsernameGeneratorOptions): Promise<string> {
const o = Object.assign({}, DefaultOptions, options);
if (o.forwardedService == null) {
@@ -152,7 +153,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
return forwarder.generate(this.apiService, forwarderOptions);
}
async getOptions(): Promise<any> {
async getOptions(): Promise<UsernameGeneratorOptions> {
let options = await this.stateService.getUsernameGenerationOptions();
if (options == null) {
options = Object.assign({}, DefaultOptions);
@@ -163,7 +164,7 @@ export class UsernameGenerationService implements UsernameGenerationServiceAbstr
return options;
}
async saveOptions(options: any) {
async saveOptions(options: UsernameGeneratorOptions) {
await this.stateService.setUsernameGenerationOptions(options);
}