orgnote-api 0.17.1 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/dist/api.d.ts +112 -0
  2. package/dist/api.js +1 -0
  3. package/dist/encryption/__tests__/encryption-keys.d.ts +3 -0
  4. package/{encryption/__tests__/encryption-keys.ts → dist/encryption/__tests__/encryption-keys.js} +0 -2
  5. package/dist/encryption/__tests__/encryption.spec.d.ts +1 -0
  6. package/dist/encryption/__tests__/encryption.spec.js +287 -0
  7. package/dist/encryption/__tests__/note-encryption.spec.d.ts +1 -0
  8. package/dist/encryption/__tests__/note-encryption.spec.js +364 -0
  9. package/dist/encryption/encryption.d.ts +27 -0
  10. package/dist/encryption/encryption.js +156 -0
  11. package/dist/encryption/index.js +2 -0
  12. package/dist/encryption/note-encryption.d.ts +12 -0
  13. package/dist/encryption/note-encryption.js +42 -0
  14. package/dist/files-api.d.ts +7 -0
  15. package/dist/files-api.js +22 -0
  16. package/dist/index.js +5 -0
  17. package/dist/models/command.d.ts +29 -0
  18. package/dist/models/command.js +1 -0
  19. package/dist/models/completion.d.ts +22 -0
  20. package/dist/models/completion.js +1 -0
  21. package/dist/models/default-commands.d.ts +30 -0
  22. package/dist/models/default-commands.js +41 -0
  23. package/dist/models/editor.d.ts +23 -0
  24. package/dist/models/editor.js +1 -0
  25. package/dist/models/encryption.d.ts +31 -0
  26. package/dist/models/encryption.js +1 -0
  27. package/dist/models/extension.d.ts +34 -0
  28. package/dist/models/extension.js +1 -0
  29. package/dist/models/file-system.d.ts +23 -0
  30. package/dist/models/file-system.js +1 -0
  31. package/dist/models/index.js +13 -0
  32. package/dist/models/modal.d.ts +10 -0
  33. package/dist/models/modal.js +1 -0
  34. package/dist/models/note.d.ts +23 -0
  35. package/dist/models/note.js +1 -0
  36. package/dist/models/sync.d.ts +34 -0
  37. package/dist/models/sync.js +1 -0
  38. package/dist/models/theme-variables.d.ts +192 -0
  39. package/dist/models/theme-variables.js +194 -0
  40. package/{models/vue-component.ts → dist/models/vue-component.d.ts} +1 -1
  41. package/dist/models/vue-component.js +1 -0
  42. package/dist/models/widget-type.d.ts +5 -0
  43. package/dist/models/widget-type.js +6 -0
  44. package/dist/models/widget.d.ts +51 -0
  45. package/dist/models/widget.js +1 -0
  46. package/dist/remote-api/api.d.ts +1525 -0
  47. package/{remote-api/api.ts → dist/remote-api/api.js} +246 -1149
  48. package/dist/remote-api/base.d.ts +66 -0
  49. package/{remote-api/base.ts → dist/remote-api/base.js} +13 -36
  50. package/dist/remote-api/common.d.ts +65 -0
  51. package/{remote-api/common.ts → dist/remote-api/common.js} +31 -48
  52. package/{remote-api/configuration.ts → dist/remote-api/configuration.d.ts} +3 -22
  53. package/dist/remote-api/configuration.js +95 -0
  54. package/dist/remote-api/index.d.ts +13 -0
  55. package/{remote-api/index.ts → dist/remote-api/index.js} +0 -3
  56. package/dist/tools/__tests__/find-files-diff.spec.d.ts +1 -0
  57. package/dist/tools/__tests__/find-files-diff.spec.js +110 -0
  58. package/dist/tools/__tests__/find-note-files-diff.spec.d.ts +1 -0
  59. package/dist/tools/__tests__/find-note-files-diff.spec.js +168 -0
  60. package/dist/tools/__tests__/get-file-name.spec.d.ts +1 -0
  61. package/{tools/__tests__/get-file-name.spec.ts → dist/tools/__tests__/get-file-name.spec.js} +4 -8
  62. package/dist/tools/__tests__/get-string-path.spec.d.ts +1 -0
  63. package/dist/tools/__tests__/get-string-path.spec.js +27 -0
  64. package/dist/tools/__tests__/is-gpg-encrypted.spec.d.ts +1 -0
  65. package/{tools/__tests__/is-gpg-encrypted.spec.ts → dist/tools/__tests__/is-gpg-encrypted.spec.js} +4 -8
  66. package/dist/tools/__tests__/is-org-file.spec.d.ts +1 -0
  67. package/dist/tools/__tests__/is-org-file.spec.js +54 -0
  68. package/dist/tools/__tests__/join.spec.d.ts +1 -0
  69. package/dist/tools/__tests__/join.spec.js +14 -0
  70. package/dist/tools/__tests__/read-org-files-recursively.spec.d.ts +1 -0
  71. package/dist/tools/__tests__/read-org-files-recursively.spec.js +51 -0
  72. package/dist/tools/extend-notes-files-diff.d.ts +3 -0
  73. package/dist/tools/extend-notes-files-diff.js +21 -0
  74. package/dist/tools/find-notes-files-diff.d.ts +5 -0
  75. package/dist/tools/find-notes-files-diff.js +108 -0
  76. package/dist/tools/get-file-name.d.ts +2 -0
  77. package/dist/tools/get-file-name.js +6 -0
  78. package/dist/tools/get-string-path.d.ts +1 -0
  79. package/dist/tools/get-string-path.js +6 -0
  80. package/dist/tools/index.js +6 -0
  81. package/dist/tools/is-gpg-encrypted.d.ts +1 -0
  82. package/dist/tools/is-gpg-encrypted.js +6 -0
  83. package/dist/tools/is-org-file.d.ts +2 -0
  84. package/dist/tools/is-org-file.js +4 -0
  85. package/dist/tools/join.d.ts +1 -0
  86. package/dist/tools/join.js +3 -0
  87. package/dist/tools/mock-server.d.ts +1 -0
  88. package/dist/tools/mock-server.js +12 -0
  89. package/package.json +6 -14
  90. package/api.ts +0 -123
  91. package/encryption/__tests__/__snapshots__/note-encryption.spec.ts.snap +0 -215
  92. package/encryption/__tests__/encryption.spec.ts +0 -355
  93. package/encryption/__tests__/note-encryption.spec.ts +0 -424
  94. package/encryption/encryption.ts +0 -265
  95. package/encryption/note-encryption.ts +0 -78
  96. package/files-api.ts +0 -25
  97. package/models/command.ts +0 -45
  98. package/models/completion.ts +0 -30
  99. package/models/default-commands.ts +0 -44
  100. package/models/editor.ts +0 -27
  101. package/models/encryption.ts +0 -54
  102. package/models/extension.ts +0 -45
  103. package/models/file-system.ts +0 -30
  104. package/models/modal.ts +0 -12
  105. package/models/note.ts +0 -26
  106. package/models/sync.ts +0 -43
  107. package/models/theme-variables.ts +0 -194
  108. package/models/widget-type.ts +0 -5
  109. package/models/widget.ts +0 -59
  110. package/remote-api/.gitignore +0 -4
  111. package/remote-api/.npmignore +0 -1
  112. package/remote-api/.openapi-generator/FILES +0 -8
  113. package/remote-api/.openapi-generator/VERSION +0 -1
  114. package/remote-api/.openapi-generator-ignore +0 -23
  115. package/remote-api/git_push.sh +0 -57
  116. package/tools/__tests__/__snapshots__/join.spec.ts.snap +0 -9
  117. package/tools/__tests__/find-files-diff.spec.ts +0 -177
  118. package/tools/__tests__/find-note-files-diff.spec.ts +0 -207
  119. package/tools/__tests__/get-string-path.spec.ts +0 -32
  120. package/tools/__tests__/is-org-file.spec.ts +0 -62
  121. package/tools/__tests__/join.spec.ts +0 -16
  122. package/tools/__tests__/read-org-files-recursively.spec.ts +0 -61
  123. package/tools/extend-notes-files-diff.ts +0 -35
  124. package/tools/find-notes-files-diff.ts +0 -185
  125. package/tools/get-file-name.ts +0 -7
  126. package/tools/get-string-path.ts +0 -6
  127. package/tools/is-gpg-encrypted.ts +0 -6
  128. package/tools/is-org-file.ts +0 -7
  129. package/tools/join.ts +0 -3
  130. package/tools/mock-server.ts +0 -16
  131. /package/{encryption/index.ts → dist/encryption/index.d.ts} +0 -0
  132. /package/{index.ts → dist/index.d.ts} +0 -0
  133. /package/{models/index.ts → dist/models/index.d.ts} +0 -0
  134. /package/{tools/index.ts → dist/tools/index.d.ts} +0 -0
