@authup/server-core 0.42.0 → 0.43.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.
- package/dist/config/index.d.ts +4 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/read.d.ts +3 -0
- package/dist/config/read.d.ts.map +1 -0
- package/dist/config/type.d.ts +5 -0
- package/dist/config/type.d.ts.map +1 -0
- package/dist/config/utils.d.ts +2 -0
- package/dist/config/utils.d.ts.map +1 -0
- package/dist/crypto/hash/compare.d.ts +2 -0
- package/dist/crypto/hash/compare.d.ts.map +1 -0
- package/dist/crypto/hash/hash.d.ts +2 -0
- package/dist/crypto/hash/hash.d.ts.map +1 -0
- package/dist/crypto/hash/index.d.ts +3 -0
- package/dist/crypto/hash/index.d.ts.map +1 -0
- package/dist/crypto/index.d.ts +4 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/json-web-token/decode.d.ts +15 -0
- package/dist/crypto/json-web-token/decode.d.ts.map +1 -0
- package/dist/crypto/json-web-token/index.d.ts +5 -0
- package/dist/crypto/json-web-token/index.d.ts.map +1 -0
- package/dist/crypto/json-web-token/sign.d.ts +4 -0
- package/dist/crypto/json-web-token/sign.d.ts.map +1 -0
- package/dist/crypto/json-web-token/type.d.ts +34 -0
- package/dist/crypto/json-web-token/type.d.ts.map +1 -0
- package/dist/crypto/json-web-token/utils.d.ts +3 -0
- package/dist/crypto/json-web-token/utils.d.ts.map +1 -0
- package/dist/crypto/json-web-token/verify.d.ts +15 -0
- package/dist/crypto/json-web-token/verify.d.ts.map +1 -0
- package/dist/crypto/key-pair/constants.d.ts +5 -0
- package/dist/crypto/key-pair/constants.d.ts.map +1 -0
- package/dist/crypto/key-pair/create.d.ts +3 -0
- package/dist/crypto/key-pair/create.d.ts.map +1 -0
- package/dist/crypto/key-pair/delete.d.ts +3 -0
- package/dist/crypto/key-pair/delete.d.ts.map +1 -0
- package/dist/crypto/key-pair/index.d.ts +9 -0
- package/dist/crypto/key-pair/index.d.ts.map +1 -0
- package/dist/crypto/key-pair/load.d.ts +3 -0
- package/dist/crypto/key-pair/load.d.ts.map +1 -0
- package/dist/crypto/key-pair/module.d.ts +3 -0
- package/dist/crypto/key-pair/module.d.ts.map +1 -0
- package/dist/crypto/key-pair/save.d.ts +3 -0
- package/dist/crypto/key-pair/save.d.ts.map +1 -0
- package/dist/crypto/key-pair/type.d.ts +65 -0
- package/dist/crypto/key-pair/type.d.ts.map +1 -0
- package/dist/crypto/key-pair/utils/check.d.ts +4 -0
- package/dist/crypto/key-pair/utils/check.d.ts.map +1 -0
- package/dist/crypto/key-pair/utils/file-name.d.ts +4 -0
- package/dist/crypto/key-pair/utils/file-name.d.ts.map +1 -0
- package/dist/crypto/key-pair/utils/index.d.ts +5 -0
- package/dist/crypto/key-pair/utils/index.d.ts.map +1 -0
- package/dist/crypto/key-pair/utils/options.d.ts +3 -0
- package/dist/crypto/key-pair/utils/options.d.ts.map +1 -0
- package/dist/crypto/key-pair/utils/private-key.d.ts +4 -0
- package/dist/crypto/key-pair/utils/private-key.d.ts.map +1 -0
- package/dist/domain-event/index.d.ts +5 -0
- package/dist/domain-event/index.d.ts.map +1 -0
- package/dist/domain-event/publish.d.ts +4 -0
- package/dist/domain-event/publish.d.ts.map +1 -0
- package/dist/domain-event/redis/index.d.ts +2 -0
- package/dist/domain-event/redis/index.d.ts.map +1 -0
- package/dist/domain-event/redis/publish.d.ts +4 -0
- package/dist/domain-event/redis/publish.d.ts.map +1 -0
- package/dist/domain-event/socket/index.d.ts +3 -0
- package/dist/domain-event/socket/index.d.ts.map +1 -0
- package/dist/domain-event/socket/publish.d.ts +4 -0
- package/dist/domain-event/socket/publish.d.ts.map +1 -0
- package/dist/domain-event/socket/singleton.d.ts +4 -0
- package/dist/domain-event/socket/singleton.d.ts.map +1 -0
- package/dist/domain-event/socket/type.d.ts +5 -0
- package/dist/domain-event/socket/type.d.ts.map +1 -0
- package/dist/domain-event/type.d.ts +7 -0
- package/dist/domain-event/type.d.ts.map +1 -0
- package/dist/domain-event/utils.d.ts +4 -0
- package/dist/domain-event/utils.d.ts.map +1 -0
- package/dist/index.cjs +855 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +817 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger/index.d.ts +4 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/logger/module.d.ts +4 -0
- package/dist/logger/module.d.ts.map +1 -0
- package/dist/logger/presets/index.d.ts +2 -0
- package/dist/logger/presets/index.d.ts.map +1 -0
- package/dist/logger/presets/void.d.ts +16 -0
- package/dist/logger/presets/void.d.ts.map +1 -0
- package/dist/logger/type.d.ts +14 -0
- package/dist/logger/type.d.ts.map +1 -0
- package/dist/proxy/index.d.ts +3 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/module.d.ts +24 -0
- package/dist/proxy/module.d.ts.map +1 -0
- package/dist/proxy/utils.d.ts +8 -0
- package/dist/proxy/utils.d.ts.map +1 -0
- package/dist/smtp/config/index.d.ts +2 -0
- package/dist/smtp/config/index.d.ts.map +1 -0
- package/dist/smtp/config/singleton.d.ts +5 -0
- package/dist/smtp/config/singleton.d.ts.map +1 -0
- package/dist/smtp/index.d.ts +5 -0
- package/dist/smtp/index.d.ts.map +1 -0
- package/dist/smtp/module.d.ts +4 -0
- package/dist/smtp/module.d.ts.map +1 -0
- package/dist/smtp/singleton.d.ts +3 -0
- package/dist/smtp/singleton.d.ts.map +1 -0
- package/dist/smtp/type.d.ts +14 -0
- package/dist/smtp/type.d.ts.map +1 -0
- package/dist/utils/env.d.ts +7 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/has-property.d.ts +2 -0
- package/dist/utils/has-property.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/package.json +3 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,817 @@
|
|
|
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
|
+
cause: e,
|
|
437
|
+
logMessage: true,
|
|
438
|
+
message: 'The JWT error could not be determined.'
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function decodeToken(token, options) {
|
|
443
|
+
options ?? (options = {});
|
|
444
|
+
try {
|
|
445
|
+
return decode(token, {
|
|
446
|
+
...options
|
|
447
|
+
});
|
|
448
|
+
} catch (e) {
|
|
449
|
+
createErrorForJWTError(e);
|
|
450
|
+
throw e;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
async function signToken(payload, context) {
|
|
455
|
+
context.expiresIn = context.expiresIn || 3600;
|
|
456
|
+
switch(context.type){
|
|
457
|
+
case KeyType.RSA:
|
|
458
|
+
case KeyType.EC:
|
|
459
|
+
{
|
|
460
|
+
const { type, keyPair, ...options } = context;
|
|
461
|
+
const { privateKey } = isKeyPair(keyPair) ? keyPair : await useKeyPair(keyPair);
|
|
462
|
+
if (type === KeyType.RSA) {
|
|
463
|
+
options.algorithm = options.algorithm || 'RS256';
|
|
464
|
+
} else {
|
|
465
|
+
options.algorithm = options.algorithm || 'ES256';
|
|
466
|
+
}
|
|
467
|
+
return sign(payload, privateKey, options);
|
|
468
|
+
}
|
|
469
|
+
case KeyType.OCT:
|
|
470
|
+
{
|
|
471
|
+
const { type, secret, ...options } = context;
|
|
472
|
+
options.algorithm = options.algorithm || 'HS256';
|
|
473
|
+
return sign(payload, secret, options);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
throw new TokenError();
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async function verifyToken(token, context) {
|
|
480
|
+
let promise;
|
|
481
|
+
try {
|
|
482
|
+
switch(context.type){
|
|
483
|
+
case KeyType.RSA:
|
|
484
|
+
case KeyType.EC:
|
|
485
|
+
{
|
|
486
|
+
const { type, keyPair, ...options } = context;
|
|
487
|
+
const { publicKey } = isKeyPairWithPublicKey(keyPair) ? keyPair : await useKeyPair(keyPair);
|
|
488
|
+
if (type === KeyType.RSA) {
|
|
489
|
+
options.algorithms = options.algorithms || [
|
|
490
|
+
'RS256',
|
|
491
|
+
'RS384',
|
|
492
|
+
'RS512',
|
|
493
|
+
'PS256',
|
|
494
|
+
'PS384',
|
|
495
|
+
'PS512'
|
|
496
|
+
];
|
|
497
|
+
} else {
|
|
498
|
+
options.algorithms = options.algorithms || [
|
|
499
|
+
'ES256',
|
|
500
|
+
'ES384',
|
|
501
|
+
'ES512'
|
|
502
|
+
];
|
|
503
|
+
}
|
|
504
|
+
promise = new Promise((resolve, reject)=>{
|
|
505
|
+
verify(token, publicKey, options, (err, decoded)=>{
|
|
506
|
+
if (err) {
|
|
507
|
+
reject(err);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
resolve(decoded);
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
case KeyType.OCT:
|
|
516
|
+
{
|
|
517
|
+
const { type, secret, ...options } = context;
|
|
518
|
+
options.algorithms = options.algorithms || [
|
|
519
|
+
'HS256',
|
|
520
|
+
'HS384',
|
|
521
|
+
'HS512'
|
|
522
|
+
];
|
|
523
|
+
promise = new Promise((resolve, reject)=>{
|
|
524
|
+
verify(token, secret, options, (err, decoded)=>{
|
|
525
|
+
if (err) {
|
|
526
|
+
reject(err);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
resolve(decoded);
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
} catch (e) {
|
|
535
|
+
throw createErrorForJWTError(e);
|
|
536
|
+
}
|
|
537
|
+
const output = await promise;
|
|
538
|
+
if (typeof output === 'undefined') {
|
|
539
|
+
throw new TokenError({
|
|
540
|
+
message: 'Invalid type.'
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
return output;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function transformDomainEventData(input) {
|
|
547
|
+
if (isObject(input)) {
|
|
548
|
+
const keys = Object.keys(input);
|
|
549
|
+
for(let i = 0; i < keys.length; i++){
|
|
550
|
+
const value = input[keys[i]];
|
|
551
|
+
if (value instanceof Date) {
|
|
552
|
+
input[keys[i]] = value.toISOString();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return input;
|
|
557
|
+
}
|
|
558
|
+
function buildDomainEventChannelName(input, id) {
|
|
559
|
+
if (typeof input === 'string') {
|
|
560
|
+
return input;
|
|
561
|
+
}
|
|
562
|
+
return input(id);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
async function publishDomainRedisEvent(context, destinations) {
|
|
566
|
+
if (!hasClient() && !hasConfig()) {
|
|
567
|
+
return Promise.resolve();
|
|
568
|
+
}
|
|
569
|
+
context = transformDomainEventData(context);
|
|
570
|
+
const json = JSON.stringify(context);
|
|
571
|
+
const client = useClient();
|
|
572
|
+
const pipeline = client.pipeline();
|
|
573
|
+
for(let i = 0; i < destinations.length; i++){
|
|
574
|
+
const { namespace } = destinations[i];
|
|
575
|
+
const keyPrefix = namespace ? `${namespace}:` : '';
|
|
576
|
+
let key = keyPrefix + buildDomainEventChannelName(destinations[i].channel);
|
|
577
|
+
pipeline.publish(key, json);
|
|
578
|
+
if (context.event !== DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
|
|
579
|
+
key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
|
|
580
|
+
pipeline.publish(key, json);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
return pipeline.exec();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
let instance$3;
|
|
587
|
+
function useSocketEmitter() {
|
|
588
|
+
if (typeof instance$3 !== 'undefined') {
|
|
589
|
+
return instance$3;
|
|
590
|
+
}
|
|
591
|
+
instance$3 = new Emitter(useClient());
|
|
592
|
+
return instance$3;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function publishDomainSocketEvent(context, destinations) {
|
|
596
|
+
if (!hasClient() && !hasConfig()) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
context = transformDomainEventData(context);
|
|
600
|
+
for(let i = 0; i < destinations.length; i++){
|
|
601
|
+
let emitter = useSocketEmitter();
|
|
602
|
+
if (destinations[i].namespace) {
|
|
603
|
+
emitter = emitter.of(destinations[i].namespace);
|
|
604
|
+
}
|
|
605
|
+
let roomName = buildDomainEventChannelName(destinations[i].channel);
|
|
606
|
+
const fullEventName = buildDomainEventFullName(context.type, context.event);
|
|
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
|
+
}
|
|
614
|
+
});
|
|
615
|
+
if (context.event !== DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
|
|
616
|
+
roomName = buildDomainEventChannelName(destinations[i].channel, context.data.id);
|
|
617
|
+
emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
618
|
+
// @ts-ignore
|
|
619
|
+
.emit(fullEventName, {
|
|
620
|
+
...context,
|
|
621
|
+
meta: {
|
|
622
|
+
roomName,
|
|
623
|
+
roomId: context.data.id
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async function publishDomainEvent(context, destinations) {
|
|
631
|
+
await publishDomainRedisEvent(context, destinations);
|
|
632
|
+
publishDomainSocketEvent(context, destinations);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/*
|
|
636
|
+
* Copyright (c) 2022.
|
|
637
|
+
* Author Peter Placzek (tada5hi)
|
|
638
|
+
* For the full copyright and license information,
|
|
639
|
+
* view the LICENSE file that was distributed with this source code.
|
|
640
|
+
*/ class VoidLogger {
|
|
641
|
+
error(message, ...meta) {
|
|
642
|
+
return this;
|
|
643
|
+
}
|
|
644
|
+
warn(message, ...meta) {
|
|
645
|
+
return this;
|
|
646
|
+
}
|
|
647
|
+
info(message, ...meta) {
|
|
648
|
+
return this;
|
|
649
|
+
}
|
|
650
|
+
http(message, ...meta) {
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
verbose(message, ...meta) {
|
|
654
|
+
return this;
|
|
655
|
+
}
|
|
656
|
+
debug(message, ...meta) {
|
|
657
|
+
return this;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
let instance$2;
|
|
662
|
+
function useLogger() {
|
|
663
|
+
if (typeof instance$2 !== 'undefined') {
|
|
664
|
+
return instance$2;
|
|
665
|
+
}
|
|
666
|
+
instance$2 = new VoidLogger();
|
|
667
|
+
return instance$2;
|
|
668
|
+
}
|
|
669
|
+
function setLogger(logger) {
|
|
670
|
+
instance$2 = logger;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
class ProxyClient {
|
|
674
|
+
/**
|
|
675
|
+
* Create a http agent for an url.
|
|
676
|
+
*
|
|
677
|
+
* @param input
|
|
678
|
+
*/ async createAgent(input) {
|
|
679
|
+
return new Promise((resolve, reject)=>{
|
|
680
|
+
const headers = {};
|
|
681
|
+
if (this.options.user && this.options.password) {
|
|
682
|
+
headers['Proxy-Authorization'] = `Basic ${Buffer.from(`${this.options.user}:${this.options.password}`).toString('base64')}`;
|
|
683
|
+
}
|
|
684
|
+
const urlParsed = new URL(input);
|
|
685
|
+
const request = http.request({
|
|
686
|
+
host: this.options.host,
|
|
687
|
+
port: this.options.port,
|
|
688
|
+
method: 'CONNECT',
|
|
689
|
+
path: `${urlParsed.hostname}:443`,
|
|
690
|
+
headers
|
|
691
|
+
});
|
|
692
|
+
request.on('connect', (res, socket)=>{
|
|
693
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
694
|
+
resolve(new https.Agent({
|
|
695
|
+
socket,
|
|
696
|
+
...this.agentOptions
|
|
697
|
+
}));
|
|
698
|
+
} else {
|
|
699
|
+
reject(new Error('Could not connect to proxy!'));
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
request.on('error', (err)=>{
|
|
703
|
+
reject(err);
|
|
704
|
+
});
|
|
705
|
+
request.on('timeout', (err)=>{
|
|
706
|
+
reject(err);
|
|
707
|
+
});
|
|
708
|
+
request.end();
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
constructor(options, agentOptions){
|
|
712
|
+
this.options = options;
|
|
713
|
+
this.agentOptions = merge(agentOptions || {}, {
|
|
714
|
+
keepAlive: false
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
async function buildHTTPClientConfigForProxy(url) {
|
|
720
|
+
const connectionString = getProxyForUrl(url);
|
|
721
|
+
if (connectionString) {
|
|
722
|
+
const connectionDetails = parseProxyConnectionString(connectionString);
|
|
723
|
+
const proxyClient = new ProxyClient({
|
|
724
|
+
host: connectionDetails.host,
|
|
725
|
+
port: connectionDetails.port || 3128,
|
|
726
|
+
...connectionDetails.auth.username ? {
|
|
727
|
+
user: connectionDetails.auth.username
|
|
728
|
+
} : {},
|
|
729
|
+
...connectionDetails.auth.password ? {
|
|
730
|
+
password: connectionDetails.auth.password
|
|
731
|
+
} : {}
|
|
732
|
+
});
|
|
733
|
+
const agent = await proxyClient.createAgent(url);
|
|
734
|
+
return {
|
|
735
|
+
agent
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
return {};
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/*
|
|
742
|
+
* Copyright (c) 2022.
|
|
743
|
+
* Author Peter Placzek (tada5hi)
|
|
744
|
+
* For the full copyright and license information,
|
|
745
|
+
* view the LICENSE file that was distributed with this source code.
|
|
746
|
+
*/ let instance$1;
|
|
747
|
+
function hasSmtpConfig() {
|
|
748
|
+
return !!instance$1;
|
|
749
|
+
}
|
|
750
|
+
function setSmtpConfig(value) {
|
|
751
|
+
instance$1 = value;
|
|
752
|
+
}
|
|
753
|
+
function useSmtpConfig() {
|
|
754
|
+
if (typeof instance$1 !== 'undefined') {
|
|
755
|
+
return instance$1;
|
|
756
|
+
}
|
|
757
|
+
instance$1 = {};
|
|
758
|
+
return instance$1;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
function createSmtpClient(options) {
|
|
762
|
+
let transport;
|
|
763
|
+
options = options || {};
|
|
764
|
+
if (typeof options === 'string') {
|
|
765
|
+
transport = createTransport(options);
|
|
766
|
+
} else if (options.connectionString) {
|
|
767
|
+
transport = createTransport(options.connectionString);
|
|
768
|
+
} else {
|
|
769
|
+
let auth;
|
|
770
|
+
if (options.user && options.password) {
|
|
771
|
+
auth = {
|
|
772
|
+
type: 'login',
|
|
773
|
+
user: options.user,
|
|
774
|
+
pass: options.password
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
transport = createTransport({
|
|
778
|
+
host: options.host,
|
|
779
|
+
port: options.port,
|
|
780
|
+
auth,
|
|
781
|
+
secure: options.ssl,
|
|
782
|
+
opportunisticTLS: options.starttls,
|
|
783
|
+
tls: {
|
|
784
|
+
rejectUnauthorized: false
|
|
785
|
+
}
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
transport.on('error', (e)=>{
|
|
789
|
+
useLogger().error(e.message);
|
|
790
|
+
});
|
|
791
|
+
return transport;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
let instance;
|
|
795
|
+
async function useSMTPClient() {
|
|
796
|
+
if (typeof instance !== 'undefined') {
|
|
797
|
+
return instance;
|
|
798
|
+
}
|
|
799
|
+
let options;
|
|
800
|
+
if (process.env.NODE_ENV === 'test') {
|
|
801
|
+
const testAccount = await createTestAccount();
|
|
802
|
+
options = {
|
|
803
|
+
host: 'smtp.ethereal.email',
|
|
804
|
+
port: 587,
|
|
805
|
+
ssl: false,
|
|
806
|
+
user: testAccount.user,
|
|
807
|
+
password: testAccount.pass
|
|
808
|
+
};
|
|
809
|
+
} else {
|
|
810
|
+
options = useSmtpConfig();
|
|
811
|
+
}
|
|
812
|
+
instance = createSmtpClient(options);
|
|
813
|
+
return instance;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
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 };
|
|
817
|
+
//# sourceMappingURL=index.mjs.map
|