|
1 | 1 | import {Vault} from "../proto"; |
| 2 | +import {platform, Platform} from "../platform"; |
| 3 | +import {Auth} from "../auth"; |
| 4 | + |
| 5 | +type RecordFieldData = { |
| 6 | + type: string, |
| 7 | + value: string[] |
| 8 | +} |
| 9 | + |
| 10 | +type DecryptedRecordData = { |
| 11 | + title: string |
| 12 | + fields?: RecordFieldData[] |
| 13 | +} |
| 14 | + |
| 15 | +type DecryptedSecurityScoreDataData = { |
| 16 | + padding: string, |
| 17 | + password: string, |
| 18 | + score: number, |
| 19 | + version: number, |
| 20 | +} |
2 | 21 |
|
3 | 22 | export class SyncDownResponseBuilder { |
4 | 23 | private readonly data: Vault.ISyncDownResponse; |
| 24 | + private readonly platform: Platform |
| 25 | + private readonly auth: Auth |
5 | 26 |
|
6 | | - constructor() { |
| 27 | + constructor(platform: Platform, auth: Auth) { |
| 28 | + this.platform = platform |
| 29 | + this.auth = auth |
7 | 30 | this.data = { |
8 | 31 | continuationToken: new Uint8Array([]), |
9 | 32 | users: [], |
@@ -39,34 +62,74 @@ export class SyncDownResponseBuilder { |
39 | 62 | } |
40 | 63 | } |
41 | 64 |
|
42 | | - addSecurityScoreData(securityScoreData: Vault.ISecurityScoreData) { |
43 | | - this.data.securityScoreData?.push(securityScoreData) |
44 | | - return this |
45 | | - } |
46 | | - |
47 | | - addBreachWatchSecurityData(breachWatchSecurityData: Vault.IBreachWatchSecurityData) { |
48 | | - this.data.breachWatchSecurityData?.push(breachWatchSecurityData) |
49 | | - return this |
50 | | - } |
51 | | - |
52 | | - addUserFolderRecord(userFolderRecord: Vault.IUserFolderRecord) { |
53 | | - this.data.userFolderRecords?.push(userFolderRecord) |
54 | | - return this |
| 65 | + addUserFolderRecord(recordUid: Uint8Array, folderUid?: Uint8Array) { |
| 66 | + this.data.userFolderRecords?.push({recordUid, folderUid, revision: Date.now()}) |
55 | 67 | } |
56 | 68 |
|
57 | 69 | addRecordMetadata(recordMetadata: Vault.IRecordMetaData) { |
58 | 70 | this.data.recordMetaData?.push(recordMetadata) |
59 | | - return this |
60 | 71 | } |
61 | 72 |
|
62 | | - addRecord(record: Vault.IRecord) { |
| 73 | + async addRecord(decryptedRecordData: DecryptedRecordData) { |
| 74 | + const decryptedRecordKey = this.platform.getRandomBytes(32) |
| 75 | + const recordKey = await this.platform.aesGcmEncrypt(decryptedRecordKey, this.auth.dataKey!) |
| 76 | + const recordUid = this.platform.getRandomBytes(16) |
| 77 | + const decodedRecordData = this.platform.stringToBytes(JSON.stringify(decryptedRecordData)) |
| 78 | + const recordData = await this.platform.aesGcmEncrypt(decodedRecordData, decryptedRecordKey) |
| 79 | + const record: Vault.IRecord = { |
| 80 | + recordUid, |
| 81 | + version: 3, |
| 82 | + data: recordData, |
| 83 | + extra: new Uint8Array([]), |
| 84 | + revision: Date.now(), |
| 85 | + } |
63 | 86 | this.data.records?.push(record) |
64 | | - return this |
| 87 | + |
| 88 | + const passwordField = decryptedRecordData.fields?.find(data => data.type === 'password') |
| 89 | + const passwordFieldValue = passwordField?.value ? passwordField.value[0] : undefined |
| 90 | + let decryptedSecurityScoreDataData: DecryptedSecurityScoreDataData | undefined; |
| 91 | + |
| 92 | + // add breach watch / security score data if a password field value presents |
| 93 | + if (!!passwordFieldValue) { |
| 94 | + this.data.breachWatchSecurityData?.push({ |
| 95 | + recordUid, |
| 96 | + revision: record.revision, |
| 97 | + }) |
| 98 | + decryptedSecurityScoreDataData = { |
| 99 | + padding: '', |
| 100 | + password: passwordFieldValue, |
| 101 | + score: 1, |
| 102 | + version: 1, |
| 103 | + } |
| 104 | + this.data.securityScoreData?.push({ |
| 105 | + recordUid, |
| 106 | + data: await platform.aesGcmEncrypt(platform.stringToBytes(JSON.stringify(decryptedSecurityScoreDataData)), decryptedRecordKey), |
| 107 | + revision: record.revision, |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + recordKey, |
| 113 | + recordUid, |
| 114 | + record, |
| 115 | + decryptedSecurityScoreDataData |
| 116 | + } |
65 | 117 | } |
66 | 118 |
|
67 | 119 | addRemovedRecord(recordUid: Uint8Array) { |
68 | 120 | this.data.removedRecords?.push(recordUid) |
69 | | - return this |
| 121 | + } |
| 122 | + |
| 123 | + addUserFolder(userFolder: Vault.IUserFolder) { |
| 124 | + this.data.userFolders?.push(userFolder) |
| 125 | + } |
| 126 | + |
| 127 | + addRemovedUserFolder(userFolderId: Uint8Array) { |
| 128 | + this.data.removedUserFolders?.push(userFolderId) |
| 129 | + } |
| 130 | + |
| 131 | + addRemovedUserFolderRecord(recordUid: Uint8Array, folderUid: Uint8Array) { |
| 132 | + this.data.removedUserFolderRecords?.push({recordUid, folderUid}) |
70 | 133 | } |
71 | 134 |
|
72 | 135 | build() { |
|
0 commit comments