Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 132 additions & 6 deletions keeperapi/src/__tests__/SyncDownResponseBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Vault} from "../proto";
import {Records, Vault} from "../proto";
import {platform, Platform} from "../platform";
import {Auth} from "../auth";

Expand All @@ -19,6 +19,24 @@ type DecryptedSecurityScoreDataData = {
version: number,
}

type DecryptedSharedFolderFolderData = {
name: string // folder name
}

type DecryptedSharedFolderData = {
name: string
}

type UserInfo = {
username: string
accountUid: Uint8Array
}

type SharedFolderPermissionData = Pick<
Vault.ISharedFolder,
"defaultCanEdit" | "defaultCanReshare" | "defaultManageUsers" | "defaultManageRecords"
>

export class SyncDownResponseBuilder {
private readonly data: Vault.ISyncDownResponse;
private readonly platform: Platform
Expand Down Expand Up @@ -62,17 +80,17 @@ export class SyncDownResponseBuilder {
}
}

addUserFolderRecord(recordUid: Uint8Array, folderUid?: Uint8Array) {
this.data.userFolderRecords?.push({recordUid, folderUid, revision: Date.now()})
addUserFolderRecord(userFolderRecord: Vault.IUserFolderRecord) {
this.data.userFolderRecords?.push(userFolderRecord)
}

addRecordMetadata(recordMetadata: Vault.IRecordMetaData) {
this.data.recordMetaData?.push(recordMetadata)
}

async addRecord(decryptedRecordData: DecryptedRecordData) {
async addRecord(decryptedRecordData: DecryptedRecordData, encryptionKey?: Uint8Array) {
const decryptedRecordKey = this.platform.getRandomBytes(32)
const recordKey = await this.platform.aesGcmEncrypt(decryptedRecordKey, this.auth.dataKey!)
const recordKey = await this.platform.aesGcmEncrypt(decryptedRecordKey, encryptionKey ? encryptionKey : this.auth.dataKey!)
const recordUid = this.platform.getRandomBytes(16)
const decodedRecordData = this.platform.stringToBytes(JSON.stringify(decryptedRecordData))
const recordData = await this.platform.aesGcmEncrypt(decodedRecordData, decryptedRecordKey)
Expand Down Expand Up @@ -112,7 +130,8 @@ export class SyncDownResponseBuilder {
recordKey,
recordUid,
record,
decryptedSecurityScoreDataData
decryptedSecurityScoreDataData,
decryptedRecordKey,
}
}

Expand All @@ -132,6 +151,113 @@ export class SyncDownResponseBuilder {
this.data.removedUserFolderRecords?.push({recordUid, folderUid})
}

async addSharedFolder(
decryptedSharedFolderData: DecryptedSharedFolderData,
userInfo: UserInfo,
permissionData: SharedFolderPermissionData,
options?: {
encryptionKey?: Uint8Array
parentFolderUid?: Uint8Array
},
) {
const sharedFolderUid = platform.getRandomBytes(16)
const decryptedSharedFolderKey = platform.getRandomBytes(32)
let sharedFolderKey: Uint8Array
if (!options?.encryptionKey) {
sharedFolderKey = await platform.aesCbcEncrypt(decryptedSharedFolderKey, options?.encryptionKey ? options?.encryptionKey : this.auth.dataKey!, true)
} else {// normally when a shared folder is shared to a team
sharedFolderKey = platform.publicEncrypt(decryptedSharedFolderKey, platform.bytesToBase64(options.encryptionKey))
}
const sharedFolder: Vault.ISharedFolder = {
sharedFolderUid,
sharedFolderKey,
owner: userInfo.username,
ownerAccountUid: userInfo.accountUid,
keyType: options?.encryptionKey ? Records.RecordKeyType.NO_KEY : Records.RecordKeyType.ENCRYPTED_BY_DATA_KEY,
revision: Date.now(),
name: await platform.aesCbcEncrypt(platform.stringToBytes(decryptedSharedFolderData.name), decryptedSharedFolderKey, true),
data: await platform.aesCbcEncrypt(platform.stringToBytes(JSON.stringify(decryptedSharedFolderData)), decryptedSharedFolderKey, true),
...permissionData,
}

this.data.sharedFolders?.push(sharedFolder)
this.data.userFolderSharedFolders?.push({
sharedFolderUid,
revision: sharedFolder.revision,
folderUid: options?.parentFolderUid ? options?.parentFolderUid : new Uint8Array([]),
})

return {sharedFolderUid, sharedFolder, sharedFolderKey, decryptedSharedFolderKey}
}

addSharedFolderUser(sharedFolderUser: Vault.ISharedFolderUser) {
this.data.sharedFolderUsers?.push(sharedFolderUser)
}

addRemovedSharedFolder(sharedFolderUid: Uint8Array) {
this.data.removedSharedFolders?.push(sharedFolderUid)
}

addRemovedSharedFolderTeam(sharedFolderTeam: Vault.ISharedFolderTeam) {
this.data.removedSharedFolderTeams?.push(sharedFolderTeam)
}

addSharedFolderTeam(sharedFolderTeam: Vault.ISharedFolderTeam) {
this.data.sharedFolderTeams?.push(sharedFolderTeam)
}

addTeam(team: Vault.ITeam) {
this.data.teams?.push(team)
}

addSharedFolderRecord(sharedFolderRecord: Vault.ISharedFolderRecord) {
this.data.sharedFolderRecords?.push(sharedFolderRecord)
}

addRemovedSharedFolderRecord(sharedFolderRecord: Vault.ISharedFolderRecord) {
this.data.removedSharedFolderRecords?.push(sharedFolderRecord)
}

async addSharedFolderFolder(
decryptedSharedFolderFolderData: DecryptedSharedFolderFolderData,
sharedFolderUid: Uint8Array,
decryptedSharedFolderKey: Uint8Array,
parentUid: Uint8Array = new Uint8Array([])
) {
const sharedFolderFolderUid = platform.getRandomBytes(16)
const decryptedSharedFolderFolderKey = platform.getRandomBytes(32)
const sharedFolderFolderKey = await platform.aesCbcEncrypt(decryptedSharedFolderFolderKey, decryptedSharedFolderKey, true)
const sharedFolderFolder: Vault.ISharedFolderFolder = {
sharedFolderUid,
folderUid: sharedFolderFolderUid,
sharedFolderFolderKey,
keyType: Records.RecordKeyType.ENCRYPTED_BY_DATA_KEY,
revision: Date.now(),
data: await platform.aesCbcEncrypt(platform.stringToBytes(JSON.stringify(decryptedSharedFolderFolderData)), decryptedSharedFolderFolderKey, true),
// either empty or the parent shared folder's uid if the folder is the direct child of the shared folder (level 0)
parentUid,
}

this.data.sharedFolderFolders?.push(sharedFolderFolder)

return {
sharedFolderFolderUid,
sharedFolderFolder,
}
}

addRemovedSharedFolderFolder(removedSharedFolderFolder: Vault.ISharedFolderFolder) {
this.data.removedSharedFolderFolders?.push(removedSharedFolderFolder)
}

addSharedFolderFolderRecord(sharedFolderFolderRecord: Vault.ISharedFolderFolderRecord) {
this.data.sharedFolderFolderRecords?.push(sharedFolderFolderRecord)
}

addRemovedSharedFolderFolderRecord(sharedFolderFolderRecord: Vault.ISharedFolderFolderRecord) {
this.data.removedSharedFolderFolderRecords?.push(sharedFolderFolderRecord)
}

build() {
return this.data
}
Expand Down
Loading