cipher collection apis

This commit is contained in:
Kyle Spearrin 2018-06-12 13:07:06 -04:00
parent b3f71ed8e4
commit 1021f23277
5 changed files with 64 additions and 28 deletions

View File

@ -1,5 +1,6 @@
import { EnvironmentUrls } from '../models/domain/environmentUrls';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherShareRequest } from '../models/request/cipherShareRequest';
import { FolderRequest } from '../models/request/folderRequest';
@ -37,11 +38,13 @@ export abstract class ApiService {
deleteFolder: (id: string) => Promise<any>;
postCipher: (request: CipherRequest) => Promise<CipherResponse>;
putCipher: (id: string, request: CipherRequest) => Promise<CipherResponse>;
shareCipher: (id: string, request: CipherShareRequest) => Promise<any>;
deleteCipher: (id: string) => Promise<any>;
putShareCipher: (id: string, request: CipherShareRequest) => Promise<any>;
putCipherCollections: (id: string, request: CipherCollectionsRequest) => Promise<any>;
postCipherAttachment: (id: string, data: FormData) => Promise<CipherResponse>;
shareCipherAttachment: (id: string, attachmentId: string, data: FormData, organizationId: string) => Promise<any>;
deleteCipherAttachment: (id: string, attachmentId: string) => Promise<any>;
postShareCipherAttachment: (id: string, attachmentId: string, data: FormData,
organizationId: string) => Promise<any>;
getSync: () => Promise<SyncResponse>;
postImportDirectory: (organizationId: string, request: ImportDirectoryRequest) => Promise<any>;
}

View File

@ -31,6 +31,7 @@ export abstract class CipherService {
organizationId: string) => Promise<any>;
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any) => Promise<Cipher>;
saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer) => Promise<Cipher>;
saveCollectionsWithServer: (cipher: Cipher) => Promise<any>;
upsert: (cipher: CipherData | CipherData[]) => Promise<any>;
replace: (ciphers: { [id: string]: CipherData; }) => Promise<any>;
clear: (userId: string) => Promise<any>;

View File

@ -2,6 +2,6 @@ export class CipherCollectionsRequest {
collectionIds: string[];
constructor(collectionIds: string[]) {
this.collectionIds = collectionIds;
this.collectionIds = collectionIds == null ? [] : collectionIds;
}
}

View File

@ -6,6 +6,7 @@ import { TokenService } from '../abstractions/token.service';
import { EnvironmentUrls } from '../models/domain/environmentUrls';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherShareRequest } from '../models/request/cipherShareRequest';
import { FolderRequest } from '../models/request/folderRequest';
@ -335,7 +336,25 @@ export class ApiService implements ApiServiceAbstraction {
}
}
async shareCipher(id: string, request: CipherShareRequest): Promise<any> {
async deleteCipher(id: string): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id, {
cache: 'no-cache',
credentials: this.getCredentials(),
headers: new Headers({
'Authorization': authHeader,
'Device-Type': this.deviceType,
}),
method: 'DELETE',
}));
if (response.status !== 200) {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
async putShareCipher(id: string, request: CipherShareRequest): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/share', {
body: JSON.stringify(request),
@ -356,16 +375,19 @@ export class ApiService implements ApiServiceAbstraction {
}
}
async deleteCipher(id: string): Promise<any> {
async putCipherCollections(id: string, request: CipherCollectionsRequest): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id, {
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/collections', {
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: 'DELETE',
method: 'PUT',
}));
if (response.status !== 200) {
@ -399,7 +421,25 @@ export class ApiService implements ApiServiceAbstraction {
}
}
async shareCipherAttachment(id: string, attachmentId: string, data: FormData,
async deleteCipherAttachment(id: string, attachmentId: string): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/attachment/' + attachmentId, {
cache: 'no-cache',
credentials: this.getCredentials(),
headers: new Headers({
'Authorization': authHeader,
'Device-Type': this.deviceType,
}),
method: 'DELETE',
}));
if (response.status !== 200) {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
async postShareCipherAttachment(id: string, attachmentId: string, data: FormData,
organizationId: string): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/attachment/' +
@ -421,24 +461,6 @@ export class ApiService implements ApiServiceAbstraction {
}
}
async deleteCipherAttachment(id: string, attachmentId: string): Promise<any> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/attachment/' + attachmentId, {
cache: 'no-cache',
credentials: this.getCredentials(),
headers: new Headers({
'Authorization': authHeader,
'Device-Type': this.deviceType,
}),
method: 'DELETE',
}));
if (response.status !== 200) {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
// Sync APIs
async getSync(): Promise<SyncResponse> {

View File

@ -15,6 +15,7 @@ import { LoginUri } from '../models/domain/loginUri';
import { SecureNote } from '../models/domain/secureNote';
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherResponse } from '../models/response/cipherResponse';
@ -357,7 +358,7 @@ export class CipherService implements CipherServiceAbstraction {
async shareWithServer(cipher: Cipher): Promise<any> {
const request = new CipherShareRequest(cipher);
await this.apiService.shareCipher(cipher.id, request);
await this.apiService.putShareCipher(cipher.id, request);
const userId = await this.userService.getUserId();
await this.upsert(cipher.toCipherData(userId));
}
@ -392,7 +393,8 @@ export class CipherService implements CipherServiceAbstraction {
let response: CipherResponse;
try {
response = await this.apiService.shareCipherAttachment(cipherId, attachmentView.id, fd, organizationId);
response = await this.apiService.postShareCipherAttachment(cipherId, attachmentView.id, fd,
organizationId);
} catch (e) {
throw new Error((e as ErrorResponse).getSingleMessage());
}
@ -450,6 +452,14 @@ export class CipherService implements CipherServiceAbstraction {
return new Cipher(cData);
}
async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
const request = new CipherCollectionsRequest(cipher.collectionIds);
const response = await this.apiService.putCipherCollections(cipher.id, request);
const userId = await this.userService.getUserId();
const data = cipher.toCipherData(userId);
await this.upsert(data);
}
async upsert(cipher: CipherData | CipherData[]): Promise<any> {
const userId = await this.userService.getUserId();
let ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(