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