@authup/server-core 0.29.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 (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +23 -0
  3. package/dist/config/index.d.ts +4 -0
  4. package/dist/config/index.d.ts.map +1 -0
  5. package/dist/config/read.d.ts +3 -0
  6. package/dist/config/read.d.ts.map +1 -0
  7. package/dist/config/type.d.ts +5 -0
  8. package/dist/config/type.d.ts.map +1 -0
  9. package/dist/config/utils.d.ts +2 -0
  10. package/dist/config/utils.d.ts.map +1 -0
  11. package/dist/crypto/hash/compare.d.ts +2 -0
  12. package/dist/crypto/hash/compare.d.ts.map +1 -0
  13. package/dist/crypto/hash/hash.d.ts +2 -0
  14. package/dist/crypto/hash/hash.d.ts.map +1 -0
  15. package/dist/crypto/hash/index.d.ts +3 -0
  16. package/dist/crypto/hash/index.d.ts.map +1 -0
  17. package/dist/crypto/index.d.ts +4 -0
  18. package/dist/crypto/index.d.ts.map +1 -0
  19. package/dist/crypto/json-web-token/decode.d.ts +15 -0
  20. package/dist/crypto/json-web-token/decode.d.ts.map +1 -0
  21. package/dist/crypto/json-web-token/index.d.ts +5 -0
  22. package/dist/crypto/json-web-token/index.d.ts.map +1 -0
  23. package/dist/crypto/json-web-token/sign.d.ts +4 -0
  24. package/dist/crypto/json-web-token/sign.d.ts.map +1 -0
  25. package/dist/crypto/json-web-token/type.d.ts +32 -0
  26. package/dist/crypto/json-web-token/type.d.ts.map +1 -0
  27. package/dist/crypto/json-web-token/utils.d.ts +2 -0
  28. package/dist/crypto/json-web-token/utils.d.ts.map +1 -0
  29. package/dist/crypto/json-web-token/verify.d.ts +15 -0
  30. package/dist/crypto/json-web-token/verify.d.ts.map +1 -0
  31. package/dist/crypto/key-pair/constants.d.ts +5 -0
  32. package/dist/crypto/key-pair/constants.d.ts.map +1 -0
  33. package/dist/crypto/key-pair/create.d.ts +3 -0
  34. package/dist/crypto/key-pair/create.d.ts.map +1 -0
  35. package/dist/crypto/key-pair/delete.d.ts +3 -0
  36. package/dist/crypto/key-pair/delete.d.ts.map +1 -0
  37. package/dist/crypto/key-pair/index.d.ts +9 -0
  38. package/dist/crypto/key-pair/index.d.ts.map +1 -0
  39. package/dist/crypto/key-pair/load.d.ts +3 -0
  40. package/dist/crypto/key-pair/load.d.ts.map +1 -0
  41. package/dist/crypto/key-pair/module.d.ts +3 -0
  42. package/dist/crypto/key-pair/module.d.ts.map +1 -0
  43. package/dist/crypto/key-pair/save.d.ts +3 -0
  44. package/dist/crypto/key-pair/save.d.ts.map +1 -0
  45. package/dist/crypto/key-pair/type.d.ts +65 -0
  46. package/dist/crypto/key-pair/type.d.ts.map +1 -0
  47. package/dist/crypto/key-pair/utils/check.d.ts +3 -0
  48. package/dist/crypto/key-pair/utils/check.d.ts.map +1 -0
  49. package/dist/crypto/key-pair/utils/file-name.d.ts +4 -0
  50. package/dist/crypto/key-pair/utils/file-name.d.ts.map +1 -0
  51. package/dist/crypto/key-pair/utils/index.d.ts +5 -0
  52. package/dist/crypto/key-pair/utils/index.d.ts.map +1 -0
  53. package/dist/crypto/key-pair/utils/options.d.ts +3 -0
  54. package/dist/crypto/key-pair/utils/options.d.ts.map +1 -0
  55. package/dist/crypto/key-pair/utils/private-key.d.ts +4 -0
  56. package/dist/crypto/key-pair/utils/private-key.d.ts.map +1 -0
  57. package/dist/domain-event/index.d.ts +5 -0
  58. package/dist/domain-event/index.d.ts.map +1 -0
  59. package/dist/domain-event/publish.d.ts +4 -0
  60. package/dist/domain-event/publish.d.ts.map +1 -0
  61. package/dist/domain-event/redis/index.d.ts +2 -0
  62. package/dist/domain-event/redis/index.d.ts.map +1 -0
  63. package/dist/domain-event/redis/publish.d.ts +4 -0
  64. package/dist/domain-event/redis/publish.d.ts.map +1 -0
  65. package/dist/domain-event/socket/index.d.ts +3 -0
  66. package/dist/domain-event/socket/index.d.ts.map +1 -0
  67. package/dist/domain-event/socket/publish.d.ts +4 -0
  68. package/dist/domain-event/socket/publish.d.ts.map +1 -0
  69. package/dist/domain-event/socket/singleton.d.ts +4 -0
  70. package/dist/domain-event/socket/singleton.d.ts.map +1 -0
  71. package/dist/domain-event/socket/type.d.ts +5 -0
  72. package/dist/domain-event/socket/type.d.ts.map +1 -0
  73. package/dist/domain-event/type.d.ts +7 -0
  74. package/dist/domain-event/type.d.ts.map +1 -0
  75. package/dist/domain-event/utils.d.ts +4 -0
  76. package/dist/domain-event/utils.d.ts.map +1 -0
  77. package/dist/domains/index.d.ts +2 -0
  78. package/dist/domains/index.d.ts.map +1 -0
  79. package/dist/domains/robot/index.d.ts +2 -0
  80. package/dist/domains/robot/index.d.ts.map +1 -0
  81. package/dist/domains/robot/vault.d.ts +5 -0
  82. package/dist/domains/robot/vault.d.ts.map +1 -0
  83. package/dist/index.cjs +873 -0
  84. package/dist/index.cjs.map +1 -0
  85. package/dist/index.d.ts +10 -0
  86. package/dist/index.d.ts.map +1 -0
  87. package/dist/index.mjs +831 -0
  88. package/dist/index.mjs.map +1 -0
  89. package/dist/logger/index.d.ts +4 -0
  90. package/dist/logger/index.d.ts.map +1 -0
  91. package/dist/logger/module.d.ts +4 -0
  92. package/dist/logger/module.d.ts.map +1 -0
  93. package/dist/logger/presets/index.d.ts +2 -0
  94. package/dist/logger/presets/index.d.ts.map +1 -0
  95. package/dist/logger/presets/void.d.ts +16 -0
  96. package/dist/logger/presets/void.d.ts.map +1 -0
  97. package/dist/logger/type.d.ts +14 -0
  98. package/dist/logger/type.d.ts.map +1 -0
  99. package/dist/proxy/index.d.ts +3 -0
  100. package/dist/proxy/index.d.ts.map +1 -0
  101. package/dist/proxy/module.d.ts +24 -0
  102. package/dist/proxy/module.d.ts.map +1 -0
  103. package/dist/proxy/utils.d.ts +3 -0
  104. package/dist/proxy/utils.d.ts.map +1 -0
  105. package/dist/smtp/config/index.d.ts +2 -0
  106. package/dist/smtp/config/index.d.ts.map +1 -0
  107. package/dist/smtp/config/singleton.d.ts +5 -0
  108. package/dist/smtp/config/singleton.d.ts.map +1 -0
  109. package/dist/smtp/index.d.ts +5 -0
  110. package/dist/smtp/index.d.ts.map +1 -0
  111. package/dist/smtp/module.d.ts +4 -0
  112. package/dist/smtp/module.d.ts.map +1 -0
  113. package/dist/smtp/singleton.d.ts +3 -0
  114. package/dist/smtp/singleton.d.ts.map +1 -0
  115. package/dist/smtp/type.d.ts +14 -0
  116. package/dist/smtp/type.d.ts.map +1 -0
  117. package/dist/utils/env.d.ts +7 -0
  118. package/dist/utils/env.d.ts.map +1 -0
  119. package/dist/utils/has-property.d.ts +2 -0
  120. package/dist/utils/has-property.d.ts.map +1 -0
  121. package/dist/utils/index.d.ts +3 -0
  122. package/dist/utils/index.d.ts.map +1 -0
  123. package/dist/vault/index.d.ts +2 -0
  124. package/dist/vault/index.d.ts.map +1 -0
  125. package/dist/vault/module.d.ts +4 -0
  126. package/dist/vault/module.d.ts.map +1 -0
  127. package/package.json +79 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,873 @@
1
+ 'use strict';
2
+
3
+ var core = require('@authup/core');
4
+ var locter = require('locter');
5
+ var path = require('node:path');
6
+ var process$1 = require('node:process');
7
+ var smob = require('smob');
8
+ var bcrypt = require('bcrypt');
9
+ var node_crypto = require('node:crypto');
10
+ var fs = require('node:fs');
11
+ var jsonwebtoken = require('jsonwebtoken');
12
+ var redisExtension = require('redis-extension');
13
+ var redisEmitter = require('@socket.io/redis-emitter');
14
+ var vault = require('@hapic/vault');
15
+ var http = require('node:http');
16
+ var https = require('node:https');
17
+ var proxyFromEnv = require('proxy-from-env');
18
+ var nodemailer = require('nodemailer');
19
+
20
+ function hasProcessEnv(key) {
21
+ const keys = Array.isArray(key) ? key : [
22
+ key
23
+ ];
24
+ for(let i = 0; i < keys.length; i++){
25
+ if (core.hasOwnProperty(process$1.env, keys[i])) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ }
31
+ function readFromProcessEnv(key, alt) {
32
+ const keys = Array.isArray(key) ? key : [
33
+ key
34
+ ];
35
+ for(let i = 0; i < keys.length; i++){
36
+ if (core.hasOwnProperty(process$1.env, keys[i])) {
37
+ return process$1.env[keys[i]];
38
+ }
39
+ }
40
+ return alt;
41
+ }
42
+ function readBoolFromProcessEnv(key, alt) {
43
+ const keys = Array.isArray(key) ? key : [
44
+ key
45
+ ];
46
+ for(let i = 0; i < keys.length; i++){
47
+ const value = readFromProcessEnv(keys[i], alt);
48
+ switch(value){
49
+ case true:
50
+ case 'true':
51
+ case 't':
52
+ case '1':
53
+ return true;
54
+ case false:
55
+ case 'false':
56
+ case 'f':
57
+ case '0':
58
+ return false;
59
+ }
60
+ }
61
+ return alt;
62
+ }
63
+ function readBoolOrStringFromProcessEnv(key, alt) {
64
+ if (hasProcessEnv(key)) {
65
+ const value = readBoolFromProcessEnv(key, undefined);
66
+ if (typeof value === 'undefined') {
67
+ return readFromProcessEnv(key, alt);
68
+ }
69
+ return value;
70
+ }
71
+ return alt;
72
+ }
73
+ function readIntFromProcessEnv(key, alt) {
74
+ const keys = Array.isArray(key) ? key : [
75
+ key
76
+ ];
77
+ for(let i = 0; i < keys.length; i++){
78
+ const value = readFromProcessEnv(keys[i], alt);
79
+ const intValue = parseInt(`${value}`, 10);
80
+ if (!Number.isNaN(intValue)) {
81
+ return intValue;
82
+ }
83
+ }
84
+ return alt;
85
+ }
86
+
87
+ /*
88
+ * Copyright (c) 2022.
89
+ * Author Peter Placzek (tada5hi)
90
+ * For the full copyright and license information,
91
+ * view the LICENSE file that was distributed with this source code.
92
+ */ function hasOwnProperty(obj, prop) {
93
+ return Object.prototype.hasOwnProperty.call(obj, prop);
94
+ }
95
+
96
+ function buildWorkingDirectoryPathsForConfigFile() {
97
+ const cwd = [
98
+ process$1.cwd()
99
+ ];
100
+ if (hasProcessEnv('WRITABLE_DIRECTORY_PATH')) {
101
+ let writableDirectoryPath = readFromProcessEnv('WRITABLE_DIRECTORY_PATH');
102
+ if (!path.isAbsolute(writableDirectoryPath)) {
103
+ writableDirectoryPath = path.join(process$1.cwd(), writableDirectoryPath);
104
+ }
105
+ cwd.push(writableDirectoryPath);
106
+ } else {
107
+ cwd.push(path.join(process$1.cwd(), 'writable'));
108
+ }
109
+ return cwd;
110
+ }
111
+
112
+ async function readConfigFile(context) {
113
+ context = context || {};
114
+ const cwd = [];
115
+ if (context.cwd) {
116
+ const arr = Array.isArray(context.cwd) ? context.cwd : [
117
+ context.cwd
118
+ ];
119
+ for(let i = 0; i < arr.length; i++){
120
+ if (path.isAbsolute(arr[i])) {
121
+ cwd.push(arr[i]);
122
+ } else {
123
+ cwd.push(path.join(process$1.cwd(), arr[i]));
124
+ }
125
+ }
126
+ }
127
+ if (cwd.length === 0) {
128
+ cwd.push(...buildWorkingDirectoryPathsForConfigFile());
129
+ }
130
+ const locations = await locter.locateMany([
131
+ 'authup.*.{conf,js,mjs,cjs,ts,mts,mts}',
132
+ 'authup.{conf,js,mjs,cjs,ts,mts,mts}'
133
+ ], {
134
+ path: cwd
135
+ });
136
+ const content = {};
137
+ for(let i = 0; i < locations.length; i++){
138
+ const file = await locter.load(locations[i]);
139
+ const fileExport = locter.getModuleExport(file);
140
+ if (!core.isObject(fileExport.value)) {
141
+ continue;
142
+ }
143
+ const [root, name] = locations[i].name.split('.');
144
+ if (root && name) {
145
+ if (context.name === name) {
146
+ smob.merge(content, fileExport.value);
147
+ } else if (!context.name) {
148
+ content[name] = content[name] || {};
149
+ smob.merge(content[name], fileExport.value);
150
+ }
151
+ }
152
+ if (root && !name) {
153
+ if (context.name) {
154
+ if (core.isObject(fileExport.value[context.name])) {
155
+ smob.merge(content, fileExport.value[context.name]);
156
+ }
157
+ } else {
158
+ smob.merge(content, fileExport.value);
159
+ }
160
+ }
161
+ }
162
+ return content;
163
+ }
164
+
165
+ async function compare(value, hashedValue) {
166
+ return bcrypt.compare(value, hashedValue);
167
+ }
168
+
169
+ async function hash(str, saltOrRounds = 10) {
170
+ return bcrypt.hash(str, saltOrRounds);
171
+ }
172
+
173
+ /*
174
+ * Copyright (c) 2022.
175
+ * Author Peter Placzek (tada5hi)
176
+ * For the full copyright and license information,
177
+ * view the LICENSE file that was distributed with this source code.
178
+ */ exports.KeyPairKind = void 0;
179
+ (function(KeyPairKind) {
180
+ KeyPairKind["PRIVATE"] = 'private';
181
+ KeyPairKind["PUBLIC"] = 'public';
182
+ })(exports.KeyPairKind || (exports.KeyPairKind = {}));
183
+
184
+ function isKeyPair(data) {
185
+ return typeof data === 'object' && data !== null && core.hasOwnProperty(data, 'privateKey') && core.hasOwnProperty(data, 'publicKey');
186
+ }
187
+
188
+ function extendKeyPairOptions(options) {
189
+ var _options;
190
+ options = options ?? {};
191
+ options.directory = options.directory || process.cwd();
192
+ options.directory = path.isAbsolute(options.directory) ? options.directory : path.resolve(process.cwd(), options.directory);
193
+ (_options = options).type ?? (_options.type = 'rsa');
194
+ if (options.type === 'rsa' || options.type === 'rsa-pss' || options.type === 'dsa') {
195
+ options.modulusLength = 2048;
196
+ }
197
+ if (!options.privateKeyEncoding) {
198
+ options.privateKeyEncoding = {
199
+ type: 'pkcs8',
200
+ format: 'pem'
201
+ };
202
+ }
203
+ if (!options.publicKeyEncoding) {
204
+ options.publicKeyEncoding = {
205
+ type: 'spki',
206
+ format: 'pem'
207
+ };
208
+ }
209
+ if (options.privateKeyEncoding.passphrase && !options.privateKeyEncoding.cipher) {
210
+ options.privateKeyEncoding.cipher = 'aes-256-cbc';
211
+ }
212
+ return options;
213
+ }
214
+
215
+ function buildKeyFileName(type, context) {
216
+ const options = extendKeyPairOptions(context);
217
+ const parts = [];
218
+ switch(type){
219
+ case exports.KeyPairKind.PRIVATE:
220
+ {
221
+ if (options.privateName) {
222
+ parts.push(options.privateName);
223
+ } else {
224
+ parts.push(type);
225
+ }
226
+ if (options.privateExtension) {
227
+ if (options.privateExtension.startsWith('.')) {
228
+ options.privateExtension = options.privateExtension.slice(1);
229
+ }
230
+ parts.push(options.privateExtension);
231
+ } else {
232
+ parts.push('pem');
233
+ }
234
+ break;
235
+ }
236
+ case exports.KeyPairKind.PUBLIC:
237
+ {
238
+ if (options.publicName) {
239
+ parts.push(options.publicName);
240
+ } else {
241
+ parts.push(type);
242
+ }
243
+ if (options.publicExtension) {
244
+ if (options.publicExtension.startsWith('.')) {
245
+ options.publicExtension = options.publicExtension.slice(1);
246
+ }
247
+ parts.push(options.publicExtension);
248
+ } else {
249
+ parts.push('pem');
250
+ }
251
+ break;
252
+ }
253
+ }
254
+ return parts.join('.');
255
+ }
256
+
257
+ function decryptRSAPrivateKey(context, key) {
258
+ const privateKey = node_crypto.createPrivateKey({
259
+ type: context.privateKeyEncoding.type,
260
+ format: context.privateKeyEncoding.format,
261
+ key,
262
+ passphrase: context.privateKeyEncoding.passphrase || context.passphrase
263
+ });
264
+ let content = privateKey.export({
265
+ type: context.privateKeyEncoding.type,
266
+ format: context.privateKeyEncoding.format
267
+ });
268
+ if (typeof content !== 'string') {
269
+ content = Buffer.from(content).toString('utf-8');
270
+ }
271
+ return content;
272
+ }
273
+
274
+ async function saveKeyPair(keyPair, context) {
275
+ context = extendKeyPairOptions(context);
276
+ await fs.promises.mkdir(context.directory, {
277
+ recursive: true
278
+ });
279
+ await Promise.all([
280
+ {
281
+ path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, context)),
282
+ content: keyPair.privateKey
283
+ },
284
+ {
285
+ path: path.resolve(context.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, context)),
286
+ content: keyPair.publicKey
287
+ }
288
+ ].map((file)=>fs.promises.writeFile(file.path, file.content)));
289
+ return keyPair;
290
+ }
291
+
292
+ async function createKeyPair(context) {
293
+ const options = extendKeyPairOptions(context);
294
+ const keyPair = await new Promise((resolve, reject)=>{
295
+ const callback = (err, publicKey, privateKey)=>{
296
+ if (err) reject(err);
297
+ resolve({
298
+ privateKey,
299
+ publicKey
300
+ });
301
+ };
302
+ switch(options.type){
303
+ case 'dsa':
304
+ node_crypto.generateKeyPair(options.type, options, callback);
305
+ break;
306
+ case 'ec':
307
+ node_crypto.generateKeyPair(options.type, options, callback);
308
+ break;
309
+ case 'rsa':
310
+ node_crypto.generateKeyPair(options.type, options, callback);
311
+ break;
312
+ case 'rsa-pss':
313
+ node_crypto.generateKeyPair(options.type, options, callback);
314
+ break;
315
+ }
316
+ });
317
+ if (options.save) {
318
+ await saveKeyPair(keyPair, options);
319
+ }
320
+ if (options.passphrase || options.privateKeyEncoding.passphrase) {
321
+ keyPair.privateKey = decryptRSAPrivateKey(options, keyPair.privateKey);
322
+ }
323
+ return keyPair;
324
+ }
325
+
326
+ async function deleteKeyPair(context) {
327
+ const options = extendKeyPairOptions(context);
328
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
329
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
330
+ try {
331
+ await Promise.all([
332
+ privateKeyPath,
333
+ publicKeyPath
334
+ ].map((filePath)=>fs.promises.stat(filePath)));
335
+ } catch (e) {
336
+ return;
337
+ }
338
+ await Promise.all([
339
+ privateKeyPath,
340
+ publicKeyPath
341
+ ].map((filePath)=>fs.promises.rm(filePath)));
342
+ }
343
+
344
+ async function loadKeyPair(context) {
345
+ const options = extendKeyPairOptions(context);
346
+ const privateKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PRIVATE, options));
347
+ try {
348
+ await fs.promises.stat(privateKeyPath);
349
+ } catch (e) {
350
+ return undefined;
351
+ }
352
+ const privateKeyBuffer = await fs.promises.readFile(privateKeyPath);
353
+ let privateKey = privateKeyBuffer.toString();
354
+ if (options.passphrase || options.privateKeyEncoding.passphrase) {
355
+ privateKey = decryptRSAPrivateKey(options, privateKey);
356
+ }
357
+ const publicKeyPath = path.resolve(options.directory, buildKeyFileName(exports.KeyPairKind.PUBLIC, options));
358
+ let publicKey;
359
+ try {
360
+ await fs.promises.stat(publicKeyPath);
361
+ const publicKeyBuffer = await fs.promises.readFile(publicKeyPath);
362
+ publicKey = publicKeyBuffer.toString();
363
+ } catch (e) {
364
+ const publicKeyObject = node_crypto.createPublicKey({
365
+ key: privateKey,
366
+ format: options.privateKeyEncoding.format,
367
+ type: options.publicKeyEncoding.type
368
+ });
369
+ const stringOrBuffer = publicKeyObject.export({
370
+ format: options.publicKeyEncoding.format,
371
+ type: options.publicKeyEncoding.type
372
+ });
373
+ if (typeof stringOrBuffer !== 'string') {
374
+ publicKey = stringOrBuffer.toString();
375
+ } else {
376
+ publicKey = stringOrBuffer;
377
+ }
378
+ if (options.save) {
379
+ await saveKeyPair({
380
+ privateKey,
381
+ publicKey
382
+ }, options);
383
+ }
384
+ }
385
+ return {
386
+ privateKey,
387
+ publicKey
388
+ };
389
+ }
390
+
391
+ const keyPairCache = {};
392
+ async function useKeyPair(value) {
393
+ let options;
394
+ if (typeof value === 'string') {
395
+ options = extendKeyPairOptions({
396
+ privateName: value
397
+ });
398
+ } else {
399
+ options = extendKeyPairOptions(value || {});
400
+ }
401
+ if (Object.prototype.hasOwnProperty.call(keyPairCache, options.privateName)) {
402
+ return keyPairCache[options.privateName];
403
+ }
404
+ let keyPair = await loadKeyPair(options);
405
+ if (typeof keyPair === 'undefined') {
406
+ keyPair = await createKeyPair(options);
407
+ }
408
+ keyPairCache[options.privateName] = keyPair;
409
+ return keyPair;
410
+ }
411
+
412
+ function handleJWTError(e) {
413
+ if (e && typeof e === 'object' && core.hasOwnProperty(e, 'name')) {
414
+ switch(e.name){
415
+ case 'TokenExpiredError':
416
+ throw core.TokenError.expired();
417
+ case 'NotBeforeError':
418
+ {
419
+ if (core.hasOwnProperty(e, 'date') && typeof e.date === 'string') {
420
+ throw core.TokenError.notActiveBefore(e.date);
421
+ }
422
+ break;
423
+ }
424
+ case 'JsonWebTokenError':
425
+ {
426
+ if (core.hasOwnProperty(e, 'message') && typeof e.message === 'string') {
427
+ throw core.TokenError.payloadInvalid(e.message);
428
+ }
429
+ }
430
+ }
431
+ }
432
+ throw new core.TokenError({
433
+ previous: e,
434
+ decorateMessage: true,
435
+ logMessage: true,
436
+ message: 'The JWT error could not be determined.'
437
+ });
438
+ }
439
+
440
+ function decodeToken(token, options) {
441
+ options ?? (options = {});
442
+ try {
443
+ return jsonwebtoken.decode(token, {
444
+ ...options
445
+ });
446
+ } catch (e) {
447
+ handleJWTError(e);
448
+ throw e;
449
+ }
450
+ }
451
+
452
+ async function signToken(payload, context) {
453
+ context.expiresIn = context.expiresIn || 3600;
454
+ switch(context.type){
455
+ case core.KeyType.RSA:
456
+ case core.KeyType.EC:
457
+ {
458
+ const { type , keyPair , ...options } = context;
459
+ const { privateKey } = isKeyPair(keyPair) ? keyPair : await useKeyPair(keyPair);
460
+ if (type === core.KeyType.RSA) {
461
+ options.algorithm = options.algorithm || 'RS256';
462
+ } else {
463
+ options.algorithm = options.algorithm || 'ES256';
464
+ }
465
+ return jsonwebtoken.sign(payload, privateKey, options);
466
+ }
467
+ case core.KeyType.OCT:
468
+ {
469
+ const { type , secret , ...options } = context;
470
+ options.algorithm = options.algorithm || 'HS256';
471
+ return jsonwebtoken.sign(payload, secret, options);
472
+ }
473
+ }
474
+ throw new core.TokenError();
475
+ }
476
+
477
+ async function verifyToken(token, context) {
478
+ try {
479
+ switch(context.type){
480
+ case core.KeyType.RSA:
481
+ case core.KeyType.EC:
482
+ {
483
+ const { type , keyPair , ...options } = context;
484
+ const { publicKey } = isKeyPair(keyPair) ? keyPair : await useKeyPair(keyPair);
485
+ if (type === core.KeyType.RSA) {
486
+ options.algorithms = options.algorithms || [
487
+ 'RS256',
488
+ 'RS384',
489
+ 'RS512',
490
+ 'PS256',
491
+ 'PS384',
492
+ 'PS512'
493
+ ];
494
+ } else {
495
+ options.algorithms = options.algorithms || [
496
+ 'ES256',
497
+ 'ES384',
498
+ 'ES512'
499
+ ];
500
+ }
501
+ return jsonwebtoken.verify(token, publicKey, {
502
+ ...options
503
+ });
504
+ }
505
+ case core.KeyType.OCT:
506
+ {
507
+ const { type , secret , ...options } = context;
508
+ options.algorithms = options.algorithms || [
509
+ 'HS256',
510
+ 'HS384',
511
+ 'HS512'
512
+ ];
513
+ return jsonwebtoken.verify(token, secret, {
514
+ ...options
515
+ });
516
+ }
517
+ }
518
+ } catch (e) {
519
+ handleJWTError(e);
520
+ throw e;
521
+ }
522
+ throw new core.TokenError({
523
+ message: 'Invalid type.'
524
+ });
525
+ }
526
+
527
+ function transformDomainEventData(input) {
528
+ if (core.isObject(input)) {
529
+ const keys = Object.keys(input);
530
+ for(let i = 0; i < keys.length; i++){
531
+ const value = input[keys[i]];
532
+ if (value instanceof Date) {
533
+ input[keys[i]] = value.toISOString();
534
+ }
535
+ }
536
+ }
537
+ return input;
538
+ }
539
+ function buildDomainEventChannelName(input, id) {
540
+ if (typeof input === 'string') {
541
+ return input;
542
+ }
543
+ return input(id);
544
+ }
545
+
546
+ async function publishDomainRedisEvent(context, destinations) {
547
+ if (!redisExtension.hasClient() && !redisExtension.hasConfig()) {
548
+ return Promise.resolve();
549
+ }
550
+ context = transformDomainEventData(context);
551
+ const json = JSON.stringify(context);
552
+ const client = redisExtension.useClient();
553
+ const pipeline = client.pipeline();
554
+ for(let i = 0; i < destinations.length; i++){
555
+ const { namespace } = destinations[i];
556
+ const keyPrefix = namespace ? `${namespace}:` : '';
557
+ let key = keyPrefix + buildDomainEventChannelName(destinations[i].channel);
558
+ pipeline.publish(key, json);
559
+ if (context.event !== core.DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
560
+ key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
561
+ pipeline.publish(key, json);
562
+ }
563
+ }
564
+ return pipeline.exec();
565
+ }
566
+
567
+ let instance$3;
568
+ function useSocketEmitter() {
569
+ if (typeof instance$3 !== 'undefined') {
570
+ return instance$3;
571
+ }
572
+ instance$3 = new redisEmitter.Emitter(redisExtension.useClient());
573
+ return instance$3;
574
+ }
575
+
576
+ function publishDomainSocketEvent(context, destinations) {
577
+ if (!redisExtension.hasClient() && !redisExtension.hasConfig()) {
578
+ return;
579
+ }
580
+ context = transformDomainEventData(context);
581
+ for(let i = 0; i < destinations.length; i++){
582
+ let emitter = useSocketEmitter();
583
+ if (destinations[i].namespace) {
584
+ emitter = emitter.of(destinations[i].namespace);
585
+ }
586
+ let roomName = buildDomainEventChannelName(destinations[i].channel);
587
+ const fullEventName = core.buildDomainEventFullName(context.type, context.event);
588
+ emitter.in(roomName).emit(fullEventName, {
589
+ data: context,
590
+ meta: {
591
+ roomName
592
+ }
593
+ });
594
+ if (context.event !== core.DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
595
+ roomName = buildDomainEventChannelName(destinations[i].channel, context.data.id);
596
+ emitter.in(roomName).emit(fullEventName, {
597
+ data: context,
598
+ meta: {
599
+ roomName,
600
+ roomId: context.data.id
601
+ }
602
+ });
603
+ }
604
+ }
605
+ }
606
+
607
+ async function publishDomainEvent(context, destinations) {
608
+ await publishDomainRedisEvent(context, destinations);
609
+ publishDomainSocketEvent(context, destinations);
610
+ }
611
+
612
+ async function saveRobotCredentialsToVault(entity) {
613
+ if (!vault.hasConfig()) {
614
+ return;
615
+ }
616
+ const client = vault.useClient();
617
+ await client.keyValue.save('robots', entity.name, {
618
+ id: entity.id,
619
+ secret: entity.secret
620
+ });
621
+ }
622
+ async function removeRobotCredentialsFromVault(entity) {
623
+ if (!vault.hasConfig()) {
624
+ return;
625
+ }
626
+ const client = vault.useClient();
627
+ await client.keyValue.delete('robots', entity.name);
628
+ }
629
+ async function findRobotCredentialsInVault(entity) {
630
+ if (!vault.hasConfig()) {
631
+ return undefined;
632
+ }
633
+ const client = vault.useClient();
634
+ const response = await client.keyValue.find('robots', entity.name);
635
+ if (response && response.data) {
636
+ return response.data;
637
+ }
638
+ return undefined;
639
+ }
640
+
641
+ /*
642
+ * Copyright (c) 2022.
643
+ * Author Peter Placzek (tada5hi)
644
+ * For the full copyright and license information,
645
+ * view the LICENSE file that was distributed with this source code.
646
+ */ class VoidLogger {
647
+ error(message, ...meta) {
648
+ return this;
649
+ }
650
+ warn(message, ...meta) {
651
+ return this;
652
+ }
653
+ info(message, ...meta) {
654
+ return this;
655
+ }
656
+ http(message, ...meta) {
657
+ return this;
658
+ }
659
+ verbose(message, ...meta) {
660
+ return this;
661
+ }
662
+ debug(message, ...meta) {
663
+ return this;
664
+ }
665
+ }
666
+
667
+ let instance$2;
668
+ function useLogger() {
669
+ if (typeof instance$2 !== 'undefined') {
670
+ return instance$2;
671
+ }
672
+ instance$2 = new VoidLogger();
673
+ return instance$2;
674
+ }
675
+ function setLogger(logger) {
676
+ instance$2 = logger;
677
+ }
678
+
679
+ class ProxyClient {
680
+ /**
681
+ * Create a http agent for an url.
682
+ *
683
+ * @param input
684
+ */ async createAgent(input) {
685
+ return new Promise((resolve, reject)=>{
686
+ const headers = {};
687
+ if (this.options.user && this.options.password) {
688
+ headers['Proxy-Authorization'] = `Basic ${Buffer.from(`${this.options.user}:${this.options.password}`).toString('base64')}`;
689
+ }
690
+ const urlParsed = new URL(input);
691
+ const request = http.request({
692
+ host: this.options.host,
693
+ port: this.options.port,
694
+ method: 'CONNECT',
695
+ path: `${urlParsed.hostname}:443`,
696
+ headers
697
+ });
698
+ request.on('connect', (res, socket)=>{
699
+ if (res.statusCode >= 200 && res.statusCode < 300) {
700
+ resolve(new https.Agent({
701
+ socket,
702
+ ...this.agentOptions
703
+ }));
704
+ } else {
705
+ reject(new Error('Could not connect to proxy!'));
706
+ }
707
+ });
708
+ request.on('error', (err)=>{
709
+ reject(err);
710
+ });
711
+ request.on('timeout', (err)=>{
712
+ reject(err);
713
+ });
714
+ request.end();
715
+ });
716
+ }
717
+ constructor(options, agentOptions){
718
+ this.options = options;
719
+ this.agentOptions = agentOptions || {};
720
+ }
721
+ }
722
+
723
+ async function buildHTTPClientConfigForProxy(url) {
724
+ const connectionString = proxyFromEnv.getProxyForUrl(url);
725
+ if (connectionString) {
726
+ const connectionDetails = core.parseProxyConnectionString(connectionString);
727
+ const proxyClient = new ProxyClient({
728
+ host: connectionDetails.host,
729
+ port: connectionDetails.port || 3128,
730
+ ...connectionDetails.auth.username ? {
731
+ user: connectionDetails.auth.username
732
+ } : {},
733
+ ...connectionDetails.auth.password ? {
734
+ password: connectionDetails.auth.password
735
+ } : {}
736
+ });
737
+ const agent = await proxyClient.createAgent(url);
738
+ return {
739
+ httpAgent: agent,
740
+ httpsAgent: agent,
741
+ proxy: false
742
+ };
743
+ }
744
+ return {};
745
+ }
746
+
747
+ /*
748
+ * Copyright (c) 2022.
749
+ * Author Peter Placzek (tada5hi)
750
+ * For the full copyright and license information,
751
+ * view the LICENSE file that was distributed with this source code.
752
+ */ let instance$1;
753
+ function hasSmtpConfig() {
754
+ return !!instance$1;
755
+ }
756
+ function setSmtpConfig(value) {
757
+ instance$1 = value;
758
+ }
759
+ function useSmtpConfig() {
760
+ if (typeof instance$1 !== 'undefined') {
761
+ return instance$1;
762
+ }
763
+ instance$1 = {};
764
+ return instance$1;
765
+ }
766
+
767
+ function createSmtpClient(options) {
768
+ let transport;
769
+ options = options || {};
770
+ if (typeof options === 'string') {
771
+ transport = nodemailer.createTransport(options);
772
+ } else if (options.connectionString) {
773
+ transport = nodemailer.createTransport(options.connectionString);
774
+ } else {
775
+ let auth;
776
+ if (options.user && options.password) {
777
+ auth = {
778
+ type: 'login',
779
+ user: options.user,
780
+ pass: options.password
781
+ };
782
+ }
783
+ transport = nodemailer.createTransport({
784
+ host: options.host,
785
+ port: options.port,
786
+ auth,
787
+ secure: options.ssl,
788
+ opportunisticTLS: options.starttls,
789
+ tls: {
790
+ rejectUnauthorized: false
791
+ }
792
+ });
793
+ }
794
+ transport.on('error', (e)=>{
795
+ useLogger().error(e.message);
796
+ });
797
+ return transport;
798
+ }
799
+
800
+ let instance;
801
+ async function useSMTPClient() {
802
+ if (typeof instance !== 'undefined') {
803
+ return instance;
804
+ }
805
+ let options;
806
+ if (process.env.NODE_ENV === 'test') {
807
+ const testAccount = await nodemailer.createTestAccount();
808
+ options = {
809
+ host: 'smtp.ethereal.email',
810
+ port: 587,
811
+ ssl: false,
812
+ user: testAccount.user,
813
+ password: testAccount.pass
814
+ };
815
+ } else {
816
+ options = useSmtpConfig();
817
+ }
818
+ instance = createSmtpClient(options);
819
+ return instance;
820
+ }
821
+
822
+ function setVaultConfig(input) {
823
+ if (vault.hasClient()) {
824
+ vault.unsetClient();
825
+ }
826
+ vault.setConfig(input);
827
+ }
828
+ function hasVaultConfig() {
829
+ return vault.hasConfig();
830
+ }
831
+
832
+ exports.ProxyClient = ProxyClient;
833
+ exports.VoidLogger = VoidLogger;
834
+ exports.buildHTTPClientConfigForProxy = buildHTTPClientConfigForProxy;
835
+ exports.buildKeyFileName = buildKeyFileName;
836
+ exports.buildWorkingDirectoryPathsForConfigFile = buildWorkingDirectoryPathsForConfigFile;
837
+ exports.compare = compare;
838
+ exports.createKeyPair = createKeyPair;
839
+ exports.createSmtpClient = createSmtpClient;
840
+ exports.decodeToken = decodeToken;
841
+ exports.decryptRSAPrivateKey = decryptRSAPrivateKey;
842
+ exports.deleteKeyPair = deleteKeyPair;
843
+ exports.extendKeyPairOptions = extendKeyPairOptions;
844
+ exports.findRobotCredentialsInVault = findRobotCredentialsInVault;
845
+ exports.hasOwnProperty = hasOwnProperty;
846
+ exports.hasProcessEnv = hasProcessEnv;
847
+ exports.hasSmtpConfig = hasSmtpConfig;
848
+ exports.hasVaultConfig = hasVaultConfig;
849
+ exports.hash = hash;
850
+ exports.isKeyPair = isKeyPair;
851
+ exports.loadKeyPair = loadKeyPair;
852
+ exports.publishDomainEvent = publishDomainEvent;
853
+ exports.publishDomainRedisEvent = publishDomainRedisEvent;
854
+ exports.publishDomainSocketEvent = publishDomainSocketEvent;
855
+ exports.readBoolFromProcessEnv = readBoolFromProcessEnv;
856
+ exports.readBoolOrStringFromProcessEnv = readBoolOrStringFromProcessEnv;
857
+ exports.readConfigFile = readConfigFile;
858
+ exports.readFromProcessEnv = readFromProcessEnv;
859
+ exports.readIntFromProcessEnv = readIntFromProcessEnv;
860
+ exports.removeRobotCredentialsFromVault = removeRobotCredentialsFromVault;
861
+ exports.saveKeyPair = saveKeyPair;
862
+ exports.saveRobotCredentialsToVault = saveRobotCredentialsToVault;
863
+ exports.setLogger = setLogger;
864
+ exports.setSmtpConfig = setSmtpConfig;
865
+ exports.setVaultConfig = setVaultConfig;
866
+ exports.signToken = signToken;
867
+ exports.useKeyPair = useKeyPair;
868
+ exports.useLogger = useLogger;
869
+ exports.useSMTPClient = useSMTPClient;
870
+ exports.useSmtpConfig = useSmtpConfig;
871
+ exports.useSocketEmitter = useSocketEmitter;
872
+ exports.verifyToken = verifyToken;
873
+ //# sourceMappingURL=index.cjs.map