1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00

bulk share apis

This commit is contained in:
Kyle Spearrin 2018-06-13 00:02:15 -04:00
parent 7a5a4e654a
commit cfad521ea8
5 changed files with 68 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import { EnvironmentUrls } from '../models/domain/environmentUrls';
import { CipherBulkDeleteRequest } from '../models/request/cipherBulkDeleteRequest';
import { CipherBulkMoveRequest } from '../models/request/cipherBulkMoveRequest';
import { CipherBulkShareRequest } from '../models/request/cipherBulkShareRequest';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherShareRequest } from '../models/request/cipherShareRequest';
@ -44,6 +45,7 @@ export abstract class ApiService {
deleteManyCiphers: (request: CipherBulkDeleteRequest) => Promise<any>;
putMoveCiphers: (request: CipherBulkMoveRequest) => Promise<any>;
putShareCipher: (id: string, request: CipherShareRequest) => Promise<any>;
putShareCiphers: (request: CipherBulkShareRequest) => Promise<any>;
putCipherCollections: (id: string, request: CipherCollectionsRequest) => Promise<any>;
postCipherAttachment: (id: string, data: FormData) => Promise<CipherResponse>;
deleteCipherAttachment: (id: string, attachmentId: string) => Promise<any>;

View File

@ -26,7 +26,8 @@ export abstract class CipherService {
updateLastUsedDate: (id: string) => Promise<void>;
saveNeverDomain: (domain: string) => Promise<void>;
saveWithServer: (cipher: Cipher) => Promise<any>;
shareWithServer: (cipher: Cipher) => Promise<any>;
shareWithServer: (cipher: CipherView, organizationId: string, collectionIds: string[]) => Promise<any>;
shareManyWithServer: (ciphers: CipherView[], organizationId: string, collectionIds: string[]) => Promise<any>;
shareAttachmentWithServer: (attachmentView: AttachmentView, cipherId: string,
organizationId: string) => Promise<any>;
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any) => Promise<Cipher>;

View File

@ -0,0 +1,18 @@
import { CipherRequest } from './cipherRequest';
import { Cipher } from '../domain/cipher';
export class CipherBulkShareRequest {
ciphers: CipherRequest[];
collectionIds: string[];
constructor(ciphers: Cipher[], collectionIds: string[]) {
if (ciphers != null) {
this.ciphers = [];
ciphers.forEach((c) => {
this.ciphers.push(new CipherRequest(c));
});
}
this.collectionIds = collectionIds;
}
}

View File

@ -8,6 +8,7 @@ import { EnvironmentUrls } from '../models/domain/environmentUrls';
import { CipherBulkDeleteRequest } from '../models/request/cipherBulkDeleteRequest';
import { CipherBulkMoveRequest } from '../models/request/cipherBulkMoveRequest';
import { CipherBulkShareRequest } from '../models/request/cipherBulkShareRequest';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherShareRequest } from '../models/request/cipherShareRequest';
@ -419,6 +420,27 @@ export class ApiService implements ApiServiceAbstraction {
}
}
async putShareCiphers(request: CipherBulkShareRequest): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/share', {
body: JSON.stringify(request),
cache: 'no-cache',
credentials: this.getCredentials(),
headers: new Headers({
'Accept': 'application/json',
'Authorization': authHeader,
'Content-Type': 'application/json; charset=utf-8',
'Device-Type': this.deviceType,
}),
method: 'PUT',
}));
if (response.status !== 200) {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
async putCipherCollections(id: string, request: CipherCollectionsRequest): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/collections', {

View File

@ -17,6 +17,7 @@ import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
import { CipherBulkDeleteRequest } from '../models/request/cipherBulkDeleteRequest';
import { CipherBulkMoveRequest } from '../models/request/cipherBulkMoveRequest';
import { CipherBulkShareRequest } from '../models/request/cipherBulkShareRequest';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherShareRequest } from '../models/request/cipherShareRequest';
@ -358,11 +359,31 @@ export class CipherService implements CipherServiceAbstraction {
await this.upsert(data);
}
async shareWithServer(cipher: Cipher): Promise<any> {
const request = new CipherShareRequest(cipher);
async shareWithServer(cipher: CipherView, organizationId: string, collectionIds: string[]): Promise<any> {
cipher.organizationId = organizationId;
cipher.collectionIds = collectionIds;
const encCipher = await this.encrypt(cipher);
const request = new CipherShareRequest(encCipher);
await this.apiService.putShareCipher(cipher.id, request);
const userId = await this.userService.getUserId();
await this.upsert(cipher.toCipherData(userId));
await this.upsert(encCipher.toCipherData(userId));
}
async shareManyWithServer(ciphers: CipherView[], organizationId: string, collectionIds: string[]): Promise<any> {
const promises: Array<Promise<any>> = [];
const encCiphers: Cipher[] = [];
for (const cipher of ciphers) {
cipher.organizationId = organizationId;
cipher.collectionIds = collectionIds;
promises.push(this.encrypt(cipher).then((c) => {
encCiphers.push(c);
}));
}
await Promise.all(promises);
const request = new CipherBulkShareRequest(encCiphers, collectionIds);
await this.apiService.putShareCiphers(request);
const userId = await this.userService.getUserId();
await this.upsert(encCiphers.map((c) => c.toCipherData(userId)));
}
async shareAttachmentWithServer(attachmentView: AttachmentView, cipherId: string,