Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
cipher collection apis
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jun 12, 2018
1 parent b3f71ed commit 1021f23
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/abstractions/api.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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>;
}
1 change: 1 addition & 0 deletions src/abstractions/cipher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
2 changes: 1 addition & 1 deletion src/models/request/cipherCollectionsRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export class CipherCollectionsRequest {
collectionIds: string[];

constructor(collectionIds: string[]) {
this.collectionIds = collectionIds;
this.collectionIds = collectionIds == null ? [] : collectionIds;
}
}
68 changes: 45 additions & 23 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand All @@ -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) {
Expand Down Expand Up @@ -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/' +
Expand All @@ -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> {
Expand Down
14 changes: 12 additions & 2 deletions src/services/cipher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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; }>(
Expand Down

0 comments on commit 1021f23

Please sign in to comment.