@authup/server-core 0.40.1 → 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 +7 -7
  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.mjs DELETED
@@ -1,805 +0,0 @@
1
- import path from 'node:path';
2
- import process$1 from 'node:process';
3
- import { hasOwnProperty as hasOwnProperty$1, isObject, TokenError, KeyType, DomainEventName, buildDomainEventFullName, parseProxyConnectionString } from '@authup/core';
4
- import { locateMany, load, getModuleExport } from 'locter';
5
- import { assign, isObject as isObject$1, merge } from 'smob';
6
- import { compare as compare$1, hash as hash$1 } from 'bcrypt';
7
- import { createPrivateKey, generateKeyPair, createPublicKey } from 'node:crypto';
8
- import fs from 'node:fs';
9
- import { decode, sign, verify } from 'jsonwebtoken';
10
- import { hasClient, hasConfig, useClient } from 'redis-extension';
11
- import { Emitter } from '@socket.io/redis-emitter';
12
- import http from 'node:http';
13
- import https from 'node:https';
14
- import { getProxyForUrl } from 'proxy-from-env';
15
- import { createTransport, createTestAccount } from 'nodemailer';
16
-
17
- function hasProcessEnv(key) {
18
- const keys = Array.isArray(key) ? key : [
19
- key
20
- ];
21
- for(let i = 0; i < keys.length; i++){
22
- if (hasOwnProperty$1(process$1.env, keys[i])) {
23
- return true;
24
- }
25
- }
26
- return false;
27
- }
28
- function readFromProcessEnv(key, alt) {
29
- const keys = Array.isArray(key) ? key : [
30
- key
31
- ];
32
- for(let i = 0; i < keys.length; i++){
33
- if (hasOwnProperty$1(process$1.env, keys[i])) {
34
- return process$1.env[keys[i]];
35
- }
36
- }
37
- return alt;
38
- }
39
- function readBoolFromProcessEnv(key, alt) {
40
- const keys = Array.isArray(key) ? key : [
41
- key
42
- ];
43
- for(let i = 0; i < keys.length; i++){
44
- const value = readFromProcessEnv(keys[i], alt);
45
- switch(value){
46
- case true:
47
- case 'true':
48
- case 't':
49
- case '1':
50
- return true;
51
- case false:
52
- case 'false':
53
- case 'f':
54
- case '0':
55
- return false;
56
- }
57
- }
58
- return alt;
59
- }
60
- function readBoolOrStringFromProcessEnv(key, alt) {
61
- if (hasProcessEnv(key)) {
62
- const value = readBoolFromProcessEnv(key, undefined);
63
- if (typeof value === 'undefined') {
64
- return readFromProcessEnv(key, alt);
65
- }
66
- return value;
67
- }
68
- return alt;
69
- }
70
- function readIntFromProcessEnv(key, alt) {
71
- const keys = Array.isArray(key) ? key : [
72
- key
73
- ];
74
- for(let i = 0; i < keys.length; i++){
75
- const value = readFromProcessEnv(keys[i], alt);
76
- const intValue = parseInt(`${value}`, 10);
77
- if (!Number.isNaN(intValue)) {
78
- return intValue;
79
- }
80
- }
81
- return alt;
82
- }
83
-
84
- /*
85
- * Copyright (c) 2022.
86
- * Author Peter Placzek (tada5hi)
87
- * For the full copyright and license information,
88
- * view the LICENSE file that was distributed with this source code.
89
- */ function hasOwnProperty(obj, prop) {
90
- return Object.prototype.hasOwnProperty.call(obj, prop);
91
- }
92
-
93
- function buildWorkingDirectoryPathsForConfigFile() {
94
- const cwd = [
95
- process$1.cwd()
96
- ];
97
- if (hasProcessEnv('WRITABLE_DIRECTORY_PATH')) {
98
- let writableDirectoryPath = readFromProcessEnv('WRITABLE_DIRECTORY_PATH');
99
- if (!path.isAbsolute(writableDirectoryPath)) {
100
- writableDirectoryPath = path.join(process$1.cwd(), writableDirectoryPath);
101
- }
102
- cwd.push(writableDirectoryPath);
103
- } else {
104
- cwd.push(path.join(process$1.cwd(), 'writable'));
105
- }
106
- return cwd;
107
- }
108
-
109
- async function readConfigFile(context) {
110
- context = context || {};
111
- const cwd = [];
112
- if (context.cwd) {
113
- const arr = Array.isArray(context.cwd) ? context.cwd : [
114
- context.cwd
115
- ];
116
- for(let i = 0; i < arr.length; i++){
117
- if (path.isAbsolute(arr[i])) {
118
- cwd.push(arr[i]);
119
- } else {
120
- cwd.push(path.join(process$1.cwd(), arr[i]));
121
- }
122
- }
123
- }
124
- if (cwd.length === 0) {
125
- cwd.push(...buildWorkingDirectoryPathsForConfigFile());
126
- }
127
- const locations = await locateMany([
128
- 'authup.*.{conf,js,mjs,cjs,ts,mts,mts}',
129
- 'authup.{conf,js,mjs,cjs,ts,mts,mts}'
130
- ], {
131
- path: cwd
132
- });
133
- const content = {};
134
- for(let i = 0; i < locations.length; i++){
135
- const file = await load(locations[i]);
136
- const fileExport = getModuleExport(file);
137
- if (!isObject(fileExport.value)) {
138
- continue;
139
- }
140
- const [root, name] = locations[i].name.split('.');
141
- if (root && name) {
142
- if (context.name === name) {
143
- assign(content, fileExport.value);
144
- } else if (!context.name) {
145
- content[name] = content[name] || {};
146
- assign(content[name], fileExport.value);
147
- }
148
- }
149
- if (root && !name) {
150
- if (context.name) {
151
- if (isObject(fileExport.value[context.name])) {
152
- assign(content, fileExport.value[context.name]);
153
- }
154
- } else {
155
- assign(content, fileExport.value);
156
- }
157
- }
158
- }
159
- return content;
160
- }
161
-
162
- async function compare(value, hashedValue) {
163
- return compare$1(value, hashedValue);
164
- }
165
-
166
- async function hash(str, saltOrRounds = 10) {
167
- return hash$1(str, saltOrRounds);
168
- }
169
-
170
- /*
171
- * Copyright (c) 2022.
172
- * Author Peter Placzek (tada5hi)
173
- * For the full copyright and license information,
174
- * view the LICENSE file that was distributed with this source code.
175
- */ var KeyPairKind;
176
- (function(KeyPairKind) {
177
- KeyPairKind["PRIVATE"] = 'private';
178
- KeyPairKind["PUBLIC"] = 'public';
179
- })(KeyPairKind || (KeyPairKind = {}));
180
-
181
- function isKeyPair(data) {
182
- return isObject(data) && typeof data.privateKey !== 'undefined' && typeof data.publicKey !== 'undefined';
183
- }
184
- function isKeyPairWithPublicKey(data) {
185
- return isObject(data) && typeof data.publicKey !== 'undefined';
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 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 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 = 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(KeyPairKind.PRIVATE, context)),
282
- content: keyPair.privateKey
283
- },
284
- {
285
- path: path.resolve(context.directory, buildKeyFileName(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
- generateKeyPair(options.type, options, callback);
305
- break;
306
- case 'ec':
307
- generateKeyPair(options.type, options, callback);
308
- break;
309
- case 'rsa':
310
- generateKeyPair(options.type, options, callback);
311
- break;
312
- case 'rsa-pss':
313
- 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(KeyPairKind.PRIVATE, options));
329
- const publicKeyPath = path.resolve(options.directory, buildKeyFileName(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(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(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 = 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 createErrorForJWTError(e) {
413
- if (isObject$1(e) && typeof e.name === 'string') {
414
- switch(e.name){
415
- case 'TokenExpiredError':
416
- {
417
- return TokenError.expired();
418
- }
419
- case 'NotBeforeError':
420
- {
421
- if (typeof e.date === 'string' || e.date instanceof Date) {
422
- return TokenError.notActiveBefore(e.date);
423
- }
424
- break;
425
- }
426
- case 'JsonWebTokenError':
427
- {
428
- if (typeof e.message === 'string') {
429
- return TokenError.payloadInvalid(e.message);
430
- }
431
- break;
432
- }
433
- }
434
- }
435
- return new TokenError({
436
- previous: e,
437
- decorateMessage: true,
438
- logMessage: true,
439
- message: 'The JWT error could not be determined.'
440
- });
441
- }
442
-
443
- function decodeToken(token, options) {
444
- options ?? (options = {});
445
- try {
446
- return decode(token, {
447
- ...options
448
- });
449
- } catch (e) {
450
- createErrorForJWTError(e);
451
- throw e;
452
- }
453
- }
454
-
455
- async function signToken(payload, context) {
456
- context.expiresIn = context.expiresIn || 3600;
457
- switch(context.type){
458
- case KeyType.RSA:
459
- case KeyType.EC:
460
- {
461
- const { type, keyPair, ...options } = context;
462
- const { privateKey } = isKeyPair(keyPair) ? keyPair : await useKeyPair(keyPair);
463
- if (type === KeyType.RSA) {
464
- options.algorithm = options.algorithm || 'RS256';
465
- } else {
466
- options.algorithm = options.algorithm || 'ES256';
467
- }
468
- return sign(payload, privateKey, options);
469
- }
470
- case KeyType.OCT:
471
- {
472
- const { type, secret, ...options } = context;
473
- options.algorithm = options.algorithm || 'HS256';
474
- return sign(payload, secret, options);
475
- }
476
- }
477
- throw new TokenError();
478
- }
479
-
480
- async function verifyToken(token, context) {
481
- let output;
482
- try {
483
- switch(context.type){
484
- case KeyType.RSA:
485
- case KeyType.EC:
486
- {
487
- const { type, keyPair, ...options } = context;
488
- const { publicKey } = isKeyPairWithPublicKey(keyPair) ? keyPair : await useKeyPair(keyPair);
489
- if (type === KeyType.RSA) {
490
- options.algorithms = options.algorithms || [
491
- 'RS256',
492
- 'RS384',
493
- 'RS512',
494
- 'PS256',
495
- 'PS384',
496
- 'PS512'
497
- ];
498
- } else {
499
- options.algorithms = options.algorithms || [
500
- 'ES256',
501
- 'ES384',
502
- 'ES512'
503
- ];
504
- }
505
- output = await verify(token, publicKey, {
506
- ...options
507
- });
508
- break;
509
- }
510
- case KeyType.OCT:
511
- {
512
- const { type, secret, ...options } = context;
513
- options.algorithms = options.algorithms || [
514
- 'HS256',
515
- 'HS384',
516
- 'HS512'
517
- ];
518
- output = await verify(token, secret, {
519
- ...options
520
- });
521
- }
522
- }
523
- } catch (e) {
524
- throw createErrorForJWTError(e);
525
- }
526
- if (typeof output === 'undefined') {
527
- throw new TokenError({
528
- message: 'Invalid type.'
529
- });
530
- }
531
- return output;
532
- }
533
-
534
- function transformDomainEventData(input) {
535
- if (isObject(input)) {
536
- const keys = Object.keys(input);
537
- for(let i = 0; i < keys.length; i++){
538
- const value = input[keys[i]];
539
- if (value instanceof Date) {
540
- input[keys[i]] = value.toISOString();
541
- }
542
- }
543
- }
544
- return input;
545
- }
546
- function buildDomainEventChannelName(input, id) {
547
- if (typeof input === 'string') {
548
- return input;
549
- }
550
- return input(id);
551
- }
552
-
553
- async function publishDomainRedisEvent(context, destinations) {
554
- if (!hasClient() && !hasConfig()) {
555
- return Promise.resolve();
556
- }
557
- context = transformDomainEventData(context);
558
- const json = JSON.stringify(context);
559
- const client = useClient();
560
- const pipeline = client.pipeline();
561
- for(let i = 0; i < destinations.length; i++){
562
- const { namespace } = destinations[i];
563
- const keyPrefix = namespace ? `${namespace}:` : '';
564
- let key = keyPrefix + buildDomainEventChannelName(destinations[i].channel);
565
- pipeline.publish(key, json);
566
- if (context.event !== DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
567
- key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
568
- pipeline.publish(key, json);
569
- }
570
- }
571
- return pipeline.exec();
572
- }
573
-
574
- let instance$3;
575
- function useSocketEmitter() {
576
- if (typeof instance$3 !== 'undefined') {
577
- return instance$3;
578
- }
579
- instance$3 = new Emitter(useClient());
580
- return instance$3;
581
- }
582
-
583
- function publishDomainSocketEvent(context, destinations) {
584
- if (!hasClient() && !hasConfig()) {
585
- return;
586
- }
587
- context = transformDomainEventData(context);
588
- for(let i = 0; i < destinations.length; i++){
589
- let emitter = useSocketEmitter();
590
- if (destinations[i].namespace) {
591
- emitter = emitter.of(destinations[i].namespace);
592
- }
593
- let roomName = buildDomainEventChannelName(destinations[i].channel);
594
- const fullEventName = buildDomainEventFullName(context.type, context.event);
595
- emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
596
- // @ts-ignore
597
- .emit(fullEventName, {
598
- ...context,
599
- meta: {
600
- roomName
601
- }
602
- });
603
- if (context.event !== DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
604
- roomName = buildDomainEventChannelName(destinations[i].channel, context.data.id);
605
- emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
606
- // @ts-ignore
607
- .emit(fullEventName, {
608
- ...context,
609
- meta: {
610
- roomName,
611
- roomId: context.data.id
612
- }
613
- });
614
- }
615
- }
616
- }
617
-
618
- async function publishDomainEvent(context, destinations) {
619
- await publishDomainRedisEvent(context, destinations);
620
- publishDomainSocketEvent(context, destinations);
621
- }
622
-
623
- /*
624
- * Copyright (c) 2022.
625
- * Author Peter Placzek (tada5hi)
626
- * For the full copyright and license information,
627
- * view the LICENSE file that was distributed with this source code.
628
- */ class VoidLogger {
629
- error(message, ...meta) {
630
- return this;
631
- }
632
- warn(message, ...meta) {
633
- return this;
634
- }
635
- info(message, ...meta) {
636
- return this;
637
- }
638
- http(message, ...meta) {
639
- return this;
640
- }
641
- verbose(message, ...meta) {
642
- return this;
643
- }
644
- debug(message, ...meta) {
645
- return this;
646
- }
647
- }
648
-
649
- let instance$2;
650
- function useLogger() {
651
- if (typeof instance$2 !== 'undefined') {
652
- return instance$2;
653
- }
654
- instance$2 = new VoidLogger();
655
- return instance$2;
656
- }
657
- function setLogger(logger) {
658
- instance$2 = logger;
659
- }
660
-
661
- class ProxyClient {
662
- /**
663
- * Create a http agent for an url.
664
- *
665
- * @param input
666
- */ async createAgent(input) {
667
- return new Promise((resolve, reject)=>{
668
- const headers = {};
669
- if (this.options.user && this.options.password) {
670
- headers['Proxy-Authorization'] = `Basic ${Buffer.from(`${this.options.user}:${this.options.password}`).toString('base64')}`;
671
- }
672
- const urlParsed = new URL(input);
673
- const request = http.request({
674
- host: this.options.host,
675
- port: this.options.port,
676
- method: 'CONNECT',
677
- path: `${urlParsed.hostname}:443`,
678
- headers
679
- });
680
- request.on('connect', (res, socket)=>{
681
- if (res.statusCode >= 200 && res.statusCode < 300) {
682
- resolve(new https.Agent({
683
- socket,
684
- ...this.agentOptions
685
- }));
686
- } else {
687
- reject(new Error('Could not connect to proxy!'));
688
- }
689
- });
690
- request.on('error', (err)=>{
691
- reject(err);
692
- });
693
- request.on('timeout', (err)=>{
694
- reject(err);
695
- });
696
- request.end();
697
- });
698
- }
699
- constructor(options, agentOptions){
700
- this.options = options;
701
- this.agentOptions = merge(agentOptions || {}, {
702
- keepAlive: false
703
- });
704
- }
705
- }
706
-
707
- async function buildHTTPClientConfigForProxy(url) {
708
- const connectionString = getProxyForUrl(url);
709
- if (connectionString) {
710
- const connectionDetails = parseProxyConnectionString(connectionString);
711
- const proxyClient = new ProxyClient({
712
- host: connectionDetails.host,
713
- port: connectionDetails.port || 3128,
714
- ...connectionDetails.auth.username ? {
715
- user: connectionDetails.auth.username
716
- } : {},
717
- ...connectionDetails.auth.password ? {
718
- password: connectionDetails.auth.password
719
- } : {}
720
- });
721
- const agent = await proxyClient.createAgent(url);
722
- return {
723
- agent
724
- };
725
- }
726
- return {};
727
- }
728
-
729
- /*
730
- * Copyright (c) 2022.
731
- * Author Peter Placzek (tada5hi)
732
- * For the full copyright and license information,
733
- * view the LICENSE file that was distributed with this source code.
734
- */ let instance$1;
735
- function hasSmtpConfig() {
736
- return !!instance$1;
737
- }
738
- function setSmtpConfig(value) {
739
- instance$1 = value;
740
- }
741
- function useSmtpConfig() {
742
- if (typeof instance$1 !== 'undefined') {
743
- return instance$1;
744
- }
745
- instance$1 = {};
746
- return instance$1;
747
- }
748
-
749
- function createSmtpClient(options) {
750
- let transport;
751
- options = options || {};
752
- if (typeof options === 'string') {
753
- transport = createTransport(options);
754
- } else if (options.connectionString) {
755
- transport = createTransport(options.connectionString);
756
- } else {
757
- let auth;
758
- if (options.user && options.password) {
759
- auth = {
760
- type: 'login',
761
- user: options.user,
762
- pass: options.password
763
- };
764
- }
765
- transport = createTransport({
766
- host: options.host,
767
- port: options.port,
768
- auth,
769
- secure: options.ssl,
770
- opportunisticTLS: options.starttls,
771
- tls: {
772
- rejectUnauthorized: false
773
- }
774
- });
775
- }
776
- transport.on('error', (e)=>{
777
- useLogger().error(e.message);
778
- });
779
- return transport;
780
- }
781
-
782
- let instance;
783
- async function useSMTPClient() {
784
- if (typeof instance !== 'undefined') {
785
- return instance;
786
- }
787
- let options;
788
- if (process.env.NODE_ENV === 'test') {
789
- const testAccount = await createTestAccount();
790
- options = {
791
- host: 'smtp.ethereal.email',
792
- port: 587,
793
- ssl: false,
794
- user: testAccount.user,
795
- password: testAccount.pass
796
- };
797
- } else {
798
- options = useSmtpConfig();
799
- }
800
- instance = createSmtpClient(options);
801
- return instance;
802
- }
803
-
804
- export { KeyPairKind, ProxyClient, VoidLogger, buildHTTPClientConfigForProxy, buildKeyFileName, buildWorkingDirectoryPathsForConfigFile, compare, createKeyPair, createSmtpClient, decodeToken, decryptRSAPrivateKey, deleteKeyPair, extendKeyPairOptions, hasOwnProperty, hasProcessEnv, hasSmtpConfig, hash, isKeyPair, isKeyPairWithPublicKey, loadKeyPair, publishDomainEvent, publishDomainRedisEvent, publishDomainSocketEvent, readBoolFromProcessEnv, readBoolOrStringFromProcessEnv, readConfigFile, readFromProcessEnv, readIntFromProcessEnv, saveKeyPair, setLogger, setSmtpConfig, signToken, useKeyPair, useLogger, useSMTPClient, useSmtpConfig, useSocketEmitter, verifyToken };
805
- //# sourceMappingURL=index.mjs.map