@@ -1,424 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import { decryptNote, encryptNote } from '../note-encryption';
3
- import { Note } from '../../models';
4
- import {
5
- armoredPublicKey,
6
- armoredPrivateKey,
7
- privateKeyPassphrase,
8
- } from './encryption-keys';
9
- import { ModelsPublicNoteEncryptionTypeEnum } from '../../remote-api';
10
-
11
- test('Should encrypt note content via password', async () => {
12
- const noteText = `#+ID: qweqwe
13
-
14
- #+TITLE: Hello worlld
15
-
16
- * Hello?`;
17
-
18
- const note: Note = {
19
- id: 'id',
20
- meta: {
21
- title: 'My note title',
22
- images: [],
23
- published: false,
24
- description: 'Awesome description',
25
- },
26
- author: {
27
- id: '1',
28
- name: 'John Doe',
29
- email: 'test@mail.com',
30
- },
31
- };
32
-
33
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
34
- content: noteText,
35
- type: 'gpgPassword',
36
- password: '123',
37
- format: 'armored',
38
- });
39
-
40
- expect(encryptedNoteText.startsWith('-----BEGIN PGP MESSAGE-----')).toBe(
41
- true
42
- );
43
- expect(encryptedNote).toMatchSnapshot();
44
- });
45
-
46
- test('Should encrypt note content via password to binary format', async () => {
47
- const noteText = `#+ID: qweqwe
48
-
49
- #+TITLE: Hello worlld
50
-
51
- * Hello?`;
52
-
53
- const note: Note = {
54
- id: 'id',
55
- meta: {
56
- title: 'My note title',
57
- images: [],
58
- published: false,
59
- description: 'Awesome description',
60
- },
61
- author: {
62
- id: '1',
63
- name: 'John Doe',
64
- email: 'test@mail.com',
65
- },
66
- };
67
-
68
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
69
- content: noteText,
70
- type: 'gpgPassword',
71
- password: '123',
72
- format: 'binary',
73
- });
74
-
75
- expect(encryptedNote).toMatchSnapshot();
76
- expect(encryptedNoteText).toBeInstanceOf(Uint8Array);
77
- });
78
-
79
- test('Should decrypt note content via password', async () => {
80
- const noteText = `-----BEGIN PGP MESSAGE-----
81
-
82
- wy4ECQMI/CCaKMJEqy/gyROJeRgW9I738dDFBltFlxIjhxrN7nQ6gkX4GgX6
83
- zt3v0mABvaBJA7corlU8su21TpPApOs/+DMWpVlbI3Zer7QfQK1fSBoSTbCR
84
- 2k3g68Afayke2nLkDNkH62tdOPiTkx7bSlp1zL4uU440IM1g6dC72JkmtoTJ
85
- 5Bjlwt4ZhxFsh+c=
86
- =csID
87
- -----END PGP MESSAGE-----`;
88
-
89
- const note: Note = {
90
- id: 'id',
91
- meta: {
92
- title: 'My note title',
93
- images: [],
94
- published: false,
95
- description: 'Awesome description',
96
- },
97
- };
98
-
99
- const decryptedNote = await decryptNote(note, {
100
- content: noteText,
101
- type: 'gpgPassword',
102
- password: '123',
103
- });
104
-
105
- expect(decryptedNote).toMatchSnapshot();
106
- });
107
-
108
- test('Should encrypt note via keys', async () => {
109
- const noteText = `#+ID: qweqwe
110
- #+TITLE: Hello worlld
111
-
112
- * Hello?`;
113
-
114
- const note: Note = {
115
- id: 'id',
116
- meta: {
117
- title: 'My note title for encryption via keys',
118
- images: [],
119
- published: false,
120
- description: 'Awesome description',
121
- },
122
- author: {
123
- id: '1',
124
- name: 'John Doe',
125
- email: 'test@mail.com',
126
- },
127
- };
128
-
129
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
130
- content: noteText,
131
- type: ModelsPublicNoteEncryptionTypeEnum.GpgKeys,
132
- publicKey: armoredPublicKey,
133
- privateKey: armoredPrivateKey,
134
- privateKeyPassphrase,
135
- format: 'armored',
136
- });
137
-
138
- expect(encryptedNoteText.startsWith('-----BEGIN PGP MESSAGE-----')).toBe(
139
- true
140
- );
141
- expect(encryptedNote).toMatchSnapshot();
142
- });
143
-
144
- test('Should decrypt note via provided keys', async () => {
145
- const encryptedNoteText = `-----BEGIN PGP MESSAGE-----
146
-
147
- wcFMA/vryg+TTn0rAQ//TBFRjKmjRQoLhSrgeH+NbsZXbxvo7Ey4k+BQ9XA5
148
- +CMpXH9uFUxsSaI5+McUSEt32VI17HRpXQDCL9nwaWqWOanMaRe0tXXhtox2
149
- gfe2f/6zsge9ux+mXF2BG4z+V5T8XIOrfzxosVprdJHZHM3x7cW5USQ0t2i2
150
- FiOUWxSZO1899J3yICpMvhDXvTLVZuKpSNQho5PyXSeZa83eN+uYkhgt9lsk
151
- 0KW88Nr435S6n6mVw/zpitXIgEKpkqh46mhG+1W3aC0lYx6j9lHm3bjtdb4r
152
- 2mtZbWKwfdBggEy8qEeiUeslvKd6uWtEccomzFgJkaqWVGknLmrBdHfztRIV
153
- fbZhxHz+J3GFOIgXf/5+fv+zg0nTazgz1mDUfnTHw3+qcAyEJ0ADdyg9EZc+
154
- bKbK0GwzMDPQeM+cCRDWUTiIa1ruyEETiwMdhWUDztF8XxY2o8jXPerZ0NhG
155
- R8l+SvAdYQQXPfxEh9lA3thkyz/Vp72pW46lUeJHGSq/fS6KIdLHLP9Z2e1J
156
- aCFpNMgyAN+BaXwnbLZfz6k5hV8awbRScSWQLEg69D9b287SFDPOYW7OZikn
157
- CKXL7xyQ5LrWJZN9Z/UPGjy+PdEw1SBhyluW6DQ+Sz1j4K8USTLqY89EInDl
158
- G7AxHGujR9UkUGyUvvc71XW7jEOogiDgn1ZMoj9Y58LSweUB8vfkY3VF84Fy
159
- zGVvLFWnFbtnWuoQC4OOXv1F6ETdmEsSMEMWKzRLDGPyqNX7FhG3cejlEuiK
160
- EsT5oQhz00RECmnR8mJkGmJhzWmmeoQvaucFqhTOZjvYl9ivuEMBZ2jtkdHn
161
- R6UNcNZRpCbfnl7YoikqbGBgqDFYydFGHXKHDpYhcQYWJsMDUayzUiPtzmaE
162
- tgfTgNNqgPxkLnWA99KYNU8DH+FwgaYBuw9dwdqwcjxSbZjHhCFGUfsqM+ik
163
- O9gcoAwVO1usODlESU7OWuSF2tIv5DBG5rhlSyxBp4d4aWmaumTswAdojy2O
164
- sM70ETbg0mC8niW9lNsgJp55oFmlksvzjUIit5rBEr55rtPcJSgakPpR2yvd
165
- Q4XjybUmV0IdONMhk/OlqaBPtnA0RG+qTcm7oP1qH/m5zRA0ZplVQ5ylkQwr
166
- LwGq5JQpJkgxxgLIrUbbtzYBShXSr5c1XXR0LIRiNgtb6s1s4mt+fbyExdJF
167
- +ceuu+/xYrW/YTuEJpHxLiZ+aNPW5g5Y7Hbqu3hp3UL/kD44cc8JjZh18spX
168
- p/ncojDhF2r2vhR+CndYcMkpGMV+t1pGKC8wlcFc7lb9GJASnqMGvhQmvIaG
169
- gO/x7UjuTZSBW+kITHHZJDqYryKUv1j0CkHxIn9tWsYOpa1giOFtXX5v0AAM
170
- AJR/s/beLlqwCsUdYnw1TkP/0u0ZK3RPio1nJ7S6ckPfsM7DqCWD8ILD8Cdr
171
- cuzQrWaE30t5PXx53xBPO+6t5wKfDL35WHWG1Irmvz9UuT7tDS3IzwtF4ijF
172
- PX6alTbxGnoHgZ4bG4J1wfpTNPppP1gJeVg67VqOypzdZi+SjofMWnFgRFmD
173
- yEN8xpFUs7A9xryVZOosp9Sfe3IbBkO99sAQ7jV4EoMYk3/GKA==
174
- =LjkG
175
- -----END PGP MESSAGE-----`;
176
-
177
- const note: Note = {
178
- id: 'id',
179
- meta: {
180
- title: 'My note title for decryption via keys',
181
- images: [],
182
- published: false,
183
- description: 'Awesome description',
184
- },
185
- };
186
-
187
- const decryptedNote = await decryptNote(note, {
188
- content: encryptedNoteText,
189
- type: 'gpgKeys',
190
- publicKey: armoredPublicKey,
191
- privateKey: armoredPrivateKey,
192
- privateKeyPassphrase,
193
- });
194
-
195
- expect(decryptedNote).toMatchSnapshot();
196
- });
197
-
198
- test('Should not encrypt public note', async () => {
199
- const noteText = `#+ID: qweqwe
200
- #+TITLE: Hello worlld
201
-
202
- * Hello?`;
203
-
204
- const note: Note = {
205
- id: 'id',
206
- meta: {
207
- title: 'My note title',
208
- images: [],
209
- published: true,
210
- description: 'Awesome description',
211
- },
212
- author: {
213
- id: '1',
214
- name: 'John Doe',
215
- email: 'test@mail.com',
216
- },
217
- };
218
-
219
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
220
- content: noteText,
221
- type: 'gpgPassword',
222
- password: '123',
223
- format: 'armored',
224
- });
225
-
226
- expect(encryptedNoteText.startsWith('-----BEGIN PGP MESSAGE-----')).toBe(
227
- false
228
- );
229
- expect(encryptedNote).toMatchSnapshot();
230
- });
231
-
232
- test('Should encrypt note with empty encrypted property', async () => {
233
- const noteText = `#+ID: qweqwe
234
- #+TITLE: Hello worlld
235
-
236
- * Hello?`;
237
-
238
- const note: Note = {
239
- id: 'id',
240
- meta: {
241
- title: 'My note title',
242
- images: [],
243
- published: false,
244
- description: 'Awesome description',
245
- },
246
- author: {
247
- id: '1',
248
- name: 'John Doe',
249
- email: 'test@mail.com',
250
- },
251
- };
252
-
253
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
254
- content: noteText,
255
- type: 'gpgPassword',
256
- password: '123',
257
- format: 'armored',
258
- });
259
-
260
- expect(encryptedNoteText.startsWith('-----BEGIN PGP MESSAGE-----')).toBe(
261
- true
262
- );
263
-
264
- expect(encryptedNote).toMatchSnapshot();
265
- });
266
-
267
- test('Should not decrypt note without provided encrypted type', async () => {
268
- const noteText = `#+ID: qweqwe
269
- #+TITLE: Hello worlld
270
-
271
- * Hello?`;
272
- const note: Note = {
273
- id: 'id',
274
- meta: {
275
- title: 'My note title',
276
- images: [],
277
- published: false,
278
- description: 'Awesome description',
279
- },
280
- encrypted: undefined,
281
- author: {
282
- id: '1',
283
- name: 'John Doe',
284
- email: 'test@mail.com',
285
- },
286
- };
287
-
288
- const decryptedInfo = await decryptNote(note, {
289
- content: noteText,
290
- type: 'gpgPassword',
291
- password: '123',
292
- });
293
-
294
- expect(decryptedInfo).toMatchSnapshot();
295
- });
296
-
297
- test('Should decrypt note and note meta', async () => {
298
- const meta = {
299
- title: 'My note title',
300
- images: ['./image1.png'],
301
- published: false,
302
- description: 'Awesome description',
303
- fileTags: ['tag1', 'tag2'],
304
- };
305
- const note: Note = {
306
- id: 'id',
307
- meta: { ...meta },
308
- };
309
-
310
- const noteText = `#+TITLE: My note title
311
- #+DESCRIPTION: Awesome description
312
- #+PUBLISHED: false
313
- #+FILETAGS: :tag1:tag2:
314
-
315
- [[./image1.png]]
316
-
317
- Hello world`;
318
-
319
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
320
- content: noteText,
321
- type: 'gpgPassword',
322
- password: '123',
323
- });
324
-
325
- const [decryptedNote, decryptedNoteText] = await decryptNote(encryptedNote, {
326
- content: encryptedNoteText,
327
- type: 'gpgPassword',
328
- password: '123',
329
- });
330
-
331
- expect(decryptedNote.meta).toEqual(meta);
332
- expect(decryptedNoteText).toEqual(noteText);
333
- });
334
-
335
- test('Should set not encrypted status when params type does not provided', async () => {
336
- const note: Note = {
337
- id: 'id',
338
- meta: {
339
- title: 'My note title for decryption via keys',
340
- images: [],
341
- published: false,
342
- description: 'Awesome description',
343
- },
344
- };
345
-
346
- const noteText = `-----BEGIN PGP MESSAGE-----
347
-
348
- wcFMA/vryg+TTn0rAQ//TBFRjKmjRQoLhSrgeH+NbsZXbxvo7Ey4k+BQ9XA5
349
- +CMpXH9uFUxsSaI5+McUSEt32VI17HRpXQDCL9nwaWqWOanMaRe0tXXhtox2
350
- gfe2f/6zsge9ux+mXF2BG4z+V5T8XIOrfzxosVprdJHZHM3x7cW5USQ0t2i2
351
- FiOUWxSZO1899J3yICpMvhDXvTLVZuKpSNQho5PyXSeZa83eN+uYkhgt9lsk
352
- 0KW88Nr435S6n6mVw/zpitXIgEKpkqh46mhG+1W3aC0lYx6j9lHm3bjtdb4r
353
- 2mtZbWKwfdBggEy8qEeiUeslvKd6uWtEccomzFgJkaqWVGknLmrBdHfztRIV
354
- fbZhxHz+J3GFOIgXf/5+fv+zg0nTazgz1mDUfnTHw3+qcAyEJ0ADdyg9EZc+
355
- bKbK0GwzMDPQeM+cCRDWUTiIa1ruyEETiwMdhWUDztF8XxY2o8jXPerZ0NhG
356
- R8l+SvAdYQQXPfxEh9lA3thkyz/Vp72pW46lUeJHGSq/fS6KIdLHLP9Z2e1J
357
- aCFpNMgyAN+BaXwnbLZfz6k5hV8awbRScSWQLEg69D9b287SFDPOYW7OZikn
358
- CKXL7xyQ5LrWJZN9Z/UPGjy+PdEw1SBhyluW6DQ+Sz1j4K8USTLqY89EInDl
359
- G7AxHGujR9UkUGyUvvc71XW7jEOogiDgn1ZMoj9Y58LSweUB8vfkY3VF84Fy
360
- zGVvLFWnFbtnWuoQC4OOXv1F6ETdmEsSMEMWKzRLDGPyqNX7FhG3cejlEuiK
361
- EsT5oQhz00RECmnR8mJkGmJhzWmmeoQvaucFqhTOZjvYl9ivuEMBZ2jtkdHn
362
- R6UNcNZRpCbfnl7YoikqbGBgqDFYydFGHXKHDpYhcQYWJsMDUayzUiPtzmaE
363
- tgfTgNNqgPxkLnWA99KYNU8DH+FwgaYBuw9dwdqwcjxSbZjHhCFGUfsqM+ik
364
- O9gcoAwVO1usODlESU7OWuSF2tIv5DBG5rhlSyxBp4d4aWmaumTswAdojy2O
365
- sM70ETbg0mC8niW9lNsgJp55oFmlksvzjUIit5rBEr55rtPcJSgakPpR2yvd
366
- Q4XjybUmV0IdONMhk/OlqaBPtnA0RG+qTcm7oP1qH/m5zRA0ZplVQ5ylkQwr
367
- LwGq5JQpJkgxxgLIrUbbtzYBShXSr5c1XXR0LIRiNgtb6s1s4mt+fbyExdJF
368
- +ceuu+/xYrW/YTuEJpHxLiZ+aNPW5g5Y7Hbqu3hp3UL/kD44cc8JjZh18spX
369
- p/ncojDhF2r2vhR+CndYcMkpGMV+t1pGKC8wlcFc7lb9GJASnqMGvhQmvIaG
370
- gO/x7UjuTZSBW+kITHHZJDqYryKUv1j0CkHxIn9tWsYOpa1giOFtXX5v0AAM
371
- AJR/s/beLlqwCsUdYnw1TkP/0u0ZK3RPio1nJ7S6ckPfsM7DqCWD8ILD8Cdr
372
- cuzQrWaE30t5PXx53xBPO+6t5wKfDL35WHWG1Irmvz9UuT7tDS3IzwtF4ijF
373
- PX6alTbxGnoHgZ4bG4J1wfpTNPppP1gJeVg67VqOypzdZi+SjofMWnFgRFmD
374
- yEN8xpFUs7A9xryVZOosp9Sfe3IbBkO99sAQ7jV4EoMYk3/GKA==
375
- =LjkG
376
- -----END PGP MESSAGE-----`;
377
-
378
- const decryptedNote = await decryptNote(note, {
379
- content: noteText,
380
- type: 'disabled',
381
- });
382
-
383
- expect(decryptedNote).toMatchSnapshot();
384
- });
385
-
386
- test('Should encrypt note to binary format', async () => {
387
- const noteText = `#+ID: qweqwe
388
- #+TITLE: Hello worlld
389
-
390
- * Hello?`;
391
-
392
- const note: Note = {
393
- id: 'id',
394
- meta: {
395
- title: 'My note title',
396
- images: [],
397
- published: false,
398
- description: 'Awesome description',
399
- },
400
- author: {
401
- id: '1',
402
- name: 'John Doe',
403
- email: 'test@mail.com',
404
- },
405
- };
406
-
407
- const [encryptedNote, encryptedNoteText] = await encryptNote(note, {
408
- content: noteText,
409
- type: 'gpgPassword',
410
- password: '123',
411
- format: 'binary',
412
- });
413
-
414
- expect(encryptedNoteText).toBeInstanceOf(Uint8Array);
415
- expect(encryptedNote).toMatchSnapshot();
416
-
417
- const [_, decryptedNoteText] = await decryptNote(encryptedNote, {
418
- content: encryptedNoteText,
419
- type: 'gpgPassword',
420
- password: '123',
421
- });
422
-
423
- expect(decryptedNoteText).toEqual(noteText);
424
- });
@@ -1,265 +0,0 @@
1
- import {
2
- createMessage,
3
- decrypt as _decrypt,
4
- decryptKey,
5
- encrypt as _encrypt,
6
- readKey,
7
- readMessage,
8
- readPrivateKey,
9
- Stream,
10
- } from 'openpgp';
11
- import { ModelsPublicNoteEncryptionTypeEnum } from '../remote-api';
12
- import {
13
- OrgNoteEncryption,
14
- OrgNotePasswordEncryption,
15
- WithDecryptionContent,
16
- } from '../models/encryption';
17
- import { OrgNoteGpgEncryption, WithEncryptionContent } from '../models';
18
- import { armor as _armor, unarmor as _unarmor, enums } from 'openpgp';
19
-
20
- export class IncorrectOrMissingPrivateKeyPasswordError extends Error {}
21
- export class ImpossibleToDecryptWithProvidedKeysError extends Error {}
22
- export class IncorrectEncryptionPasswordError extends Error {}
23
- export class NoKeysProvidedError extends Error {}
24
- export class NoPasswordProvidedError extends Error {}
25
-
26
- const noPrivateKeyPassphraseProvidedErrorMsg =
27
- 'Error: Signing key is not decrypted.';
28
- const incorrectPrivateKeyPassphraseErrorMsg =
29
- 'Error decrypting private key: Incorrect key passphrase';
30
- const decryptionKeyIsNotDecryptedErrorMsg =
31
- 'Error decrypting message: Decryption key is not decrypted.';
32
- const corruptedPrivateKeyErrorMsg = 'Misformed armored text';
33
-
34
- const decriptionFailedErrorMsg =
35
- 'Error decrypting message: Session key decryption failed.';
36
- const incorrectEncryptionPasswordErrorMsg =
37
- 'Error decrypting message: Modification detected.';
38
-
39
- const noSymmetricallyEncryptedSessionKeyErrorMsg =
40
- 'Error decrypting message: No symmetrically encrypted session key packet found.';
41
-
42
- const armoredTextNotTypePrivateKeyErrorMsg =
43
- 'Armored text not of type private key';
44
-
45
- const notPrivateKeyErrprMsg =
46
- 'Error decrypting message: No public key encrypted session key packet found.';
47
-
48
- export const encryptViaKeys = withCustomErrors(_encryptViaKeys);
49
- export const encryptViaPassword = withCustomErrors(_encryptViaPassword);
50
- export const decryptViaPassword = withCustomErrors(_decryptViaPassword);
51
- export const decryptViaKeys = withCustomErrors(_decryptViaKeys);
52
-
53
- export const encrypt = async <
54
- T extends WithEncryptionContent<OrgNoteEncryption>,
55
- >(
56
- encryptionParams: T
57
- ): Promise<T['format'] extends 'binary' ? Uint8Array : string> => {
58
- if (
59
- !encryptionParams.type ||
60
- encryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.Disabled
61
- ) {
62
- return encryptionParams.content as any;
63
- }
64
-
65
- const res = (encryptionParams.type ===
66
- ModelsPublicNoteEncryptionTypeEnum.GpgKeys
67
- ? await encryptViaKeys(encryptionParams)
68
- : await encryptViaPassword(encryptionParams)) as unknown as Promise<
69
- T['format'] extends 'binary' ? Uint8Array : string
70
- >;
71
-
72
- return res;
73
- };
74
-
75
- export const decrypt = async <
76
- T extends WithDecryptionContent<OrgNoteEncryption>,
77
- >(
78
- decryptionParams: T
79
- ): Promise<T['format'] extends 'binary' ? Uint8Array : string> => {
80
- if (
81
- !decryptionParams.type ||
82
- decryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.Disabled
83
- ) {
84
- return decryptionParams.content as any;
85
- }
86
- const decryptedNote = (decryptionParams.type ===
87
- ModelsPublicNoteEncryptionTypeEnum.GpgKeys
88
- ? await decryptViaKeys(decryptionParams)
89
- : await decryptViaPassword(decryptionParams)) as unknown as Promise<
90
- T['format'] extends 'binary' ? Uint8Array : string
91
- >;
92
-
93
- return decryptedNote;
94
- };
95
-
96
- async function _encryptViaPassword<
97
- T extends WithEncryptionContent<OrgNotePasswordEncryption>,
98
- >({
99
- content,
100
- password,
101
- format = 'binary',
102
- }: T): Promise<T['format'] extends 'binary' ? Uint8Array : string> {
103
- const message = await createMessage({
104
- text: content,
105
- });
106
-
107
- const encryptedMessage = await _encrypt({
108
- message,
109
- format: format as any,
110
- passwords: [password],
111
- });
112
-
113
- return encryptedMessage as Promise<
114
- T['format'] extends 'binary' ? Uint8Array : string
115
- >;
116
- }
117
-
118
- export async function _encryptViaKeys<
119
- T extends WithEncryptionContent<OrgNoteGpgEncryption>,
120
- >({
121
- content,
122
- publicKey: armoredPublicKey,
123
- privateKey: armoredPrivateKey,
124
- privateKeyPassphrase,
125
- format = 'binary',
126
- }: T): Promise<T['format'] extends 'binary' ? Uint8Array : string> {
127
- const publicKey = await readKey({ armoredKey: armoredPublicKey });
128
-
129
- const message = await createMessage({
130
- text: content,
131
- });
132
-
133
- const encryptedPrivateKey = await readPrivateKey({
134
- armoredKey: armoredPrivateKey,
135
- });
136
-
137
- const privateKey = privateKeyPassphrase
138
- ? await decryptKey({
139
- privateKey: encryptedPrivateKey,
140
- passphrase: privateKeyPassphrase,
141
- })
142
- : encryptedPrivateKey;
143
-
144
- const encryptedMessage = await _encrypt({
145
- message,
146
- format: format as any,
147
- encryptionKeys: publicKey,
148
- signingKeys: privateKey,
149
- });
150
-
151
- return encryptedMessage as Promise<
152
- T['format'] extends 'binary' ? Uint8Array : string
153
- >;
154
- }
155
-
156
- async function _decryptViaPassword<
157
- T extends Omit<WithDecryptionContent<OrgNotePasswordEncryption>, 'type'>,
158
- >({
159
- content,
160
- password,
161
- format = 'utf8',
162
- }: T): Promise<T['format'] extends 'binary' ? Uint8Array : string> {
163
- const isArmoredContent = typeof content === 'string';
164
-
165
- const message = await (isArmoredContent
166
- ? readMessage({ armoredMessage: content })
167
- : readMessage({ binaryMessage: content }));
168
-
169
- const { data: decryptedText } = await _decrypt({
170
- message,
171
- format,
172
- passwords: password,
173
- });
174
-
175
- return decryptedText as Promise<
176
- T['format'] extends 'binary' ? Uint8Array : string
177
- >;
178
- }
179
-
180
- // TODO: master OrgNoteGpgDecryption type
181
- async function _decryptViaKeys<
182
- T extends Omit<WithDecryptionContent<OrgNoteGpgEncryption>, 'type'>,
183
- >({
184
- privateKey: armoredPrivateKey,
185
- privateKeyPassphrase,
186
- content,
187
- format = 'utf8',
188
- }: T): Promise<T['format'] extends 'binary' ? Uint8Array : string> {
189
- const encryptedPrivateKey = await readPrivateKey({
190
- armoredKey: armoredPrivateKey,
191
- });
192
-
193
- const privateKey = privateKeyPassphrase
194
- ? await decryptKey({
195
- privateKey: encryptedPrivateKey,
196
- passphrase: privateKeyPassphrase,
197
- })
198
- : encryptedPrivateKey;
199
-
200
- const isString = typeof content === 'string';
201
- const message = await (isString
202
- ? readMessage({ armoredMessage: content })
203
- : readMessage({ binaryMessage: content }));
204
-
205
- const { data: decryptedText } = await _decrypt({
206
- message,
207
- format,
208
- decryptionKeys: privateKey,
209
- });
210
-
211
- return decryptedText as Promise<
212
- T['format'] extends 'binary' ? Uint8Array : string
213
- >;
214
- }
215
-
216
- function withCustomErrors<P extends unknown[], T>(
217
- fn: (...args: P) => Promise<T | never>
218
- ) {
219
- return async (...args: P): Promise<T> => {
220
- try {
221
- return await fn(...args);
222
- } catch (e: unknown) {
223
- if (!(e instanceof Error)) {
224
- throw e;
225
- }
226
- if (
227
- [
228
- noPrivateKeyPassphraseProvidedErrorMsg,
229
- incorrectPrivateKeyPassphraseErrorMsg,
230
- corruptedPrivateKeyErrorMsg,
231
- decryptionKeyIsNotDecryptedErrorMsg,
232
- armoredTextNotTypePrivateKeyErrorMsg,
233
- ].includes(e.message)
234
- ) {
235
- throw new IncorrectOrMissingPrivateKeyPasswordError(e.message);
236
- }
237
- if (e.message === decriptionFailedErrorMsg) {
238
- throw new ImpossibleToDecryptWithProvidedKeysError(e.message);
239
- }
240
- if (e.message === incorrectEncryptionPasswordErrorMsg) {
241
- throw new IncorrectEncryptionPasswordError();
242
- }
243
- if (e.message === noSymmetricallyEncryptedSessionKeyErrorMsg) {
244
- throw new NoKeysProvidedError();
245
- }
246
-
247
- if (e.message === notPrivateKeyErrprMsg) {
248
- throw new NoPasswordProvidedError();
249
- }
250
-
251
- throw e;
252
- }
253
- };
254
- }
255
-
256
- // TODO: feat/native-encryption-support add custom error handling
257
- export function armor(data: Uint8Array): string {
258
- return _armor(enums.armor.message, data);
259
- }
260
-
261
- export async function unarmor(
262
- data: string
263
- ): Promise<{ text: string; data: Stream<Uint8Array>; type: enums.armor }> {
264
- return await _unarmor(data);
265
- }