Skip to content

Commit 9cfd552

Browse files
committed
feat: Add existing file encryption, decryption and reecryption
These methods are useful when moving an existing file to a different location, with a different encryption context.
1 parent 8264009 commit 9cfd552

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"cozy-authentication": "2.8.3",
117117
"cozy-bar": "7.17.7",
118118
"cozy-ci": "0.4.1",
119-
"cozy-client": "^29.0.1",
119+
"cozy-client": "^29.1.0",
120120
"cozy-client-js": "0.19.0",
121121
"cozy-device-helper": "^2.1.0",
122122
"cozy-doctypes": "1.82.2",
@@ -130,7 +130,7 @@
130130
"cozy-scanner": "2.0.2",
131131
"cozy-scripts": "6.3.1",
132132
"cozy-sharing": "4.1.5",
133-
"cozy-stack-client": "^29.0.0",
133+
"cozy-stack-client": "^29.1.0",
134134
"cozy-ui": "^67.0.2",
135135
"date-fns": "1.30.1",
136136
"diacritics": "1.3.0",

src/drive/lib/encryption.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export const createEncryptedDir = async (
6060
export const encryptAndUploadNewFile = async (
6161
client,
6262
vaultClient,
63-
{ file, encryptionKey, fileOptions }
63+
{ binary, encryptionKey, fileOptions }
6464
) => {
6565
const { name, dirID, onUploadProgress } = fileOptions
66-
const encryptedFile = await vaultClient.encryptFile(file, encryptionKey)
66+
const encryptedFile = await vaultClient.encryptFile(binary, encryptionKey)
6767
const resp = await client
6868
.collection(DOCTYPE_FILES)
6969
.createFile(encryptedFile, {
@@ -75,6 +75,69 @@ export const encryptAndUploadNewFile = async (
7575
return resp.data
7676
}
7777

78+
export const encryptAndUploadExistingFile = async (
79+
client,
80+
vaultClient,
81+
{ file, encryptionKey }
82+
) => {
83+
const encryptedFile = await encryptFile(client, vaultClient, {
84+
file,
85+
encryptionKey
86+
})
87+
88+
const attributes = {
89+
...file,
90+
encrypted: true,
91+
fileId: file._id,
92+
data: encryptedFile
93+
}
94+
const resp = await client.save(attributes)
95+
return resp.data
96+
}
97+
98+
export const decryptAndUploadExistingFile = async (
99+
client,
100+
vaultClient,
101+
{ file, decryptionKey }
102+
) => {
103+
const plaintextFile = await decryptFile(client, vaultClient, {
104+
file,
105+
decryptionKey
106+
})
107+
108+
const attributes = {
109+
...file,
110+
encrypted: false,
111+
fileId: file._id,
112+
data: plaintextFile
113+
}
114+
const resp = await client.save(attributes)
115+
return resp.data
116+
}
117+
118+
export const reencryptAndUploadExistingFile = async (
119+
client,
120+
vaultClient,
121+
{ file, encryptionKey, decryptionKey }
122+
) => {
123+
const plaintextFile = await decryptFile(client, vaultClient, {
124+
file,
125+
decryptionKey
126+
})
127+
const reencryptedFile = await vaultClient.encryptFile(
128+
plaintextFile,
129+
encryptionKey
130+
)
131+
const attributes = {
132+
...file,
133+
encrypted: true,
134+
fileId: file._id,
135+
data: reencryptedFile
136+
}
137+
const resp = await client.save(attributes)
138+
return resp.data
139+
}
140+
78141
const getBinaryFile = async (client, fileId) => {
79142
const resp = await client
80143
.collection(DOCTYPE_FILES)

src/drive/web/modules/upload/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ const uploadFile = async (client, file, dirID, options = {}) => {
337337
const fr = new FileReader()
338338
fr.onloadend = async () => {
339339
return encryptAndUploadNewFile(client, vaultClient, {
340-
file: fr.result,
340+
binary: fr.result,
341341
encryptionKey,
342342
fileOptions: {
343343
name: file.name,

yarn.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5993,6 +5993,30 @@ cozy-client@^29.0.1:
59935993
sift "^6.0.0"
59945994
url-search-params-polyfill "^8.0.0"
59955995

5996+
cozy-client@^29.1.0:
5997+
version "29.1.0"
5998+
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-29.1.0.tgz#2852b260d0d619f4d106606d0464bf1ea7733cfa"
5999+
integrity sha512-CyPYEgzI0MrsEUWTLFFJS/Z4Fi9BieWFLTXYYzahTPATKoCuA1vfDIHWtMjuFVBDkikNghxbYZ463AD3IJHJpQ==
6000+
dependencies:
6001+
"@cozy/minilog" "1.0.0"
6002+
"@types/jest" "^26.0.20"
6003+
"@types/lodash" "^4.14.170"
6004+
btoa "^1.2.1"
6005+
cozy-stack-client "^29.1.0"
6006+
json-stable-stringify "^1.0.1"
6007+
lodash "^4.17.13"
6008+
microee "^0.0.6"
6009+
node-fetch "^2.6.1"
6010+
node-polyglot "2.4.2"
6011+
open "7.4.2"
6012+
prop-types "^15.6.2"
6013+
react-redux "^7.2.0"
6014+
redux "3 || 4"
6015+
redux-thunk "^2.3.0"
6016+
server-destroy "^1.0.1"
6017+
sift "^6.0.0"
6018+
url-search-params-polyfill "^8.0.0"
6019+
59966020
cozy-device-helper@^1.12.0, cozy-device-helper@^1.13.2, cozy-device-helper@^1.16.1, cozy-device-helper@^1.17.0, cozy-device-helper@^1.7.5:
59976021
version "1.17.0"
59986022
resolved "https://registry.yarnpkg.com/cozy-device-helper/-/cozy-device-helper-1.17.0.tgz#fbce9737ea83c67969b2b173163b37299a36283c"
@@ -6249,6 +6273,15 @@ cozy-stack-client@^29.0.0:
62496273
mime "^2.4.0"
62506274
qs "^6.7.0"
62516275

6276+
cozy-stack-client@^29.1.0:
6277+
version "29.1.0"
6278+
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-29.1.0.tgz#a101e164b45088560df828ff39e369cd9e5a5466"
6279+
integrity sha512-KxLTdGhqNFDX4TpstzuyROROTGkvvMy4sSiRS5yHir8hfudCuME6pyRIKTslTlMx/y7vjYqcInaEnrwlZP7zaA==
6280+
dependencies:
6281+
detect-node "^2.0.4"
6282+
mime "^2.4.0"
6283+
qs "^6.7.0"
6284+
62526285
62536286
version "22.3.1"
62546287
resolved "https://registry.yarnpkg.com/cozy-ui/-/cozy-ui-22.3.1.tgz#9bd22099255e88ab2a9694a8b18d21cde3f627f5"

0 commit comments

Comments
 (0)