import { APIService } from './api' import { DataCreateResponse, DataResponse, GrantedLockboxes, LockboxDataRequest, LockboxGrantRequest, LockboxManifest, SharedSecretResponse, Uuid, EncryptedVaultIndex, IndexKey, EncryptedIndexEntry, Grant, LockboxCreateResponse } from '../models' export class VaultService { constructor(private api: APIService, private baseURL: string) { } public async lockboxCreate(): Promise { return this.api.post( `${this.baseURL}/v1/lockboxes` ) } public async lockboxMetadataAdd( lockboxUuid: Uuid, lockboxMetadata: Object, lockboxOwnerUuid?: Uuid ): Promise { return this.api.put( `${this.baseURL}/v1/lockbox/${lockboxUuid}`, lockboxMetadata, { params: { lockbox_owner_uuid: lockboxOwnerUuid } } ) } public async lockboxSecretGet( lockboxUuid: Uuid, lockboxOwnerUuid?: Uuid ): Promise { return this.api.get( `${this.baseURL}/v1/lockboxes/${lockboxUuid}/secret`, { params: { lockbox_owner_uuid: lockboxOwnerUuid } } ) } public async lockboxGrant( lockboxUuid: Uuid, req: LockboxGrantRequest, lockboxOwnerUuid?: Uuid ): Promise { return this.api.post( `${this.baseURL}/v1/lockboxes/${lockboxUuid}/grant`, req, { params: { lockbox_owner_uuid: lockboxOwnerUuid } } ) } /** * Get all lockboxes granted to user * @param filter filter of lockbox metadata * @returns decrypted lockboxes granted to user */ public async grantsGet(): Promise { return this.api.get(`${this.baseURL}/v1/grants`) } /** * This function create or update a data into the vault. * @note At creation it is necessary to have all `req` filled * @note When setting `previousDataUuid` you are updating the data. `req` metadata fields are optional. * @param lockboxUuid The lockbox uuid the data will be stored in * @param req The request (please see notes) * @param lockboxOwnerUuid The uuid of the owner of the lockbox (@deprecated) * @param previousDataUuid The data uuid of the data you want to update * @returns */ public async lockboxDataStore( lockboxUuid: Uuid, req: LockboxDataRequest, lockboxOwnerUuid?: Uuid, previousDataUuid?: Uuid ): Promise { return this.api.post( `${this.baseURL}/v1/lockboxes/${lockboxUuid}/data`, req, { params: { lockbox_owner_uuid: lockboxOwnerUuid, data_uuid: previousDataUuid, }, } ) } public async lockboxDataGet( lockboxUuid: Uuid, dataUuid: Uuid, lockboxOwnerUuid?: Uuid, stream: boolean = true ): Promise { let data = await this.api.get( `${this.baseURL}/v1/lockboxes/${lockboxUuid}/data/${dataUuid}`, { params: { lockbox_owner_uuid: lockboxOwnerUuid, stream } } ) // returned as stream, we need to put inside a DataResponse object if (stream) return { data } return data } public async lockboxManifestGet( lockboxUuid: Uuid, filter?: Object, lockboxOwnerUuid?: Uuid ): Promise { return this.api.get(`${this.baseURL}/v1/lockboxes/${lockboxUuid}`, { params: { lockbox_owner_uuid: lockboxOwnerUuid, filter }, }) } public async lockboxMetadataGet( lockboxUuid: Uuid, fields: string[], groupby: string[], filter?: Object, lockboxOwnerUuid?: Uuid ): Promise { return this.api.get(`${this.baseURL}/v1/lockboxes/${lockboxUuid}/metadata`, { params: { lockbox_owner_uuid: lockboxOwnerUuid, fields, groupby, filter }, }) } /** * inserts or updates encrypted index entries * @note if the index data is being inserted for a user other than the requester, use `indexOwnerUuid` * @note if a uuid for an entry is provided, the service will perform an update * @param entries the encrypted index data * @param indexOwnerUuid */ public async vaultIndexPut(entries: EncryptedVaultIndex, indexOwnerUuid?: Uuid): Promise { return this.api.put(`${this.baseURL}/v1/index`, entries, { params: { index_owner_uuid: indexOwnerUuid, }, } ) } /** * inserts or updates index snapshot for the provided index owner * @note if the index data is being inserted for a user other than the requester, use `indexOwnerUuid` * @param entry the encrypted index snapshot */ public async vaultIndexSnapshotPut(entry: EncryptedIndexEntry): Promise { return this.api.put(`${this.baseURL}/v1/index-snapshot`, entry) } /** * Retrieves the encrypted index from the vault for the requesting user * @note index keys can be specified to narrow the scope of index being requested * @param indexKeys accepted index fields determined by vault * @param identifiers: an array of unique_hashes or consultation uuids used to identify an index entry * @param timestamp the minimum timestamp that index entries were created * @returns the encrypted index */ public async vaultIndexGet(indexKeys: IndexKey[], identifiers?: string[], timestamp?: Date): Promise { return this.api.get(`${this.baseURL}/v1/index`, { params: { index_keys: indexKeys, identifiers, timestamp }, }) } }