diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index b00da4fba7..8b70c39dfb 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -5,6 +5,7 @@ import { CipherType } from 'jslib/enums/cipherType'; import { CipherService } from 'jslib/abstractions/cipher.service'; import { CollectionService } from 'jslib/abstractions/collection.service'; import { FolderService } from 'jslib/abstractions/folder.service'; +import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service'; import { SyncService } from 'jslib/abstractions/sync.service'; import { TotpService } from 'jslib/abstractions/totp.service'; @@ -28,10 +29,10 @@ import { SecureNote } from '../models/secureNote'; export class GetCommand { constructor(private cipherService: CipherService, private folderService: FolderService, private collectionService: CollectionService, private totpService: TotpService, - private syncService: SyncService) { } + private syncService: SyncService, private passwordGenerationService: PasswordGenerationService) { } async run(object: string, id: string, cmd: program.Command): Promise { - if (id == null && object !== 'lastsync') { + if (id == null && object !== 'lastsync' && object !== 'password') { return Response.badRequest('`id` argument is required.'); } @@ -48,6 +49,8 @@ export class GetCommand { return await this.getTemplate(id); case 'lastsync': return await this.getLastSync(); + case 'password': + return await this.getPassword(cmd); default: return Response.badRequest('Unknown object.'); } @@ -153,4 +156,25 @@ export class GetCommand { const res = new StringResponse(lastSyncDate == null ? null : lastSyncDate.toISOString()); return Response.success(res); } + + private async getPassword(cmd: program.Command) { + const options = { + uppercase: cmd.uppercase || false, + lowercase: cmd.lowercase || false, + number: cmd.number || false, + special: cmd.special || false, + length: cmd.length || 14, + }; + if (!options.uppercase && !options.lowercase && !options.special && !options.number) { + options.lowercase = true; + options.uppercase = true; + options.number = true; + } + if (options.length < 5) { + options.length = 5; + } + const password = await this.passwordGenerationService.generatePassword(options); + const res = new StringResponse(password); + return Response.success(res); + } } diff --git a/src/program.ts b/src/program.ts index 2185847947..67a1960107 100644 --- a/src/program.ts +++ b/src/program.ts @@ -107,10 +107,16 @@ export class Program { program .command('get [id]') .description('Get an object.') + .option('--uppercase', 'Include uppercase characters.') + .option('--lowercase', 'Include lowercase characters.') + .option('--number', 'Include numeric characters.') + .option('--special', 'Include special characters.') + .option('--length ', 'Password length.') .action(async (object, id, cmd) => { await this.exitIfLocked(); const command = new GetCommand(this.main.cipherService, this.main.folderService, - this.main.collectionService, this.main.totpService, this.main.syncService); + this.main.collectionService, this.main.totpService, this.main.syncService, + this.main.passwordGenerationService); const response = await command.run(object, id, cmd); this.processResponse(response, cmd); });