bitwarden-estensione-browser/apps/cli/src/commands/get.command.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

541 lines
18 KiB
TypeScript
Raw Normal View History

2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AuditService } from "@bitwarden/common/abstractions/audit.service";
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
import { CollectionService } from "@bitwarden/common/abstractions/collection.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { FolderService } from "@bitwarden/common/abstractions/folder.service";
import { OrganizationService } from "@bitwarden/common/abstractions/organization.service";
import { SearchService } from "@bitwarden/common/abstractions/search.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { TotpService } from "@bitwarden/common/abstractions/totp.service";
import { CipherType } from "@bitwarden/common/enums/cipherType";
import { SendType } from "@bitwarden/common/enums/sendType";
import { Utils } from "@bitwarden/common/misc/utils";
import { EncString } from "@bitwarden/common/models/domain/encString";
import { Organization } from "@bitwarden/common/models/domain/organization";
import { CardExport } from "@bitwarden/common/models/export/cardExport";
import { CipherExport } from "@bitwarden/common/models/export/cipherExport";
import { CollectionExport } from "@bitwarden/common/models/export/collectionExport";
import { FieldExport } from "@bitwarden/common/models/export/fieldExport";
import { FolderExport } from "@bitwarden/common/models/export/folderExport";
import { IdentityExport } from "@bitwarden/common/models/export/identityExport";
import { LoginExport } from "@bitwarden/common/models/export/loginExport";
import { LoginUriExport } from "@bitwarden/common/models/export/loginUriExport";
import { SecureNoteExport } from "@bitwarden/common/models/export/secureNoteExport";
import { ErrorResponse } from "@bitwarden/common/models/response/errorResponse";
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { CollectionView } from "@bitwarden/common/models/view/collectionView";
import { FolderView } from "@bitwarden/common/models/view/folderView";
import { Response } from "@bitwarden/node/cli/models/response";
import { StringResponse } from "@bitwarden/node/cli/models/response/stringResponse";
2022-03-03 18:24:41 +01:00
import { OrganizationCollectionRequest } from "../models/request/organizationCollectionRequest";
2018-05-14 20:54:19 +02:00
import { CipherResponse } from "../models/response/cipherResponse";
import { CollectionResponse } from "../models/response/collectionResponse";
import { FolderResponse } from "../models/response/folderResponse";
2019-09-25 23:11:48 +02:00
import { OrganizationCollectionResponse } from "../models/response/organizationCollectionResponse";
2018-05-18 21:26:59 +02:00
import { OrganizationResponse } from "../models/response/organizationResponse";
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
import { SendResponse } from "../models/response/sendResponse";
2018-05-14 22:25:14 +02:00
import { TemplateResponse } from "../models/response/templateResponse";
2019-09-25 23:11:48 +02:00
import { SelectionReadOnly } from "../models/selectionReadOnly";
2018-05-16 17:54:59 +02:00
import { CliUtils } from "../utils";
2022-03-03 18:24:41 +01:00
import { DownloadCommand } from "./download.command";
2018-11-16 16:02:22 +01:00
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
export class GetCommand extends DownloadCommand {
2018-05-14 19:37:52 +02:00
constructor(
private cipherService: CipherService,
private folderService: FolderService,
2018-05-17 17:07:53 +02:00
private collectionService: CollectionService,
private totpService: TotpService,
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
private auditService: AuditService,
cryptoService: CryptoService,
private stateService: StateService,
2018-05-14 22:25:14 +02:00
private searchService: SearchService,
2018-05-16 19:59:08 +02:00
private apiService: ApiService,
private organizationService: OrganizationService
2021-12-20 18:04:00 +01:00
) {
2018-05-14 20:54:19 +02:00
super(cryptoService);
2021-12-20 18:04:00 +01:00
}
2022-01-19 16:45:14 +01:00
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
2018-11-16 16:02:22 +01:00
if (id != null) {
2018-05-14 20:54:19 +02:00
id = id.toLowerCase();
2018-05-14 19:37:52 +02:00
}
2022-01-19 16:45:14 +01:00
const normalizedOptions = new Options(cmdOptions);
2019-10-21 20:16:16 +02:00
switch (object.toLowerCase()) {
case "item":
2018-05-16 17:54:59 +02:00
return await this.getCipher(id);
case "username":
return await this.getUsername(id);
case "password":
2018-05-16 19:59:08 +02:00
return await this.getPassword(id);
2021-12-20 18:04:00 +01:00
case "uri":
2018-05-16 19:59:08 +02:00
return await this.getUri(id);
2018-05-16 17:54:59 +02:00
case "totp":
2018-05-14 20:54:19 +02:00
return await this.getTotp(id);
2021-12-20 18:04:00 +01:00
case "notes":
return await this.getNotes(id);
2018-05-17 17:07:53 +02:00
case "exposed":
return await this.getExposed(id);
case "attachment":
2022-01-19 16:45:14 +01:00
return await this.getAttachment(id, normalizedOptions);
2018-05-16 17:54:59 +02:00
case "folder":
return await this.getFolder(id);
2019-09-25 23:11:48 +02:00
case "collection":
2018-05-14 20:54:19 +02:00
return await this.getCollection(id);
2019-09-25 23:11:48 +02:00
case "org-collection":
2022-01-19 16:45:14 +01:00
return await this.getOrganizationCollection(id, normalizedOptions);
2018-05-18 21:26:59 +02:00
case "organization":
return await this.getOrganization(id);
2018-05-14 22:25:14 +02:00
case "template":
2018-05-16 17:54:59 +02:00
return await this.getTemplate(id);
2018-08-13 20:38:04 +02:00
case "fingerprint":
return await this.getFingerprint(id);
2018-12-08 03:55:32 +01:00
default:
return Response.badRequest("Unknown object.");
}
2021-12-20 18:04:00 +01:00
}
2019-10-21 20:16:16 +02:00
private async getCipherView(id: string): Promise<CipherView | CipherView[]> {
let decCipher: CipherView = null;
2019-10-01 17:04:15 +02:00
if (Utils.isGuid(id)) {
2019-10-21 20:16:16 +02:00
const cipher = await this.cipherService.get(id);
2018-05-16 17:54:59 +02:00
if (cipher != null) {
decCipher = await cipher.decrypt();
2021-12-20 18:04:00 +01:00
}
} else if (id.trim() !== "") {
let ciphers = await this.cipherService.getAllDecrypted();
2018-08-13 20:38:04 +02:00
ciphers = this.searchService.searchCiphersBasic(ciphers, id);
if (ciphers.length > 1) {
2019-10-21 20:16:16 +02:00
return ciphers;
2021-12-20 18:04:00 +01:00
}
2018-05-16 17:54:59 +02:00
if (ciphers.length > 0) {
decCipher = ciphers[0];
2018-12-08 03:55:32 +01:00
}
2018-05-14 19:37:52 +02:00
}
2018-05-16 19:59:08 +02:00
return decCipher;
}
private async getCipher(id: string, filter?: (c: CipherView) => boolean) {
2018-12-08 03:55:32 +01:00
let decCipher = await this.getCipherView(id);
2018-05-18 16:55:50 +02:00
if (decCipher == null) {
2018-05-16 17:54:59 +02:00
return Response.notFound();
2021-12-20 18:04:00 +01:00
}
2019-10-21 20:16:16 +02:00
if (Array.isArray(decCipher)) {
if (filter != null) {
2018-05-16 17:54:59 +02:00
decCipher = decCipher.filter(filter);
2018-05-16 19:59:08 +02:00
if (decCipher.length === 1) {
2018-05-16 17:54:59 +02:00
decCipher = decCipher[0];
2021-12-20 18:04:00 +01:00
}
}
2019-10-21 20:16:16 +02:00
if (Array.isArray(decCipher)) {
2018-05-16 19:59:08 +02:00
return Response.multipleResults(decCipher.map((c) => c.id));
2021-12-20 18:04:00 +01:00
}
}
2018-05-16 19:59:08 +02:00
const res = new CipherResponse(decCipher);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-16 19:59:08 +02:00
private async getUsername(id: string) {
2019-10-21 20:16:16 +02:00
const cipherResponse = await this.getCipher(
2021-12-20 18:04:00 +01:00
id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.username)
2021-12-20 18:04:00 +01:00
);
2018-05-16 19:59:08 +02:00
if (!cipherResponse.success) {
return cipherResponse;
}
const cipher = cipherResponse.data as CipherResponse;
if (cipher.type !== CipherType.Login) {
return Response.badRequest("Not a login.");
}
2019-10-21 20:16:16 +02:00
if (Utils.isNullOrWhitespace(cipher.login.username)) {
2018-05-16 19:59:08 +02:00
return Response.error("No username available for this login.");
}
const res = new StringResponse(cipher.login.username);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-16 19:59:08 +02:00
private async getPassword(id: string) {
2019-10-21 20:16:16 +02:00
const cipherResponse = await this.getCipher(
2021-12-20 18:04:00 +01:00
id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.password)
2021-12-20 18:04:00 +01:00
);
2018-05-16 19:59:08 +02:00
if (!cipherResponse.success) {
return cipherResponse;
}
2019-10-21 20:16:16 +02:00
const cipher = cipherResponse.data as CipherResponse;
if (cipher.type !== CipherType.Login) {
2018-05-16 19:59:08 +02:00
return Response.badRequest("Not a login.");
}
if (Utils.isNullOrWhitespace(cipher.login.password)) {
return Response.error("No password available for this login.");
}
const res = new StringResponse(cipher.login.password);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-16 19:59:08 +02:00
private async getUri(id: string) {
const cipherResponse = await this.getCipher(
2021-12-20 18:04:00 +01:00
id,
(c) =>
c.type === CipherType.Login &&
2018-05-16 19:59:08 +02:00
c.login.uris != null &&
c.login.uris.length > 0 &&
c.login.uris[0].uri !== ""
2021-12-20 18:04:00 +01:00
);
2018-05-16 17:54:59 +02:00
if (!cipherResponse.success) {
return cipherResponse;
2018-05-16 19:59:08 +02:00
}
const cipher = cipherResponse.data as CipherResponse;
if (cipher.type !== CipherType.Login) {
return Response.badRequest("Not a login.");
}
2021-12-20 18:04:00 +01:00
if (
cipher.login.uris == null ||
cipher.login.uris.length === 0 ||
2018-05-16 17:54:59 +02:00
cipher.login.uris[0].uri === ""
) {
return Response.error("No uri available for this login.");
2018-05-14 19:37:52 +02:00
}
2018-05-16 17:54:59 +02:00
const res = new StringResponse(cipher.login.uris[0].uri);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-16 17:54:59 +02:00
private async getTotp(id: string) {
const cipherResponse = await this.getCipher(
2018-05-14 19:37:52 +02:00
id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.totp)
2021-12-20 18:04:00 +01:00
);
2018-05-14 19:37:52 +02:00
if (!cipherResponse.success) {
2018-05-14 20:54:19 +02:00
return cipherResponse;
2018-05-14 19:37:52 +02:00
}
2019-10-21 20:16:16 +02:00
const cipher = cipherResponse.data as CipherResponse;
if (cipher.type !== CipherType.Login) {
2018-05-14 20:54:19 +02:00
return Response.badRequest("Not a login.");
2018-05-14 19:37:52 +02:00
}
2018-05-16 17:54:59 +02:00
if (Utils.isNullOrWhitespace(cipher.login.totp)) {
2018-05-14 20:54:19 +02:00
return Response.error("No TOTP available for this login.");
2018-05-14 19:37:52 +02:00
}
const totp = await this.totpService.getCode(cipher.login.totp);
2018-05-18 16:55:50 +02:00
if (totp == null) {
2018-08-29 05:17:44 +02:00
return Response.error("Couldn't generate TOTP code.");
2018-05-18 16:55:50 +02:00
}
const canAccessPremium = await this.stateService.getCanAccessPremium();
2018-08-29 05:17:44 +02:00
if (!canAccessPremium) {
2018-05-14 20:54:19 +02:00
const originalCipher = await this.cipherService.get(cipher.id);
2021-12-20 18:04:00 +01:00
if (
2018-05-14 20:54:19 +02:00
originalCipher == null ||
2018-05-18 16:55:50 +02:00
originalCipher.organizationId == null ||
2018-05-14 20:54:19 +02:00
!originalCipher.organizationUseTotp
2021-12-20 18:04:00 +01:00
) {
2018-05-14 20:54:19 +02:00
return Response.error("Premium status is required to use this feature.");
2021-12-20 18:04:00 +01:00
}
2018-05-14 19:37:52 +02:00
}
const res = new StringResponse(totp);
return Response.success(res);
}
private async getNotes(id: string) {
const cipherResponse = await this.getCipher(id, (c) => !Utils.isNullOrWhitespace(c.notes));
if (!cipherResponse.success) {
return cipherResponse;
}
const cipher = cipherResponse.data as CipherResponse;
if (Utils.isNullOrWhitespace(cipher.notes)) {
return Response.error("No notes available for this item.");
}
2018-05-17 17:07:53 +02:00
const res = new StringResponse(cipher.notes);
return Response.success(res);
}
private async getExposed(id: string) {
const passwordResponse = await this.getPassword(id);
if (!passwordResponse.success) {
return passwordResponse;
}
const exposedNumber = await this.auditService.passwordLeaked(
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
(passwordResponse.data as StringResponse).data
2021-12-20 18:04:00 +01:00
);
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
const res = new StringResponse(exposedNumber.toString());
2018-05-17 21:55:44 +02:00
return Response.success(res);
}
2018-05-17 19:43:53 +02:00
2022-01-19 16:45:14 +01:00
private async getAttachment(id: string, options: Options) {
if (options.itemId == null || options.itemId === "") {
2018-05-17 19:28:22 +02:00
return Response.badRequest("--itemid <itemid> required.");
}
2022-01-19 16:45:14 +01:00
const itemId = options.itemId.toLowerCase();
const cipherResponse = await this.getCipher(itemId);
2018-05-17 19:43:53 +02:00
if (!cipherResponse.success) {
2018-05-17 21:55:44 +02:00
return cipherResponse;
2018-05-17 19:28:22 +02:00
}
const cipher = await this.getCipherView(itemId);
2021-12-20 18:04:00 +01:00
if (
cipher == null ||
Array.isArray(cipher) ||
cipher.attachments == null ||
cipher.attachments.length === 0
2021-12-20 18:04:00 +01:00
) {
return Response.error("No attachments available for this item.");
}
let attachments = cipher.attachments.filter(
2021-12-20 18:04:00 +01:00
(a) =>
2018-05-16 17:54:59 +02:00
a.id.toLowerCase() === id ||
2018-05-17 19:43:53 +02:00
(a.fileName != null && a.fileName.toLowerCase().indexOf(id) > -1)
2021-12-20 18:04:00 +01:00
);
2018-05-17 19:43:53 +02:00
if (attachments.length === 0) {
return Response.error("Attachment `" + id + "` was not found.");
2018-05-17 19:28:22 +02:00
}
2018-05-18 16:55:50 +02:00
const exactMatches = attachments.filter((a) => a.fileName.toLowerCase() === id);
if (exactMatches.length === 1) {
attachments = exactMatches;
2018-05-18 16:55:50 +02:00
}
if (attachments.length > 1) {
return Response.multipleResults(attachments.map((a) => a.id));
}
if (!(await this.stateService.getCanAccessPremium())) {
2018-05-16 17:54:59 +02:00
const originalCipher = await this.cipherService.get(cipher.id);
if (originalCipher == null || originalCipher.organizationId == null) {
return Response.error("Premium status is required to use this feature.");
2018-05-16 17:54:59 +02:00
}
}
2018-05-14 19:37:52 +02:00
let url: string;
try {
2018-05-16 17:54:59 +02:00
const attachmentDownloadResponse = await this.apiService.getAttachmentData(
cipher.id,
attachments[0].id
2018-05-16 17:54:59 +02:00
);
url = attachmentDownloadResponse.url;
} catch (e) {
2018-05-16 17:54:59 +02:00
if (e instanceof ErrorResponse && (e as ErrorResponse).statusCode === 404) {
url = attachments[0].url;
} else if (e instanceof ErrorResponse) {
throw new Error((e as ErrorResponse).getSingleMessage());
2018-05-16 17:54:59 +02:00
} else {
throw e;
}
2018-05-14 19:37:52 +02:00
}
2018-05-16 17:54:59 +02:00
const key =
attachments[0].key != null
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
? attachments[0].key
: await this.cryptoService.getOrgKey(cipher.organizationId);
2018-05-16 17:54:59 +02:00
return await this.saveAttachmentToFile(url, key, attachments[0].fileName, options.output);
2021-12-20 18:04:00 +01:00
}
2018-05-14 19:37:52 +02:00
private async getFolder(id: string) {
2018-05-16 17:54:59 +02:00
let decFolder: FolderView = null;
2019-10-01 17:04:15 +02:00
if (Utils.isGuid(id)) {
2018-05-16 17:54:59 +02:00
const folder = await this.folderService.get(id);
if (folder != null) {
decFolder = await folder.decrypt();
}
2018-05-18 21:26:59 +02:00
} else if (id.trim() !== "") {
2018-05-14 20:54:19 +02:00
let folders = await this.folderService.getAllDecrypted();
folders = CliUtils.searchFolders(folders, id);
if (folders.length > 1) {
return Response.multipleResults(folders.map((f) => f.id));
2021-12-20 18:04:00 +01:00
}
2018-05-16 17:54:59 +02:00
if (folders.length > 0) {
decFolder = folders[0];
2021-12-20 18:04:00 +01:00
}
2018-05-14 19:37:52 +02:00
}
2018-05-14 22:25:14 +02:00
2018-05-16 17:54:59 +02:00
if (decFolder == null) {
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
return Response.notFound();
2021-12-20 18:04:00 +01:00
}
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
const res = new FolderResponse(decFolder);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
private async getCollection(id: string) {
let decCollection: CollectionView = null;
2019-10-01 17:04:15 +02:00
if (Utils.isGuid(id)) {
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
const collection = await this.collectionService.get(id);
2019-09-25 23:11:48 +02:00
if (collection != null) {
decCollection = await collection.decrypt();
}
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
} else if (id.trim() !== "") {
let collections = await this.collectionService.getAllDecrypted();
2019-09-25 23:11:48 +02:00
collections = CliUtils.searchCollections(collections, id);
if (collections.length > 1) {
return Response.multipleResults(collections.map((c) => c.id));
}
if (collections.length > 0) {
decCollection = collections[0];
}
}
2018-05-18 21:26:59 +02:00
if (decCollection == null) {
return Response.notFound();
}
const res = new CollectionResponse(decCollection);
return Response.success(res);
2018-05-18 21:26:59 +02:00
}
2022-01-19 16:45:14 +01:00
private async getOrganizationCollection(id: string, options: Options) {
if (options.organizationId == null || options.organizationId === "") {
return Response.badRequest("`organizationid` option is required.");
2021-12-20 18:04:00 +01:00
}
2018-05-18 21:26:59 +02:00
if (!Utils.isGuid(id)) {
2022-01-19 16:45:14 +01:00
return Response.badRequest("`" + id + "` is not a GUID.");
2018-05-18 21:26:59 +02:00
}
2022-01-19 16:45:14 +01:00
if (!Utils.isGuid(options.organizationId)) {
return Response.badRequest("`" + options.organizationId + "` is not a GUID.");
2021-12-20 18:04:00 +01:00
}
try {
2022-01-19 16:45:14 +01:00
const orgKey = await this.cryptoService.getOrgKey(options.organizationId);
2018-05-14 22:25:14 +02:00
if (orgKey == null) {
2018-05-15 18:18:47 +02:00
throw new Error("No encryption key for this organization.");
2021-12-20 18:04:00 +01:00
}
2022-01-19 16:45:14 +01:00
const response = await this.apiService.getCollectionDetails(options.organizationId, id);
2018-05-18 15:16:34 +02:00
const decCollection = new CollectionView(response);
2018-05-15 18:18:47 +02:00
decCollection.name = await this.cryptoService.decryptToUtf8(
new EncString(response.name),
2021-12-20 18:04:00 +01:00
orgKey
2018-05-15 18:18:47 +02:00
);
2018-05-15 18:53:08 +02:00
const groups =
response.groups == null
2018-10-23 23:31:59 +02:00
? null
: response.groups.map((g) => new SelectionReadOnly(g.id, g.readOnly, g.hidePasswords));
const res = new OrganizationCollectionResponse(decCollection, groups);
2018-05-18 21:26:59 +02:00
return Response.success(res);
} catch (e) {
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
return Response.error(e);
2021-12-20 18:04:00 +01:00
}
}
2018-10-23 23:31:59 +02:00
private async getOrganization(id: string) {
2019-09-25 22:08:56 +02:00
let org: Organization = null;
2019-10-01 17:04:15 +02:00
if (Utils.isGuid(id)) {
org = await this.organizationService.get(id);
2019-09-25 22:08:56 +02:00
} else if (id.trim() !== "") {
let orgs = await this.organizationService.getAll();
orgs = CliUtils.searchOrganizations(orgs, id);
if (orgs.length > 1) {
2018-05-14 23:13:57 +02:00
return Response.multipleResults(orgs.map((c) => c.id));
2021-12-20 18:04:00 +01:00
}
2018-05-18 21:26:59 +02:00
if (orgs.length > 0) {
2018-05-14 23:13:57 +02:00
org = orgs[0];
2021-12-20 18:04:00 +01:00
}
2018-05-14 22:25:14 +02:00
}
2018-05-15 05:40:11 +02:00
2018-11-16 16:02:22 +01:00
if (org == null) {
return Response.notFound();
2021-12-20 18:04:00 +01:00
}
2018-05-14 22:25:14 +02:00
const res = new OrganizationResponse(org);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-14 22:25:14 +02:00
private async getTemplate(id: string) {
let template: any = null;
switch (id.toLowerCase()) {
2021-12-20 18:04:00 +01:00
case "item":
template = CipherExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.field":
template = FieldExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.login":
template = LoginExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.login.uri":
template = LoginUriExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.card":
template = CardExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.identity":
template = IdentityExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-18 15:16:34 +02:00
case "item.securenote":
template = SecureNoteExport.template();
2021-12-20 18:04:00 +01:00
break;
2018-05-15 18:53:08 +02:00
case "folder":
template = FolderExport.template();
2021-12-20 18:04:00 +01:00
break;
2019-09-25 23:11:48 +02:00
case "collection":
template = CollectionExport.template();
2021-12-20 18:04:00 +01:00
break;
2019-09-25 23:11:48 +02:00
case "item-collections":
2018-05-16 17:54:59 +02:00
template = ["collection-id1", "collection-id2"];
2021-12-20 18:04:00 +01:00
break;
2019-09-25 23:11:48 +02:00
case "org-collection":
2018-05-15 18:53:08 +02:00
template = OrganizationCollectionRequest.template();
2021-12-20 18:04:00 +01:00
break;
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
case "send.text":
template = SendResponse.template(SendType.Text);
2021-12-20 18:04:00 +01:00
break;
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
case "send.file":
template = SendResponse.template(SendType.File);
2021-12-20 18:04:00 +01:00
break;
default:
2018-05-14 20:54:19 +02:00
return Response.badRequest("Unknown template object.");
2018-05-14 22:25:14 +02:00
}
2018-11-16 16:02:22 +01:00
const res = new TemplateResponse(template);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-11-16 16:02:22 +01:00
private async getFingerprint(id: string) {
let fingerprint: string[] = null;
if (id === "me") {
fingerprint = await this.cryptoService.getFingerprint(await this.stateService.getUserId());
2019-10-01 17:04:15 +02:00
} else if (Utils.isGuid(id)) {
2018-11-16 16:02:22 +01:00
try {
const response = await this.apiService.getUserPublicKey(id);
const pubKey = Utils.fromB64ToArray(response.publicKey);
fingerprint = await this.cryptoService.getFingerprint(id, pubKey.buffer);
2022-03-03 18:24:41 +01:00
} catch {
// eslint-disable-next-line
}
2018-11-16 16:02:22 +01:00
}
if (fingerprint == null) {
return Response.notFound();
}
const res = new StringResponse(fingerprint.join("-"));
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2018-05-14 19:37:52 +02:00
}
2022-01-19 16:45:14 +01:00
class Options {
itemId: string;
organizationId: string;
output: string;
constructor(passedOptions: Record<string, any>) {
this.organizationId = passedOptions?.organizationid || passedOptions?.organizationId;
this.itemId = passedOptions?.itemid || passedOptions?.itemId;
this.output = passedOptions?.output;
2022-01-19 16:45:14 +01:00
}
}