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