2021-12-20 18:04:00 +01:00
|
|
|
import { CipherType } from "jslib-common/enums/cipherType";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { AuditService } from "jslib-common/abstractions/audit.service";
|
|
|
|
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
|
|
|
import { CollectionService } from "jslib-common/abstractions/collection.service";
|
|
|
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
|
|
|
import { FolderService } from "jslib-common/abstractions/folder.service";
|
2021-12-28 21:38:51 +01:00
|
|
|
import { OrganizationService } from "jslib-common/abstractions/organization.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
import { SearchService } from "jslib-common/abstractions/search.service";
|
2021-12-28 21:38:51 +01:00
|
|
|
import { StateService } from "jslib-common/abstractions/state.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
import { TotpService } from "jslib-common/abstractions/totp.service";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { Organization } from "jslib-common/models/domain/organization";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { Card } from "jslib-common/models/export/card";
|
|
|
|
import { Cipher } from "jslib-common/models/export/cipher";
|
|
|
|
import { Collection } from "jslib-common/models/export/collection";
|
|
|
|
import { Field } from "jslib-common/models/export/field";
|
|
|
|
import { Folder } from "jslib-common/models/export/folder";
|
|
|
|
import { Identity } from "jslib-common/models/export/identity";
|
|
|
|
import { Login } from "jslib-common/models/export/login";
|
|
|
|
import { LoginUri } from "jslib-common/models/export/loginUri";
|
|
|
|
import { SecureNote } from "jslib-common/models/export/secureNote";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { CipherView } from "jslib-common/models/view/cipherView";
|
|
|
|
import { CollectionView } from "jslib-common/models/view/collectionView";
|
|
|
|
import { FolderView } from "jslib-common/models/view/folderView";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { EncString } from "jslib-common/models/domain/encString";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { ErrorResponse } from "jslib-common/models/response/errorResponse";
|
|
|
|
import { Response } from "jslib-node/cli/models/response";
|
|
|
|
import { StringResponse } from "jslib-node/cli/models/response/stringResponse";
|
2021-06-07 19:25:55 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { SendType } from "jslib-common/enums/sendType";
|
2021-02-26 19:47:11 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { CipherResponse } from "../models/response/cipherResponse";
|
|
|
|
import { CollectionResponse } from "../models/response/collectionResponse";
|
|
|
|
import { FolderResponse } from "../models/response/folderResponse";
|
|
|
|
import { OrganizationCollectionResponse } from "../models/response/organizationCollectionResponse";
|
|
|
|
import { OrganizationResponse } from "../models/response/organizationResponse";
|
|
|
|
import { SendResponse } from "../models/response/sendResponse";
|
|
|
|
import { TemplateResponse } from "../models/response/templateResponse";
|
2018-05-14 22:25:14 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { OrganizationCollectionRequest } from "../models/request/organizationCollectionRequest";
|
2019-09-25 22:08:56 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { SelectionReadOnly } from "../models/selectionReadOnly";
|
2019-09-25 23:11:48 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { DownloadCommand } from "./download.command";
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { CliUtils } from "../utils";
|
2018-05-16 17:54:59 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
2018-11-16 16:02:22 +01:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
export class GetCommand extends DownloadCommand {
|
2021-12-20 18:04:00 +01:00
|
|
|
constructor(
|
|
|
|
private cipherService: CipherService,
|
|
|
|
private folderService: FolderService,
|
|
|
|
private collectionService: CollectionService,
|
|
|
|
private totpService: TotpService,
|
|
|
|
private auditService: AuditService,
|
|
|
|
cryptoService: CryptoService,
|
2021-12-28 21:38:51 +01:00
|
|
|
private stateService: StateService,
|
2021-12-20 18:04:00 +01:00
|
|
|
private searchService: SearchService,
|
|
|
|
private apiService: ApiService,
|
2021-12-28 21:38:51 +01:00
|
|
|
private organizationService: OrganizationService
|
2021-12-20 18:04:00 +01:00
|
|
|
) {
|
|
|
|
super(cryptoService);
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
|
2021-12-20 18:04:00 +01:00
|
|
|
if (id != null) {
|
|
|
|
id = id.toLowerCase();
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
const normalizedOptions = new Options(cmdOptions);
|
2021-12-20 18:04:00 +01:00
|
|
|
switch (object.toLowerCase()) {
|
|
|
|
case "item":
|
|
|
|
return await this.getCipher(id);
|
|
|
|
case "username":
|
|
|
|
return await this.getUsername(id);
|
|
|
|
case "password":
|
|
|
|
return await this.getPassword(id);
|
|
|
|
case "uri":
|
|
|
|
return await this.getUri(id);
|
|
|
|
case "totp":
|
|
|
|
return await this.getTotp(id);
|
|
|
|
case "notes":
|
|
|
|
return await this.getNotes(id);
|
|
|
|
case "exposed":
|
|
|
|
return await this.getExposed(id);
|
|
|
|
case "attachment":
|
2022-01-19 16:45:14 +01:00
|
|
|
return await this.getAttachment(id, normalizedOptions);
|
2021-12-20 18:04:00 +01:00
|
|
|
case "folder":
|
|
|
|
return await this.getFolder(id);
|
|
|
|
case "collection":
|
|
|
|
return await this.getCollection(id);
|
|
|
|
case "org-collection":
|
2022-01-19 16:45:14 +01:00
|
|
|
return await this.getOrganizationCollection(id, normalizedOptions);
|
2021-12-20 18:04:00 +01:00
|
|
|
case "organization":
|
|
|
|
return await this.getOrganization(id);
|
|
|
|
case "template":
|
|
|
|
return await this.getTemplate(id);
|
|
|
|
case "fingerprint":
|
|
|
|
return await this.getFingerprint(id);
|
|
|
|
default:
|
|
|
|
return Response.badRequest("Unknown object.");
|
2018-12-08 03:55:32 +01:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getCipherView(id: string): Promise<CipherView | CipherView[]> {
|
|
|
|
let decCipher: CipherView = null;
|
|
|
|
if (Utils.isGuid(id)) {
|
|
|
|
const cipher = await this.cipherService.get(id);
|
|
|
|
if (cipher != null) {
|
|
|
|
decCipher = await cipher.decrypt();
|
|
|
|
}
|
|
|
|
} else if (id.trim() !== "") {
|
|
|
|
let ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
ciphers = this.searchService.searchCiphersBasic(ciphers, id);
|
|
|
|
if (ciphers.length > 1) {
|
|
|
|
return ciphers;
|
|
|
|
}
|
|
|
|
if (ciphers.length > 0) {
|
|
|
|
decCipher = ciphers[0];
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
return decCipher;
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
private async getCipher(id: string, filter?: (c: CipherView) => boolean) {
|
|
|
|
let decCipher = await this.getCipherView(id);
|
|
|
|
if (decCipher == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
|
|
|
if (Array.isArray(decCipher)) {
|
|
|
|
if (filter != null) {
|
|
|
|
decCipher = decCipher.filter(filter);
|
|
|
|
if (decCipher.length === 1) {
|
|
|
|
decCipher = decCipher[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Array.isArray(decCipher)) {
|
|
|
|
return Response.multipleResults(decCipher.map((c) => c.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const res = new CipherResponse(decCipher);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getUsername(id: string) {
|
|
|
|
const cipherResponse = await this.getCipher(
|
|
|
|
id,
|
|
|
|
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.username)
|
|
|
|
);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
2018-05-16 19:59:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
|
|
|
if (cipher.type !== CipherType.Login) {
|
|
|
|
return Response.badRequest("Not a login.");
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (Utils.isNullOrWhitespace(cipher.login.username)) {
|
|
|
|
return Response.error("No username available for this login.");
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(cipher.login.username);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getPassword(id: string) {
|
|
|
|
const cipherResponse = await this.getCipher(
|
|
|
|
id,
|
|
|
|
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.password)
|
|
|
|
);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
2018-05-16 19:59:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
|
|
|
if (cipher.type !== CipherType.Login) {
|
|
|
|
return Response.badRequest("Not a login.");
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (Utils.isNullOrWhitespace(cipher.login.password)) {
|
|
|
|
return Response.error("No password available for this login.");
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(cipher.login.password);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getUri(id: string) {
|
|
|
|
const cipherResponse = await this.getCipher(
|
|
|
|
id,
|
|
|
|
(c) =>
|
|
|
|
c.type === CipherType.Login &&
|
|
|
|
c.login.uris != null &&
|
|
|
|
c.login.uris.length > 0 &&
|
|
|
|
c.login.uris[0].uri !== ""
|
|
|
|
);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
|
|
|
}
|
2018-05-16 19:59:08 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
|
|
|
if (cipher.type !== CipherType.Login) {
|
|
|
|
return Response.badRequest("Not a login.");
|
2018-05-16 19:59:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (
|
|
|
|
cipher.login.uris == null ||
|
|
|
|
cipher.login.uris.length === 0 ||
|
|
|
|
cipher.login.uris[0].uri === ""
|
|
|
|
) {
|
|
|
|
return Response.error("No uri available for this login.");
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(cipher.login.uris[0].uri);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getTotp(id: string) {
|
|
|
|
const cipherResponse = await this.getCipher(
|
|
|
|
id,
|
|
|
|
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.totp)
|
|
|
|
);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
|
|
|
if (cipher.type !== CipherType.Login) {
|
|
|
|
return Response.badRequest("Not a login.");
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (Utils.isNullOrWhitespace(cipher.login.totp)) {
|
|
|
|
return Response.error("No TOTP available for this login.");
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const totp = await this.totpService.getCode(cipher.login.totp);
|
|
|
|
if (totp == null) {
|
|
|
|
return Response.error("Couldn't generate TOTP code.");
|
|
|
|
}
|
2018-05-18 16:55:50 +02:00
|
|
|
|
2021-12-28 21:38:51 +01:00
|
|
|
const canAccessPremium = await this.stateService.getCanAccessPremium();
|
2021-12-20 18:04:00 +01:00
|
|
|
if (!canAccessPremium) {
|
|
|
|
const originalCipher = await this.cipherService.get(cipher.id);
|
|
|
|
if (
|
|
|
|
originalCipher == null ||
|
|
|
|
originalCipher.organizationId == null ||
|
|
|
|
!originalCipher.organizationUseTotp
|
|
|
|
) {
|
|
|
|
return Response.error("Premium status is required to use this feature.");
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(totp);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2021-05-12 04:19:47 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
private async getNotes(id: string) {
|
|
|
|
const cipherResponse = await this.getCipher(id, (c) => !Utils.isNullOrWhitespace(c.notes));
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
|
|
|
}
|
2021-05-12 04:19:47 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
|
|
|
if (Utils.isNullOrWhitespace(cipher.notes)) {
|
|
|
|
return Response.error("No notes available for this item.");
|
2021-05-12 04:19:47 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(cipher.notes);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2018-05-17 17:07:53 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
private async getExposed(id: string) {
|
|
|
|
const passwordResponse = await this.getPassword(id);
|
|
|
|
if (!passwordResponse.success) {
|
|
|
|
return passwordResponse;
|
2018-05-17 17:07:53 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const exposedNumber = await this.auditService.passwordLeaked(
|
|
|
|
(passwordResponse.data as StringResponse).data
|
|
|
|
);
|
|
|
|
const res = new StringResponse(exposedNumber.toString());
|
|
|
|
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 === "") {
|
2021-12-20 18:04:00 +01:00
|
|
|
return Response.badRequest("--itemid <itemid> required.");
|
|
|
|
}
|
2018-05-17 19:28:22 +02:00
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
const itemId = options.itemId.toLowerCase();
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipherResponse = await this.getCipher(itemId);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
|
|
|
}
|
2021-02-03 19:06:39 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const cipher = await this.getCipherView(itemId);
|
|
|
|
if (
|
|
|
|
cipher == null ||
|
|
|
|
Array.isArray(cipher) ||
|
|
|
|
cipher.attachments == null ||
|
|
|
|
cipher.attachments.length === 0
|
|
|
|
) {
|
|
|
|
return Response.error("No attachments available for this item.");
|
|
|
|
}
|
2021-02-03 19:06:39 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
let attachments = cipher.attachments.filter(
|
|
|
|
(a) =>
|
|
|
|
a.id.toLowerCase() === id ||
|
|
|
|
(a.fileName != null && a.fileName.toLowerCase().indexOf(id) > -1)
|
|
|
|
);
|
|
|
|
if (attachments.length === 0) {
|
|
|
|
return Response.error("Attachment `" + id + "` was not found.");
|
|
|
|
}
|
2018-05-17 19:28:22 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const exactMatches = attachments.filter((a) => a.fileName.toLowerCase() === id);
|
|
|
|
if (exactMatches.length === 1) {
|
|
|
|
attachments = exactMatches;
|
|
|
|
}
|
2018-05-18 16:55:50 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (attachments.length > 1) {
|
|
|
|
return Response.multipleResults(attachments.map((a) => a.id));
|
|
|
|
}
|
2021-04-29 16:49:04 +02:00
|
|
|
|
2021-12-28 21:38:51 +01:00
|
|
|
if (!(await this.stateService.getCanAccessPremium())) {
|
2021-12-20 18:04:00 +01: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-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
let url: string;
|
|
|
|
try {
|
|
|
|
const attachmentDownloadResponse = await this.apiService.getAttachmentData(
|
|
|
|
cipher.id,
|
|
|
|
attachments[0].id
|
|
|
|
);
|
|
|
|
url = attachmentDownloadResponse.url;
|
|
|
|
} catch (e) {
|
|
|
|
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());
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const key =
|
|
|
|
attachments[0].key != null
|
|
|
|
? attachments[0].key
|
|
|
|
: await this.cryptoService.getOrgKey(cipher.organizationId);
|
|
|
|
return await this.saveAttachmentToFile(url, key, attachments[0].fileName, options.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getFolder(id: string) {
|
|
|
|
let decFolder: FolderView = null;
|
|
|
|
if (Utils.isGuid(id)) {
|
|
|
|
const folder = await this.folderService.get(id);
|
|
|
|
if (folder != null) {
|
|
|
|
decFolder = await folder.decrypt();
|
|
|
|
}
|
|
|
|
} else if (id.trim() !== "") {
|
|
|
|
let folders = await this.folderService.getAllDecrypted();
|
|
|
|
folders = CliUtils.searchFolders(folders, id);
|
|
|
|
if (folders.length > 1) {
|
|
|
|
return Response.multipleResults(folders.map((f) => f.id));
|
|
|
|
}
|
|
|
|
if (folders.length > 0) {
|
|
|
|
decFolder = folders[0];
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
2018-05-14 22:25:14 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (decFolder == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
|
|
|
const res = new FolderResponse(decFolder);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getCollection(id: string) {
|
|
|
|
let decCollection: CollectionView = null;
|
|
|
|
if (Utils.isGuid(id)) {
|
|
|
|
const collection = await this.collectionService.get(id);
|
|
|
|
if (collection != null) {
|
|
|
|
decCollection = await collection.decrypt();
|
|
|
|
}
|
|
|
|
} else if (id.trim() !== "") {
|
|
|
|
let collections = await this.collectionService.getAllDecrypted();
|
|
|
|
collections = CliUtils.searchCollections(collections, id);
|
|
|
|
if (collections.length > 1) {
|
|
|
|
return Response.multipleResults(collections.map((c) => c.id));
|
|
|
|
}
|
|
|
|
if (collections.length > 0) {
|
|
|
|
decCollection = collections[0];
|
|
|
|
}
|
2019-09-25 23:11:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 18:04:00 +01: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
|
|
|
}
|
|
|
|
if (!Utils.isGuid(id)) {
|
2022-01-19 16:45:14 +01:00
|
|
|
return Response.badRequest("`" + id + "` is not a GUID.");
|
2021-12-20 18:04:00 +01: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);
|
2021-12-20 18:04:00 +01:00
|
|
|
if (orgKey == null) {
|
|
|
|
throw new Error("No encryption key for this organization.");
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
const response = await this.apiService.getCollectionDetails(options.organizationId, id);
|
2021-12-20 18:04:00 +01:00
|
|
|
const decCollection = new CollectionView(response);
|
|
|
|
decCollection.name = await this.cryptoService.decryptToUtf8(
|
|
|
|
new EncString(response.name),
|
|
|
|
orgKey
|
|
|
|
);
|
|
|
|
const groups =
|
|
|
|
response.groups == null
|
|
|
|
? null
|
|
|
|
: response.groups.map((g) => new SelectionReadOnly(g.id, g.readOnly, g.hidePasswords));
|
|
|
|
const res = new OrganizationCollectionResponse(decCollection, groups);
|
|
|
|
return Response.success(res);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getOrganization(id: string) {
|
|
|
|
let org: Organization = null;
|
|
|
|
if (Utils.isGuid(id)) {
|
2021-12-28 21:38:51 +01:00
|
|
|
org = await this.organizationService.get(id);
|
2021-12-20 18:04:00 +01:00
|
|
|
} else if (id.trim() !== "") {
|
2021-12-28 21:38:51 +01:00
|
|
|
let orgs = await this.organizationService.getAll();
|
2021-12-20 18:04:00 +01:00
|
|
|
orgs = CliUtils.searchOrganizations(orgs, id);
|
|
|
|
if (orgs.length > 1) {
|
|
|
|
return Response.multipleResults(orgs.map((c) => c.id));
|
|
|
|
}
|
|
|
|
if (orgs.length > 0) {
|
|
|
|
org = orgs[0];
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 05:40:11 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (org == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
|
|
|
const res = new OrganizationResponse(org);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getTemplate(id: string) {
|
|
|
|
let template: any = null;
|
|
|
|
switch (id.toLowerCase()) {
|
|
|
|
case "item":
|
|
|
|
template = Cipher.template();
|
|
|
|
break;
|
|
|
|
case "item.field":
|
|
|
|
template = Field.template();
|
|
|
|
break;
|
|
|
|
case "item.login":
|
|
|
|
template = Login.template();
|
|
|
|
break;
|
|
|
|
case "item.login.uri":
|
|
|
|
template = LoginUri.template();
|
|
|
|
break;
|
|
|
|
case "item.card":
|
|
|
|
template = Card.template();
|
|
|
|
break;
|
|
|
|
case "item.identity":
|
|
|
|
template = Identity.template();
|
|
|
|
break;
|
|
|
|
case "item.securenote":
|
|
|
|
template = SecureNote.template();
|
|
|
|
break;
|
|
|
|
case "folder":
|
|
|
|
template = Folder.template();
|
|
|
|
break;
|
|
|
|
case "collection":
|
|
|
|
template = Collection.template();
|
|
|
|
break;
|
|
|
|
case "item-collections":
|
|
|
|
template = ["collection-id1", "collection-id2"];
|
|
|
|
break;
|
|
|
|
case "org-collection":
|
|
|
|
template = OrganizationCollectionRequest.template();
|
|
|
|
break;
|
|
|
|
case "send.text":
|
|
|
|
template = SendResponse.template(SendType.Text);
|
|
|
|
break;
|
|
|
|
case "send.file":
|
|
|
|
template = SendResponse.template(SendType.File);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Response.badRequest("Unknown template object.");
|
2018-05-14 22:25:14 +02:00
|
|
|
}
|
2018-11-16 16:02:22 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new TemplateResponse(template);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getFingerprint(id: string) {
|
|
|
|
let fingerprint: string[] = null;
|
|
|
|
if (id === "me") {
|
2021-12-28 21:38:51 +01:00
|
|
|
fingerprint = await this.cryptoService.getFingerprint(await this.stateService.getUserId());
|
2021-12-20 18:04:00 +01:00
|
|
|
} else if (Utils.isGuid(id)) {
|
|
|
|
try {
|
|
|
|
const response = await this.apiService.getUserPublicKey(id);
|
|
|
|
const pubKey = Utils.fromB64ToArray(response.publicKey);
|
|
|
|
fingerprint = await this.cryptoService.getFingerprint(id, pubKey.buffer);
|
|
|
|
} catch {}
|
|
|
|
}
|
2018-11-16 16:02:22 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (fingerprint == null) {
|
|
|
|
return Response.notFound();
|
2018-11-16 16:02:22 +01:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
const res = new StringResponse(fingerprint.join("-"));
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|