orgnote-api 0.7.1 → 0.7.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.
@@ -0,0 +1,152 @@
1
+ import {
2
+ createMessage,
3
+ decrypt,
4
+ decryptKey,
5
+ encrypt,
6
+ readKey,
7
+ readMessage,
8
+ readPrivateKey,
9
+ } from 'openpgp';
10
+
11
+ export class IncorrectOrMissingPrivateKeyPasswordError extends Error {}
12
+ export class ImpossibleToDecryptWithProvidedKeysError extends Error {}
13
+ export class IncorrectEncryptionPasswordError extends Error {}
14
+
15
+ const noPrivateKeyPassphraseProvidedErrorMsg =
16
+ 'Error: Signing key is not decrypted.';
17
+ const incorrectPrivateKeyPassphraseErrorMsg =
18
+ 'Error decrypting private key: Incorrect key passphrase';
19
+ const decryptionKeyIsNotDecryptedErrorMsg =
20
+ 'Error decrypting message: Decryption key is not decrypted.';
21
+ const corruptedPrivateKeyErrorMsg = 'Misformed armored text';
22
+
23
+ const decriptionFailedErrorMsg =
24
+ 'Error decrypting message: Session key decryption failed.';
25
+ const incorrectEncryptionPasswordErrorMsg =
26
+ 'Error decrypting message: Modification detected.';
27
+
28
+ export const encryptViaKeys = withCustomErrors(_encryptViaKeys);
29
+ export const encryptViaPassword = withCustomErrors(_encryptViaPassword);
30
+ export const decryptViaPassword = withCustomErrors(_decryptViaPassword);
31
+ export const decryptViaKeys = withCustomErrors(_decryptViaKeys);
32
+
33
+ async function _encryptViaPassword(
34
+ text: string,
35
+ password: string
36
+ ): Promise<string> {
37
+ const message = await createMessage({
38
+ text,
39
+ });
40
+
41
+ const encryptedMessage = await encrypt({
42
+ message,
43
+ format: 'armored',
44
+ passwords: [password],
45
+ });
46
+
47
+ return encryptedMessage.toString();
48
+ }
49
+
50
+ async function _encryptViaKeys(
51
+ text: string,
52
+ armoredPublicKey: string,
53
+ armoredPrivateKey: string,
54
+ privateKeyPassphrase?: string
55
+ ): Promise<string> {
56
+ const publicKey = await readKey({ armoredKey: armoredPublicKey });
57
+
58
+ const message = await createMessage({
59
+ text,
60
+ });
61
+
62
+ const encryptedPrivateKey = await readPrivateKey({
63
+ armoredKey: armoredPrivateKey,
64
+ });
65
+
66
+ const privateKey = privateKeyPassphrase
67
+ ? await decryptKey({
68
+ privateKey: encryptedPrivateKey,
69
+ passphrase: privateKeyPassphrase,
70
+ })
71
+ : encryptedPrivateKey;
72
+
73
+ const encryptedMessage = await encrypt({
74
+ message,
75
+ format: 'armored',
76
+ encryptionKeys: publicKey,
77
+ signingKeys: privateKey,
78
+ });
79
+
80
+ return encryptedMessage.toString();
81
+ }
82
+
83
+ async function _decryptViaPassword(
84
+ data: string,
85
+ password: string
86
+ ): Promise<string> {
87
+ const message = await readMessage({ armoredMessage: data });
88
+
89
+ const { data: decryptedText } = await decrypt({
90
+ message,
91
+ passwords: password,
92
+ });
93
+
94
+ return decryptedText.toString();
95
+ }
96
+
97
+ async function _decryptViaKeys(
98
+ data: string,
99
+ armoredPrivateKey: string,
100
+ privateKeyPassword?: string
101
+ ): Promise<string> {
102
+ const encryptedPrivateKey = await readPrivateKey({
103
+ armoredKey: armoredPrivateKey,
104
+ });
105
+
106
+ const privateKey = privateKeyPassword
107
+ ? await decryptKey({
108
+ privateKey: encryptedPrivateKey,
109
+ passphrase: privateKeyPassword,
110
+ })
111
+ : encryptedPrivateKey;
112
+
113
+ const message = await readMessage({ armoredMessage: data });
114
+
115
+ const { data: decryptedText } = await decrypt({
116
+ message,
117
+ decryptionKeys: privateKey,
118
+ });
119
+
120
+ return decryptedText.toString();
121
+ }
122
+
123
+ function withCustomErrors<P extends unknown[], T>(
124
+ fn: (...args: P) => Promise<T | never>
125
+ ) {
126
+ return async (...args: P): Promise<T> => {
127
+ try {
128
+ return await fn(...args);
129
+ } catch (e: unknown) {
130
+ if (!(e instanceof Error)) {
131
+ throw e;
132
+ }
133
+ if (
134
+ [
135
+ noPrivateKeyPassphraseProvidedErrorMsg,
136
+ incorrectPrivateKeyPassphraseErrorMsg,
137
+ corruptedPrivateKeyErrorMsg,
138
+ decryptionKeyIsNotDecryptedErrorMsg,
139
+ ].includes(e.message)
140
+ ) {
141
+ throw new IncorrectOrMissingPrivateKeyPasswordError(e.message);
142
+ }
143
+ if (e.message === decriptionFailedErrorMsg) {
144
+ throw new ImpossibleToDecryptWithProvidedKeysError(e.message);
145
+ }
146
+ if (e.message === incorrectEncryptionPasswordErrorMsg) {
147
+ throw new IncorrectEncryptionPasswordError();
148
+ }
149
+ throw e;
150
+ }
151
+ };
152
+ }
@@ -0,0 +1,2 @@
1
+ export * from './note-encryption';
2
+ export * from './encryption';
@@ -0,0 +1,129 @@
1
+ import {
2
+ ModelsPublicNote,
3
+ ModelsPublicNoteEncryptedEnum,
4
+ } from 'src/remote-api';
5
+ import {
6
+ decryptViaKeys,
7
+ decryptViaPassword,
8
+ encryptViaKeys,
9
+ encryptViaPassword,
10
+ } from './encryption';
11
+ import { OrgNoteEncryption } from 'src/models/encryption';
12
+ import { parse, withMetaInfo } from 'org-mode-ast';
13
+
14
+ export interface AbstractEncryptedNote {
15
+ content: string;
16
+ encrypted: ModelsPublicNote['encrypted'];
17
+ meta: {
18
+ [key: string]: any;
19
+ published: boolean;
20
+ };
21
+ }
22
+
23
+ export async function encryptNote<T extends AbstractEncryptedNote>(
24
+ note: T,
25
+ encryptionParams: OrgNoteEncryption
26
+ ): Promise<T> {
27
+ if (
28
+ encryptionParams.type === ModelsPublicNoteEncryptedEnum.Disabled ||
29
+ note.meta.published
30
+ ) {
31
+ return note;
32
+ }
33
+
34
+ note.meta = { id: note.meta.id, published: note.meta.published };
35
+
36
+ if (encryptionParams.type === ModelsPublicNoteEncryptedEnum.GpgKeys) {
37
+ return encryptNoteViaKeys(
38
+ note,
39
+ encryptionParams.publicKey,
40
+ encryptionParams.privateKey,
41
+ encryptionParams.privateKeyPassphrase
42
+ );
43
+ }
44
+ return encryptNoteViaPassword(note, encryptionParams.password);
45
+ }
46
+ export async function encryptNoteViaPassword<T extends AbstractEncryptedNote>(
47
+ note: T,
48
+ password: string
49
+ ): Promise<T> {
50
+ const content = encryptViaPassword(note.content, password);
51
+ return {
52
+ ...note,
53
+ content,
54
+ encrypted: ModelsPublicNoteEncryptedEnum.GpgPassword,
55
+ };
56
+ }
57
+
58
+ export async function encryptNoteViaKeys<T extends AbstractEncryptedNote>(
59
+ note: T,
60
+ publicKey: string,
61
+ privateKey: string,
62
+ privateKeyPassphrase?: string
63
+ ): Promise<T> {
64
+ const content = await encryptViaKeys(
65
+ note.content,
66
+ publicKey,
67
+ privateKey,
68
+ privateKeyPassphrase
69
+ );
70
+
71
+ return {
72
+ ...note,
73
+ content,
74
+ encrypted: ModelsPublicNoteEncryptedEnum.GpgKeys,
75
+ };
76
+ }
77
+
78
+ export async function decryptNote<T extends AbstractEncryptedNote>(
79
+ note: T,
80
+ encryptionParams: OrgNoteEncryption
81
+ ): Promise<T> {
82
+ if (encryptionParams.type === ModelsPublicNoteEncryptedEnum.Disabled) {
83
+ return note;
84
+ }
85
+ const decryptedNote =
86
+ encryptionParams.type === ModelsPublicNoteEncryptedEnum.GpgKeys
87
+ ? await decryptNoteViaKeys(
88
+ note,
89
+ encryptionParams.privateKey,
90
+ encryptionParams.privateKeyPassphrase
91
+ )
92
+ : await decryptNoteViaPassword(note, encryptionParams.password);
93
+
94
+ const parsed = withMetaInfo(parse(decryptedNote.content));
95
+
96
+ return {
97
+ ...decryptedNote,
98
+ meta: { ...decryptedNote.meta, published: parsed.meta.published },
99
+ };
100
+ }
101
+
102
+ export async function decryptNoteViaPassword<T extends AbstractEncryptedNote>(
103
+ note: T,
104
+ password: string
105
+ ): Promise<T> {
106
+ const content = decryptViaPassword(note.content, password);
107
+ return {
108
+ ...note,
109
+ content,
110
+ encrypted: ModelsPublicNoteEncryptedEnum.GpgPassword,
111
+ };
112
+ }
113
+
114
+ export async function decryptNoteViaKeys<T extends AbstractEncryptedNote>(
115
+ note: T,
116
+ privateKey: string,
117
+ privateKeyPassphrase?: string
118
+ ): Promise<T> {
119
+ const content = await decryptViaKeys(
120
+ note.content,
121
+ privateKey,
122
+ privateKeyPassphrase
123
+ );
124
+ return {
125
+ ...note,
126
+ content,
127
+ encrypted: ModelsPublicNoteEncryptedEnum.GpgKeys,
128
+ };
129
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orgnote-api",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Official API for creating extensions for OrgNote app",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -17,7 +17,13 @@
17
17
  "type": "git",
18
18
  "url": "git+https://github.com/artawower/orgnote-api.git"
19
19
  },
20
- "files": ["index.ts", "api.ts", "remote-api/**", "models/**"],
20
+ "files": [
21
+ "index.ts",
22
+ "api.ts",
23
+ "remote-api/**",
24
+ "models/**",
25
+ "encryption/**"
26
+ ],
21
27
  "exports": {
22
28
  ".": "./index.ts",
23
29
  "./remote-api": "./remote-api/index.ts",