@lockerpm/desktop-service 1.0.0

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 (158) hide show
  1. package/README.md +98 -0
  2. package/lib/cjs/abstractions/api.service.js +2 -0
  3. package/lib/cjs/abstractions/crypto.service.js +147 -0
  4. package/lib/cjs/abstractions/errors.js +99 -0
  5. package/lib/cjs/abstractions/event.service.js +2 -0
  6. package/lib/cjs/abstractions/index.js +2 -0
  7. package/lib/cjs/abstractions/socket.service.js +11 -0
  8. package/lib/cjs/abstractions/storage.service.js +2 -0
  9. package/lib/cjs/index.js +243 -0
  10. package/lib/cjs/misc/config.js +15 -0
  11. package/lib/cjs/misc/utils.js +37 -0
  12. package/lib/cjs/proto/google/api/annotations.js +2 -0
  13. package/lib/cjs/proto/google/api/http.js +477 -0
  14. package/lib/cjs/proto/google/protobuf/descriptor.js +4873 -0
  15. package/lib/cjs/proto/locker-service-grpc.js +1915 -0
  16. package/lib/cjs/services/api.service.js +182 -0
  17. package/lib/cjs/services/cache.service.js +50 -0
  18. package/lib/cjs/services/core-crypto.service.js +193 -0
  19. package/lib/cjs/services/crypto.service.js +101 -0
  20. package/lib/cjs/services/event.service.js +31 -0
  21. package/lib/cjs/services/fido.service.js +136 -0
  22. package/lib/cjs/services/grpc.service.js +130 -0
  23. package/lib/cjs/services/log.service.js +30 -0
  24. package/lib/cjs/services/pairing.service.js +122 -0
  25. package/lib/cjs/services/socket.service.js +280 -0
  26. package/lib/cjs/services/user.service.js +134 -0
  27. package/lib/cjs/types/abstractions/api.service.d.ts +40 -0
  28. package/lib/cjs/types/abstractions/api.service.d.ts.map +1 -0
  29. package/lib/cjs/types/abstractions/crypto.service.d.ts +46 -0
  30. package/lib/cjs/types/abstractions/crypto.service.d.ts.map +1 -0
  31. package/lib/cjs/types/abstractions/errors.d.ts +73 -0
  32. package/lib/cjs/types/abstractions/errors.d.ts.map +1 -0
  33. package/lib/cjs/types/abstractions/event.service.d.ts +23 -0
  34. package/lib/cjs/types/abstractions/event.service.d.ts.map +1 -0
  35. package/lib/cjs/types/abstractions/index.d.ts +56 -0
  36. package/lib/cjs/types/abstractions/index.d.ts.map +1 -0
  37. package/lib/cjs/types/abstractions/socket.service.d.ts +52 -0
  38. package/lib/cjs/types/abstractions/socket.service.d.ts.map +1 -0
  39. package/lib/cjs/types/abstractions/storage.service.d.ts +6 -0
  40. package/lib/cjs/types/abstractions/storage.service.d.ts.map +1 -0
  41. package/lib/cjs/types/index.d.ts +164 -0
  42. package/lib/cjs/types/index.d.ts.map +1 -0
  43. package/lib/cjs/types/misc/config.d.ts +6 -0
  44. package/lib/cjs/types/misc/config.d.ts.map +1 -0
  45. package/lib/cjs/types/misc/utils.d.ts +21 -0
  46. package/lib/cjs/types/misc/utils.d.ts.map +1 -0
  47. package/lib/cjs/types/proto/google/api/annotations.d.ts +2 -0
  48. package/lib/cjs/types/proto/google/api/annotations.d.ts.map +1 -0
  49. package/lib/cjs/types/proto/google/api/http.d.ts +195 -0
  50. package/lib/cjs/types/proto/google/api/http.d.ts.map +1 -0
  51. package/lib/cjs/types/proto/google/protobuf/descriptor.d.ts +3409 -0
  52. package/lib/cjs/types/proto/google/protobuf/descriptor.d.ts.map +1 -0
  53. package/lib/cjs/types/proto/locker-service-grpc.d.ts +622 -0
  54. package/lib/cjs/types/proto/locker-service-grpc.d.ts.map +1 -0
  55. package/lib/cjs/types/services/api.service.d.ts +37 -0
  56. package/lib/cjs/types/services/api.service.d.ts.map +1 -0
  57. package/lib/cjs/types/services/cache.service.d.ts +10 -0
  58. package/lib/cjs/types/services/cache.service.d.ts.map +1 -0
  59. package/lib/cjs/types/services/core-crypto.service.d.ts +17 -0
  60. package/lib/cjs/types/services/core-crypto.service.d.ts.map +1 -0
  61. package/lib/cjs/types/services/crypto.service.d.ts +23 -0
  62. package/lib/cjs/types/services/crypto.service.d.ts.map +1 -0
  63. package/lib/cjs/types/services/event.service.d.ts +14 -0
  64. package/lib/cjs/types/services/event.service.d.ts.map +1 -0
  65. package/lib/cjs/types/services/fido.service.d.ts +40 -0
  66. package/lib/cjs/types/services/fido.service.d.ts.map +1 -0
  67. package/lib/cjs/types/services/grpc.service.d.ts +34 -0
  68. package/lib/cjs/types/services/grpc.service.d.ts.map +1 -0
  69. package/lib/cjs/types/services/log.service.d.ts +13 -0
  70. package/lib/cjs/types/services/log.service.d.ts.map +1 -0
  71. package/lib/cjs/types/services/pairing.service.d.ts +37 -0
  72. package/lib/cjs/types/services/pairing.service.d.ts.map +1 -0
  73. package/lib/cjs/types/services/socket.service.d.ts +39 -0
  74. package/lib/cjs/types/services/socket.service.d.ts.map +1 -0
  75. package/lib/cjs/types/services/user.service.d.ts +32 -0
  76. package/lib/cjs/types/services/user.service.d.ts.map +1 -0
  77. package/lib/cjs/types/usecases/fido.d.ts +54 -0
  78. package/lib/cjs/types/usecases/fido.d.ts.map +1 -0
  79. package/lib/cjs/usecases/fido.js +227 -0
  80. package/lib/esm/abstractions/api.service.js +2 -0
  81. package/lib/esm/abstractions/crypto.service.js +165 -0
  82. package/lib/esm/abstractions/errors.js +100 -0
  83. package/lib/esm/abstractions/event.service.js +2 -0
  84. package/lib/esm/abstractions/index.js +2 -0
  85. package/lib/esm/abstractions/socket.service.js +11 -0
  86. package/lib/esm/abstractions/storage.service.js +2 -0
  87. package/lib/esm/index.mjs +227 -0
  88. package/lib/esm/misc/config.js +15 -0
  89. package/lib/esm/misc/utils.js +37 -0
  90. package/lib/esm/proto/google/api/annotations.js +2 -0
  91. package/lib/esm/proto/google/api/http.js +468 -0
  92. package/lib/esm/proto/google/protobuf/descriptor.js +4830 -0
  93. package/lib/esm/proto/locker-service-grpc.js +1892 -0
  94. package/lib/esm/services/api.service.js +177 -0
  95. package/lib/esm/services/cache.service.js +52 -0
  96. package/lib/esm/services/core-crypto.service.js +164 -0
  97. package/lib/esm/services/crypto.service.js +83 -0
  98. package/lib/esm/services/event.service.js +33 -0
  99. package/lib/esm/services/fido.service.js +139 -0
  100. package/lib/esm/services/grpc.service.js +119 -0
  101. package/lib/esm/services/log.service.js +31 -0
  102. package/lib/esm/services/pairing.service.js +107 -0
  103. package/lib/esm/services/socket.service.js +265 -0
  104. package/lib/esm/services/user.service.js +116 -0
  105. package/lib/esm/types/abstractions/api.service.d.ts +40 -0
  106. package/lib/esm/types/abstractions/api.service.d.ts.map +1 -0
  107. package/lib/esm/types/abstractions/crypto.service.d.ts +46 -0
  108. package/lib/esm/types/abstractions/crypto.service.d.ts.map +1 -0
  109. package/lib/esm/types/abstractions/errors.d.ts +73 -0
  110. package/lib/esm/types/abstractions/errors.d.ts.map +1 -0
  111. package/lib/esm/types/abstractions/event.service.d.ts +23 -0
  112. package/lib/esm/types/abstractions/event.service.d.ts.map +1 -0
  113. package/lib/esm/types/abstractions/index.d.ts +56 -0
  114. package/lib/esm/types/abstractions/index.d.ts.map +1 -0
  115. package/lib/esm/types/abstractions/socket.service.d.ts +52 -0
  116. package/lib/esm/types/abstractions/socket.service.d.ts.map +1 -0
  117. package/lib/esm/types/abstractions/storage.service.d.ts +6 -0
  118. package/lib/esm/types/abstractions/storage.service.d.ts.map +1 -0
  119. package/lib/esm/types/index.d.ts +164 -0
  120. package/lib/esm/types/index.d.ts.map +1 -0
  121. package/lib/esm/types/misc/config.d.ts +6 -0
  122. package/lib/esm/types/misc/config.d.ts.map +1 -0
  123. package/lib/esm/types/misc/utils.d.ts +21 -0
  124. package/lib/esm/types/misc/utils.d.ts.map +1 -0
  125. package/lib/esm/types/proto/google/api/annotations.d.ts +2 -0
  126. package/lib/esm/types/proto/google/api/annotations.d.ts.map +1 -0
  127. package/lib/esm/types/proto/google/api/http.d.ts +195 -0
  128. package/lib/esm/types/proto/google/api/http.d.ts.map +1 -0
  129. package/lib/esm/types/proto/google/protobuf/descriptor.d.ts +3409 -0
  130. package/lib/esm/types/proto/google/protobuf/descriptor.d.ts.map +1 -0
  131. package/lib/esm/types/proto/locker-service-grpc.d.ts +622 -0
  132. package/lib/esm/types/proto/locker-service-grpc.d.ts.map +1 -0
  133. package/lib/esm/types/services/api.service.d.ts +37 -0
  134. package/lib/esm/types/services/api.service.d.ts.map +1 -0
  135. package/lib/esm/types/services/cache.service.d.ts +10 -0
  136. package/lib/esm/types/services/cache.service.d.ts.map +1 -0
  137. package/lib/esm/types/services/core-crypto.service.d.ts +17 -0
  138. package/lib/esm/types/services/core-crypto.service.d.ts.map +1 -0
  139. package/lib/esm/types/services/crypto.service.d.ts +23 -0
  140. package/lib/esm/types/services/crypto.service.d.ts.map +1 -0
  141. package/lib/esm/types/services/event.service.d.ts +14 -0
  142. package/lib/esm/types/services/event.service.d.ts.map +1 -0
  143. package/lib/esm/types/services/fido.service.d.ts +40 -0
  144. package/lib/esm/types/services/fido.service.d.ts.map +1 -0
  145. package/lib/esm/types/services/grpc.service.d.ts +34 -0
  146. package/lib/esm/types/services/grpc.service.d.ts.map +1 -0
  147. package/lib/esm/types/services/log.service.d.ts +13 -0
  148. package/lib/esm/types/services/log.service.d.ts.map +1 -0
  149. package/lib/esm/types/services/pairing.service.d.ts +37 -0
  150. package/lib/esm/types/services/pairing.service.d.ts.map +1 -0
  151. package/lib/esm/types/services/socket.service.d.ts +39 -0
  152. package/lib/esm/types/services/socket.service.d.ts.map +1 -0
  153. package/lib/esm/types/services/user.service.d.ts +32 -0
  154. package/lib/esm/types/services/user.service.d.ts.map +1 -0
  155. package/lib/esm/types/usecases/fido.d.ts +54 -0
  156. package/lib/esm/types/usecases/fido.d.ts.map +1 -0
  157. package/lib/esm/usecases/fido.js +201 -0
  158. package/package.json +55 -0
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ApiService = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const errors_1 = require("../abstractions/errors");
18
+ class ApiService {
19
+ constructor(params) {
20
+ const { baseUrl, logger, headers } = params;
21
+ this.baseUrl = baseUrl;
22
+ this.token = '';
23
+ this.logger = logger;
24
+ this.headers = headers || {};
25
+ }
26
+ setToken(token) {
27
+ this.token = token;
28
+ }
29
+ getPasswordlessCredentials(email) {
30
+ var _a;
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ try {
33
+ const url = `${this.baseUrl}/cystack_platform/pm/passwordless/credential`;
34
+ const res = yield axios_1.default.get(url, {
35
+ params: {
36
+ email,
37
+ },
38
+ headers: this.headers,
39
+ });
40
+ const data = res.data;
41
+ this.logDebug({ url, method: 'GET', data });
42
+ return data;
43
+ }
44
+ catch (error) {
45
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
46
+ throw new errors_1.ServiceError('1002', error);
47
+ }
48
+ throw new errors_1.ServiceError('1000', error);
49
+ }
50
+ });
51
+ }
52
+ // TODO: if service is only built by CyStack, then this base API must be fixed
53
+ getReleases(os) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ try {
56
+ const url = `${this.baseUrl}/cystack_platform/pm/releases`;
57
+ const res = yield axios_1.default.get(url, {
58
+ params: {
59
+ client_id: 'desktop',
60
+ os,
61
+ },
62
+ headers: this.headers,
63
+ });
64
+ const data = res.data;
65
+ this.logDebug({ url, method: 'GET', data });
66
+ return data;
67
+ }
68
+ catch (error) {
69
+ throw new errors_1.ServiceError('1000', error);
70
+ }
71
+ });
72
+ }
73
+ setPasswordlessCredential(params) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ if (!this.token) {
76
+ throw new errors_1.ServiceError('1001');
77
+ }
78
+ try {
79
+ const { credentialId, name, type, random } = params;
80
+ const url = `${this.baseUrl}/cystack_platform/pm/passwordless/credential`;
81
+ const res = yield axios_1.default.post(url, {
82
+ credential_id: credentialId,
83
+ name,
84
+ type,
85
+ random,
86
+ }, {
87
+ headers: Object.assign({ Authorization: `Bearer ${this.token}` }, this.headers),
88
+ });
89
+ const data = res.data;
90
+ this.logDebug({ url, method: 'POST', data, payload: params });
91
+ return data;
92
+ }
93
+ catch (error) {
94
+ throw new errors_1.ServiceError('1000', error);
95
+ }
96
+ });
97
+ }
98
+ deletePasswordlessCredential() {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ if (!this.token) {
101
+ throw new errors_1.ServiceError('1001');
102
+ }
103
+ try {
104
+ const url = `${this.baseUrl}/cystack_platform/pm/passwordless/credential`;
105
+ yield axios_1.default.delete(url, {
106
+ headers: Object.assign({ Authorization: `Bearer ${this.token}` }, this.headers),
107
+ });
108
+ this.logDebug({
109
+ url,
110
+ method: 'DELETE',
111
+ data: undefined,
112
+ });
113
+ }
114
+ catch (error) {
115
+ throw new errors_1.ServiceError('1000', error);
116
+ }
117
+ });
118
+ }
119
+ // ---------------- BACKUP KEYS ----------------
120
+ listBackupPasswordlessCredentials() {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ if (!this.token) {
123
+ throw new errors_1.ServiceError('1001');
124
+ }
125
+ try {
126
+ const url = `${this.baseUrl}/cystack_platform/pm/users/backup_credentials`;
127
+ const res = yield axios_1.default.get(url, {
128
+ params: {
129
+ paging: 0,
130
+ },
131
+ headers: Object.assign({ Authorization: `Bearer ${this.token}` }, this.headers),
132
+ });
133
+ const data = res.data;
134
+ this.logDebug({ url, method: 'GET', data });
135
+ return data;
136
+ }
137
+ catch (error) {
138
+ throw new errors_1.ServiceError('1000', error);
139
+ }
140
+ });
141
+ }
142
+ setBackupPasswordlessCredential(payload) {
143
+ return __awaiter(this, void 0, void 0, function* () {
144
+ if (!this.token) {
145
+ throw new errors_1.ServiceError('1001');
146
+ }
147
+ try {
148
+ const url = `${this.baseUrl}/cystack_platform/pm/users/backup_credentials`;
149
+ const res = yield axios_1.default.post(url, payload, {
150
+ headers: Object.assign({ Authorization: `Bearer ${this.token}` }, this.headers),
151
+ });
152
+ this.logDebug({ url, method: 'POST', data: res.data, payload });
153
+ return res.data;
154
+ }
155
+ catch (error) {
156
+ throw new errors_1.ServiceError('1000', error);
157
+ }
158
+ });
159
+ }
160
+ deleteBackupPasswordlessCredential(id) {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ if (!this.token) {
163
+ throw new errors_1.ServiceError('1001');
164
+ }
165
+ try {
166
+ const url = `${this.baseUrl}/cystack_platform/pm/users/backup_credentials/${id}`;
167
+ yield axios_1.default.delete(url, {
168
+ headers: Object.assign({ Authorization: `Bearer ${this.token}` }, this.headers),
169
+ });
170
+ }
171
+ catch (error) {
172
+ throw new errors_1.ServiceError('1000', error);
173
+ }
174
+ });
175
+ }
176
+ // ---------------- PRIVATE METHODS ----------------
177
+ logDebug(params) {
178
+ const { url, method, data, payload } = params;
179
+ this.logger.debug(`API call ${method} ${url}\nwith ${JSON.stringify(payload)} \nreturn ${JSON.stringify(data)}`);
180
+ }
181
+ }
182
+ exports.ApiService = ApiService;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheService = void 0;
4
+ const locker_service_grpc_1 = require("../proto/locker-service-grpc");
5
+ const errors_1 = require("../abstractions/errors");
6
+ var CacheMessage;
7
+ (function (CacheMessage) {
8
+ CacheMessage["GET_CACHE"] = "getCache";
9
+ CacheMessage["SET_CACHE"] = "setCache";
10
+ })(CacheMessage || (CacheMessage = {}));
11
+ class CacheService {
12
+ constructor(logger, grpcService) {
13
+ this.logger = logger;
14
+ this.grpc = grpcService;
15
+ }
16
+ getCache() {
17
+ const req = new locker_service_grpc_1.locker_service_grpc.CacheRequest();
18
+ req.message = CacheMessage.GET_CACHE;
19
+ return new Promise((resolve, reject) => {
20
+ this.grpc.client.CacheChannel(req, (err, res) => {
21
+ this.logger.debug(res);
22
+ if (err) {
23
+ reject(errors_1.ServiceError.fromError(err));
24
+ return;
25
+ }
26
+ if (!(res === null || res === void 0 ? void 0 : res.json)) {
27
+ resolve(null);
28
+ return;
29
+ }
30
+ resolve(JSON.parse(res.json));
31
+ });
32
+ });
33
+ }
34
+ setCache(data) {
35
+ const req = new locker_service_grpc_1.locker_service_grpc.CacheRequest();
36
+ req.message = CacheMessage.SET_CACHE;
37
+ req.json = JSON.stringify(data);
38
+ return new Promise((resolve, reject) => {
39
+ this.grpc.client.CacheChannel(req, (err, res) => {
40
+ this.logger.debug(res);
41
+ if (err) {
42
+ reject(errors_1.ServiceError.fromError(err));
43
+ return;
44
+ }
45
+ resolve();
46
+ });
47
+ });
48
+ }
49
+ }
50
+ exports.CacheService = CacheService;
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CoreCryptoService = void 0;
16
+ /**
17
+ * Crypto core from Locker Vault
18
+ * */
19
+ const crypto_1 = __importDefault(require("crypto"));
20
+ const crypto_service_1 = require("../abstractions/crypto.service");
21
+ const utils_1 = require("../misc/utils");
22
+ const crypto = crypto_1.default.webcrypto;
23
+ class CoreCryptoService {
24
+ makeKey(password, salt, kdf, kdfIterations) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ let key;
27
+ if (!kdf || kdf === crypto_service_1.KdfType.PBKDF2_SHA256) {
28
+ if (!kdfIterations) {
29
+ kdfIterations = 5000;
30
+ }
31
+ else if (kdfIterations < 5000) {
32
+ throw new Error('PBKDF2 iteration minimum is 5000.');
33
+ }
34
+ key = yield this.pbkdf2(password, salt, 'sha256', kdfIterations);
35
+ }
36
+ else {
37
+ throw new Error('Unknown Kdf.');
38
+ }
39
+ return new crypto_service_1.SymmetricCryptoKey(key);
40
+ });
41
+ }
42
+ hashPassword(password, key) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ if (!password || !key) {
45
+ throw new Error('Invalid parameters.');
46
+ }
47
+ const hash = yield this.pbkdf2(key.key, password, 'sha256', 1);
48
+ return utils_1.Utils.fromBufferToB64(hash);
49
+ });
50
+ }
51
+ remakeEncKey(currentEncKey, key) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ let encKeyEnc;
54
+ if (key.key.byteLength === 32) {
55
+ const newKey = yield this.stretchKey(key);
56
+ encKeyEnc = yield this.encrypt(currentEncKey, newKey);
57
+ }
58
+ else if (key.key.byteLength === 64) {
59
+ encKeyEnc = yield this.encrypt(currentEncKey, key);
60
+ }
61
+ else {
62
+ throw new Error('Invalid key size.');
63
+ }
64
+ return [new crypto_service_1.SymmetricCryptoKey(currentEncKey), encKeyEnc];
65
+ });
66
+ }
67
+ // ---------------- PRIVATE METHODS -------------------
68
+ pbkdf2(password, salt, algorithm, iterations) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const wcLen = algorithm === 'sha256' ? 256 : 512;
71
+ const passwordBuf = this.toBuf(password);
72
+ const saltBuf = this.toBuf(salt);
73
+ const pbkdf2Params = {
74
+ name: 'PBKDF2',
75
+ salt: saltBuf,
76
+ iterations,
77
+ hash: { name: this.toWebCryptoAlgorithm(algorithm) },
78
+ };
79
+ const impKey = yield crypto.subtle.importKey('raw', passwordBuf, { name: 'PBKDF2' }, false, ['deriveBits']);
80
+ return yield crypto.subtle.deriveBits(pbkdf2Params, impKey, wcLen);
81
+ });
82
+ }
83
+ toBuf(value) {
84
+ let buf;
85
+ if (typeof value === 'string') {
86
+ buf = utils_1.Utils.fromUtf8ToArray(value).buffer;
87
+ }
88
+ else {
89
+ buf = value;
90
+ }
91
+ return buf;
92
+ }
93
+ toWebCryptoAlgorithm(algorithm) {
94
+ if (algorithm === 'md5') {
95
+ throw new Error('MD5 is not supported in WebCrypto.');
96
+ }
97
+ return algorithm === 'sha1' ? 'SHA-1' : algorithm === 'sha256' ? 'SHA-256' : 'SHA-512';
98
+ }
99
+ stretchKey(key) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const newKey = new Uint8Array(64);
102
+ const encKey = yield this.hkdfExpand(key.key, 'enc', 32, 'sha256');
103
+ const macKey = yield this.hkdfExpand(key.key, 'mac', 32, 'sha256');
104
+ newKey.set(new Uint8Array(encKey));
105
+ newKey.set(new Uint8Array(macKey), 32);
106
+ return new crypto_service_1.SymmetricCryptoKey(newKey.buffer);
107
+ });
108
+ }
109
+ hkdfExpand(prk, info, outputByteSize, algorithm) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ const hashLen = algorithm === 'sha256' ? 32 : 64;
112
+ if (outputByteSize > 255 * hashLen) {
113
+ throw new Error('outputByteSize is too large.');
114
+ }
115
+ const prkArr = new Uint8Array(prk);
116
+ if (prkArr.length < hashLen) {
117
+ throw new Error('prk is too small.');
118
+ }
119
+ const infoBuf = this.toBuf(info);
120
+ const infoArr = new Uint8Array(infoBuf);
121
+ let runningOkmLength = 0;
122
+ let previousT = new Uint8Array(0);
123
+ const n = Math.ceil(outputByteSize / hashLen);
124
+ const okm = new Uint8Array(n * hashLen);
125
+ for (let i = 0; i < n; i++) {
126
+ const t = new Uint8Array(previousT.length + infoArr.length + 1);
127
+ t.set(previousT);
128
+ t.set(infoArr, previousT.length);
129
+ t.set([i + 1], t.length - 1);
130
+ previousT = new Uint8Array(yield this.hmac(t.buffer, prk, algorithm));
131
+ okm.set(previousT, runningOkmLength);
132
+ runningOkmLength += previousT.length;
133
+ if (runningOkmLength >= outputByteSize) {
134
+ break;
135
+ }
136
+ }
137
+ return okm.slice(0, outputByteSize).buffer;
138
+ });
139
+ }
140
+ hmac(value, key, algorithm) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const signingAlgorithm = {
143
+ name: 'HMAC',
144
+ hash: { name: this.toWebCryptoAlgorithm(algorithm) },
145
+ };
146
+ const impKey = yield crypto.subtle.importKey('raw', key, signingAlgorithm, false, ['sign']);
147
+ return yield crypto.subtle.sign(signingAlgorithm, impKey, value);
148
+ });
149
+ }
150
+ encrypt(plainValue, key) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ let plainBuf;
153
+ if (typeof plainValue === 'string') {
154
+ plainBuf = utils_1.Utils.fromUtf8ToArray(plainValue).buffer;
155
+ }
156
+ else {
157
+ plainBuf = plainValue;
158
+ }
159
+ const encObj = yield this.aesEncrypt(plainBuf, key);
160
+ const iv = utils_1.Utils.fromBufferToB64(encObj.iv);
161
+ const data = utils_1.Utils.fromBufferToB64(encObj.data);
162
+ const mac = encObj.mac ? utils_1.Utils.fromBufferToB64(encObj.mac) : undefined;
163
+ return new crypto_service_1.EncString(encObj.key.encType, data, iv, mac);
164
+ });
165
+ }
166
+ aesEncrypt(data, key) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const obj = new crypto_service_1.EncryptedObject();
169
+ obj.key = key;
170
+ obj.iv = this.randomBytes(16);
171
+ obj.data = yield this._aesEncrypt(data, obj.iv, obj.key.encKey);
172
+ if (obj.key.macKey != null) {
173
+ const macData = new Uint8Array(obj.iv.byteLength + obj.data.byteLength);
174
+ macData.set(new Uint8Array(obj.iv), 0);
175
+ macData.set(new Uint8Array(obj.data), obj.iv.byteLength);
176
+ obj.mac = yield this.hmac(macData.buffer, obj.key.macKey, 'sha256');
177
+ }
178
+ return obj;
179
+ });
180
+ }
181
+ randomBytes(length) {
182
+ return crypto.getRandomValues(new Uint8Array(length));
183
+ }
184
+ _aesEncrypt(data, iv, key) {
185
+ return __awaiter(this, void 0, void 0, function* () {
186
+ const impKey = yield crypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, [
187
+ 'encrypt',
188
+ ]);
189
+ return yield crypto.subtle.encrypt({ name: 'AES-CBC', iv }, impKey, data);
190
+ });
191
+ }
192
+ }
193
+ exports.CoreCryptoService = CoreCryptoService;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CryptoService = void 0;
16
+ const crypto_1 = __importDefault(require("crypto"));
17
+ const fs_1 = __importDefault(require("fs"));
18
+ const crypto_service_1 = require("../abstractions/crypto.service");
19
+ const utils_1 = require("../misc/utils");
20
+ const core_crypto_service_1 = require("./core-crypto.service");
21
+ const crypto = crypto_1.default.webcrypto;
22
+ class CryptoService {
23
+ constructor() {
24
+ this.core = new core_crypto_service_1.CoreCryptoService();
25
+ }
26
+ createECDHKeyPair() {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const keyPair = yield crypto.subtle.generateKey(crypto_service_1.ECDH, true, ['deriveKey', 'deriveBits']);
29
+ const rawPublicKey = yield crypto.subtle.exportKey('raw', keyPair.publicKey);
30
+ return {
31
+ publicKey: utils_1.Utils.fromBufferToHex(rawPublicKey),
32
+ privateKey: keyPair.privateKey,
33
+ };
34
+ });
35
+ }
36
+ createEncKey(theirPublicKey, ourPrivateKey) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const sharedSecret = yield this.createECDHSharedSecret(theirPublicKey, ourPrivateKey);
39
+ const approveCode = new Uint8Array(sharedSecret, 0, 2).join('').padStart(6, '0').substring(0, 6);
40
+ return {
41
+ encKey: utils_1.Utils.fromBufferToHex(sharedSecret),
42
+ approveCode,
43
+ };
44
+ });
45
+ }
46
+ aesEncrypt(data, key) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const iv = this.getRandomValues(16);
49
+ const dataBuffer = new TextEncoder().encode(data);
50
+ const cryptoKey = yield crypto.subtle.importKey('raw', utils_1.Utils.fromHexToArray(key), crypto_service_1.AES, false, [
51
+ 'encrypt',
52
+ ]);
53
+ const encrypted = yield crypto.subtle.encrypt({
54
+ name: crypto_service_1.AES.name,
55
+ iv,
56
+ }, cryptoKey, dataBuffer);
57
+ return `${utils_1.Utils.fromBufferToHex(iv)}.${utils_1.Utils.fromBufferToHex(encrypted)}`;
58
+ });
59
+ }
60
+ aesDecrypt(data, key) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const cryptoKey = yield crypto.subtle.importKey('raw', utils_1.Utils.fromHexToArray(key), crypto_service_1.AES, false, [
63
+ 'decrypt',
64
+ ]);
65
+ const [iv, encString] = data.split('.');
66
+ const decrypted = yield crypto.subtle.decrypt({
67
+ name: crypto_service_1.AES.name,
68
+ iv: utils_1.Utils.fromHexToArray(iv),
69
+ }, cryptoKey, utils_1.Utils.fromHexToArray(encString));
70
+ const decryptedString = new TextDecoder().decode(decrypted);
71
+ return decryptedString;
72
+ });
73
+ }
74
+ getFileChecksum(path) {
75
+ return new Promise((resolve, reject) => {
76
+ try {
77
+ const file = fs_1.default.readFileSync(path);
78
+ const checksum = crypto_1.default.createHash('sha256').update(file).digest('hex');
79
+ resolve(checksum);
80
+ }
81
+ catch (error) {
82
+ reject(error);
83
+ }
84
+ });
85
+ }
86
+ getRandomValues(length) {
87
+ return crypto.getRandomValues(new Uint8Array(length));
88
+ }
89
+ // ---------------- PRIVATE METHODS -------------------
90
+ createECDHSharedSecret(theirPublicKey, ourPrivateKey) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ const publicKey = yield crypto.subtle.importKey('raw', utils_1.Utils.fromHexToArray(theirPublicKey), crypto_service_1.ECDH, false, []);
93
+ const secret = yield crypto.subtle.deriveBits({
94
+ name: crypto_service_1.ECDH.name,
95
+ public: publicKey,
96
+ }, ourPrivateKey, 256);
97
+ return secret;
98
+ });
99
+ }
100
+ }
101
+ exports.CryptoService = CryptoService;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.EventService = void 0;
7
+ const eventemitter3_1 = __importDefault(require("eventemitter3"));
8
+ class EventService {
9
+ constructor(logger) {
10
+ this.events = new eventemitter3_1.default();
11
+ this.logger = logger;
12
+ }
13
+ emit(event, args) {
14
+ this.logger.debug(`Event '${event}' fired`);
15
+ // console.log(`Event '${event}' fired`)
16
+ return this.events.emit(event, args);
17
+ }
18
+ on(event, handler) {
19
+ return this.events.on(event, handler);
20
+ }
21
+ once(event, handler) {
22
+ return this.events.once(event, handler);
23
+ }
24
+ removeListener(event, handler) {
25
+ this.events.removeListener(event, handler);
26
+ }
27
+ removeAllListeners() {
28
+ this.events.removeAllListeners();
29
+ }
30
+ }
31
+ exports.EventService = EventService;