@authup/server-kit 1.0.0-beta.2 → 1.0.0-beta.20
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 +201 -21
- package/README.md +2 -2
- package/dist/crypto/hash/hash.d.ts +1 -1
- package/dist/crypto/hash/hash.d.ts.map +1 -1
- package/dist/crypto/json-web-token/extract.d.ts +11 -0
- package/dist/crypto/json-web-token/extract.d.ts.map +1 -0
- package/dist/crypto/json-web-token/index.d.ts +1 -1
- package/dist/crypto/json-web-token/index.d.ts.map +1 -1
- package/dist/crypto/json-web-token/sign/index.d.ts +3 -0
- package/dist/crypto/json-web-token/sign/index.d.ts.map +1 -0
- package/dist/crypto/json-web-token/sign/module.d.ts +4 -0
- package/dist/crypto/json-web-token/sign/module.d.ts.map +1 -0
- package/dist/crypto/json-web-token/sign/types.d.ts +23 -0
- package/dist/crypto/json-web-token/sign/types.d.ts.map +1 -0
- package/dist/crypto/json-web-token/type.d.ts +4 -33
- package/dist/crypto/json-web-token/type.d.ts.map +1 -1
- package/dist/crypto/json-web-token/utils.d.ts +4 -1
- package/dist/crypto/json-web-token/utils.d.ts.map +1 -1
- package/dist/crypto/json-web-token/verify/index.d.ts +3 -0
- package/dist/crypto/json-web-token/verify/index.d.ts.map +1 -0
- package/dist/crypto/json-web-token/verify/module.d.ts +12 -0
- package/dist/crypto/json-web-token/verify/module.d.ts.map +1 -0
- package/dist/crypto/json-web-token/verify/types.d.ts +20 -0
- package/dist/crypto/json-web-token/verify/types.d.ts.map +1 -0
- package/dist/crypto/key-pair/helpers/check.d.ts.map +1 -0
- package/dist/crypto/key-pair/helpers/file-name.d.ts.map +1 -0
- package/dist/crypto/key-pair/{utils → helpers}/index.d.ts +1 -0
- package/dist/crypto/key-pair/helpers/index.d.ts.map +1 -0
- package/dist/crypto/key-pair/helpers/options.d.ts.map +1 -0
- package/dist/crypto/key-pair/{utils → helpers}/private-key.d.ts +0 -1
- package/dist/crypto/key-pair/helpers/private-key.d.ts.map +1 -0
- package/dist/crypto/key-pair/helpers/wrap.d.ts +5 -0
- package/dist/crypto/key-pair/helpers/wrap.d.ts.map +1 -0
- package/dist/crypto/key-pair/index.d.ts +1 -1
- package/dist/crypto/key-pair/index.d.ts.map +1 -1
- package/dist/crypto/key-pair/type.d.ts +0 -1
- package/dist/crypto/key-pair/type.d.ts.map +1 -1
- package/dist/domain-event/index.d.ts +1 -1
- package/dist/domain-event/index.d.ts.map +1 -1
- package/dist/domain-event/module.d.ts +8 -0
- package/dist/domain-event/module.d.ts.map +1 -0
- package/dist/domain-event/redis/index.d.ts +1 -1
- package/dist/domain-event/redis/index.d.ts.map +1 -1
- package/dist/domain-event/redis/module.d.ts +8 -0
- package/dist/domain-event/redis/module.d.ts.map +1 -0
- package/dist/domain-event/socket/index.d.ts +1 -2
- package/dist/domain-event/socket/index.d.ts.map +1 -1
- package/dist/domain-event/socket/module.d.ts +9 -0
- package/dist/domain-event/socket/module.d.ts.map +1 -0
- package/dist/domain-event/type.d.ts +8 -0
- package/dist/domain-event/type.d.ts.map +1 -1
- package/dist/index.cjs +454 -268
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +368 -252
- package/dist/index.mjs.map +1 -1
- package/dist/services/index.d.ts +4 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/logger/index.d.ts +4 -0
- package/dist/services/logger/index.d.ts.map +1 -0
- package/dist/services/logger/module.d.ts +3 -0
- package/dist/services/logger/module.d.ts.map +1 -0
- package/dist/services/logger/singleton.d.ts +7 -0
- package/dist/services/logger/singleton.d.ts.map +1 -0
- package/dist/services/logger/types.d.ts +7 -0
- package/dist/services/logger/types.d.ts.map +1 -0
- package/dist/services/redis/index.d.ts +3 -0
- package/dist/services/redis/index.d.ts.map +1 -0
- package/dist/services/redis/module.d.ts +3 -0
- package/dist/services/redis/module.d.ts.map +1 -0
- package/dist/services/redis/singleton.d.ts +7 -0
- package/dist/services/redis/singleton.d.ts.map +1 -0
- package/dist/services/vault/index.d.ts +3 -0
- package/dist/services/vault/index.d.ts.map +1 -0
- package/dist/services/vault/module.d.ts +3 -0
- package/dist/services/vault/module.d.ts.map +1 -0
- package/dist/services/vault/singleton.d.ts +6 -0
- package/dist/services/vault/singleton.d.ts.map +1 -0
- package/package.json +17 -20
- package/dist/crypto/json-web-token/decode.d.ts +0 -15
- package/dist/crypto/json-web-token/decode.d.ts.map +0 -1
- package/dist/crypto/json-web-token/sign.d.ts +0 -4
- package/dist/crypto/json-web-token/sign.d.ts.map +0 -1
- package/dist/crypto/json-web-token/verify.d.ts +0 -15
- package/dist/crypto/json-web-token/verify.d.ts.map +0 -1
- package/dist/crypto/key-pair/utils/check.d.ts.map +0 -1
- package/dist/crypto/key-pair/utils/file-name.d.ts.map +0 -1
- package/dist/crypto/key-pair/utils/index.d.ts.map +0 -1
- package/dist/crypto/key-pair/utils/options.d.ts.map +0 -1
- package/dist/crypto/key-pair/utils/private-key.d.ts.map +0 -1
- package/dist/domain-event/publish.d.ts +0 -4
- package/dist/domain-event/publish.d.ts.map +0 -1
- package/dist/domain-event/redis/publish.d.ts +0 -4
- package/dist/domain-event/redis/publish.d.ts.map +0 -1
- package/dist/domain-event/socket/publish.d.ts +0 -4
- package/dist/domain-event/socket/publish.d.ts.map +0 -1
- package/dist/domain-event/socket/singleton.d.ts +0 -4
- package/dist/domain-event/socket/singleton.d.ts.map +0 -1
- package/dist/domain-event/socket/type.d.ts +0 -5
- package/dist/domain-event/socket/type.d.ts.map +0 -1
- package/dist/logger/index.d.ts +0 -4
- package/dist/logger/index.d.ts.map +0 -1
- package/dist/logger/module.d.ts +0 -4
- package/dist/logger/module.d.ts.map +0 -1
- package/dist/logger/presets/index.d.ts +0 -2
- package/dist/logger/presets/index.d.ts.map +0 -1
- package/dist/logger/presets/void.d.ts +0 -16
- package/dist/logger/presets/void.d.ts.map +0 -1
- package/dist/logger/type.d.ts +0 -14
- package/dist/logger/type.d.ts.map +0 -1
- package/dist/smtp/config/index.d.ts +0 -2
- package/dist/smtp/config/index.d.ts.map +0 -1
- package/dist/smtp/config/singleton.d.ts +0 -5
- package/dist/smtp/config/singleton.d.ts.map +0 -1
- package/dist/smtp/index.d.ts +0 -5
- package/dist/smtp/index.d.ts.map +0 -1
- package/dist/smtp/module.d.ts +0 -4
- package/dist/smtp/module.d.ts.map +0 -1
- package/dist/smtp/singleton.d.ts +0 -3
- package/dist/smtp/singleton.d.ts.map +0 -1
- package/dist/smtp/type.d.ts +0 -14
- package/dist/smtp/type.d.ts.map +0 -1
- /package/dist/crypto/key-pair/{utils → helpers}/check.d.ts +0 -0
- /package/dist/crypto/key-pair/{utils → helpers}/file-name.d.ts +0 -0
- /package/dist/crypto/key-pair/{utils → helpers}/options.d.ts +0 -0
package/dist/index.mjs
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import { compare as compare$1, hash as hash$1 } from 'bcrypt';
|
|
1
|
+
import { compare as compare$1, hash as hash$1 } from '@node-rs/bcrypt';
|
|
2
2
|
import { createPrivateKey, generateKeyPair, createPublicKey } from 'node:crypto';
|
|
3
|
-
import { isObject, TokenError,
|
|
3
|
+
import { isObject, TokenError, JWTAlgorithm, JWKType, buildEventFullName } from '@authup/kit';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import fs from 'node:fs';
|
|
6
|
-
import {
|
|
6
|
+
import { Algorithm, sign, verify } from '@node-rs/jsonwebtoken';
|
|
7
7
|
import { isObject as isObject$1 } from 'smob';
|
|
8
|
-
import
|
|
8
|
+
import * as process$1 from 'node:process';
|
|
9
|
+
import { transports, createLogger as createLogger$1, format } from 'winston';
|
|
10
|
+
export { Logger } from 'winston';
|
|
11
|
+
import { singa } from 'singa';
|
|
12
|
+
export { Client as RedisClient, ClientOptions as RedisClientOptions, JsonAdapter as RedisJsonAdapter, Watcher as RedisWatcher, buildKeyPath as buildRedisKeyPath, createClient as createRedisClient, escapeKey as escapeRedisKey, parseKeyPath as parseRedisKeyPath } from 'redis-extension';
|
|
13
|
+
export { VaultClient, createClient as createVaultClient } from '@hapic/vault';
|
|
9
14
|
import { Emitter } from '@socket.io/redis-emitter';
|
|
10
|
-
import { createTransport, createTestAccount } from 'nodemailer';
|
|
11
15
|
|
|
12
16
|
async function compare(value, hashedValue) {
|
|
13
17
|
return compare$1(value, hashedValue);
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
async function hash(str,
|
|
17
|
-
return hash$1(str,
|
|
20
|
+
async function hash(str, rounds = 10) {
|
|
21
|
+
return hash$1(str, rounds);
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
/*
|
|
@@ -121,6 +125,41 @@ function decryptRSAPrivateKey(context, key) {
|
|
|
121
125
|
return content;
|
|
122
126
|
}
|
|
123
127
|
|
|
128
|
+
/*
|
|
129
|
+
* Copyright (c) 2024.
|
|
130
|
+
* Author Peter Placzek (tada5hi)
|
|
131
|
+
* For the full copyright and license information,
|
|
132
|
+
* view the LICENSE file that was distributed with this source code.
|
|
133
|
+
*/ function wrapPem(type, input) {
|
|
134
|
+
if (typeof input !== 'string') {
|
|
135
|
+
input = Buffer.from(input).toString('base64');
|
|
136
|
+
}
|
|
137
|
+
return `-----BEGIN ${type}-----\n${input}\n-----END ${type}-----`;
|
|
138
|
+
}
|
|
139
|
+
function wrapPrivateKeyPem(input) {
|
|
140
|
+
return wrapPem('PRIVATE KEY', input);
|
|
141
|
+
}
|
|
142
|
+
function wrapPublicKeyPem(input) {
|
|
143
|
+
return wrapPem('PUBLIC KEY', input);
|
|
144
|
+
}
|
|
145
|
+
// ------------------------------------------------------------
|
|
146
|
+
function unwrapPem(type, input) {
|
|
147
|
+
if (typeof input !== 'string') {
|
|
148
|
+
input = Buffer.from(input).toString('base64');
|
|
149
|
+
}
|
|
150
|
+
input = input.replace(`-----BEGIN ${type}-----\n`, '');
|
|
151
|
+
input = input.replace(`\n-----END ${type}-----\n`, '');
|
|
152
|
+
input = input.replace(`-----END ${type}-----\n`, '');
|
|
153
|
+
input = input.replace(`\n-----END ${type}-----`, '');
|
|
154
|
+
return input;
|
|
155
|
+
}
|
|
156
|
+
function unwrapPrivateKeyPem(input) {
|
|
157
|
+
return unwrapPem('PRIVATE KEY', input);
|
|
158
|
+
}
|
|
159
|
+
function unwrapPublicKeyPem(input) {
|
|
160
|
+
return unwrapPem('PUBLIC KEY', input);
|
|
161
|
+
}
|
|
162
|
+
|
|
124
163
|
async function saveKeyPair(keyPair, context) {
|
|
125
164
|
context = extendKeyPairOptions(context);
|
|
126
165
|
await fs.promises.mkdir(context.directory, {
|
|
@@ -259,26 +298,89 @@ async function useKeyPair(value) {
|
|
|
259
298
|
return keyPair;
|
|
260
299
|
}
|
|
261
300
|
|
|
301
|
+
/**
|
|
302
|
+
* Decode a JWT token with no verification.
|
|
303
|
+
*
|
|
304
|
+
* @param token
|
|
305
|
+
*
|
|
306
|
+
* @throws TokenError
|
|
307
|
+
*/ function extractTokenHeader(token) {
|
|
308
|
+
const parts = token.split('.');
|
|
309
|
+
if (parts.length !== 3) {
|
|
310
|
+
throw TokenError.payloadInvalid('The token format is not valid.');
|
|
311
|
+
}
|
|
312
|
+
const [headerBase64] = parts;
|
|
313
|
+
try {
|
|
314
|
+
const payload = atob(headerBase64);
|
|
315
|
+
return JSON.parse(payload);
|
|
316
|
+
/*
|
|
317
|
+
return {
|
|
318
|
+
typ: 'JWT',
|
|
319
|
+
alg: transformInternalToJWTAlgorithm(header.algorithm),
|
|
320
|
+
cty: header.contentType,
|
|
321
|
+
jku: header.jsonKeyUrl,
|
|
322
|
+
kid: header.keyId,
|
|
323
|
+
x5u: header.x5Url,
|
|
324
|
+
x5c: header.x5CertChain,
|
|
325
|
+
x5t: header.x5CertThumbprint,
|
|
326
|
+
'x5t#S256': header.x5TS256CertThumbprint,
|
|
327
|
+
};
|
|
328
|
+
*/ } catch (e) {
|
|
329
|
+
throw TokenError.headerInvalid('The token header could not be extracted.');
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function extractTokenPayload(token) {
|
|
333
|
+
const parts = token.split('.');
|
|
334
|
+
if (parts.length !== 3) {
|
|
335
|
+
throw TokenError.payloadInvalid('The token format is not valid.');
|
|
336
|
+
}
|
|
337
|
+
const [, payloadBase64] = parts;
|
|
338
|
+
try {
|
|
339
|
+
const payload = atob(payloadBase64);
|
|
340
|
+
return JSON.parse(payload);
|
|
341
|
+
} catch (e) {
|
|
342
|
+
throw TokenError.payloadInvalid('The token payload could not be extracted.');
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
262
346
|
function createErrorForJWTError(e) {
|
|
263
|
-
if (isObject$1(e)
|
|
264
|
-
|
|
265
|
-
|
|
347
|
+
if (isObject$1(e)) {
|
|
348
|
+
if (typeof e.name === 'string') {
|
|
349
|
+
switch(e.name){
|
|
350
|
+
case 'TokenExpiredError':
|
|
351
|
+
{
|
|
352
|
+
return TokenError.expired();
|
|
353
|
+
}
|
|
354
|
+
case 'NotBeforeError':
|
|
355
|
+
{
|
|
356
|
+
if (typeof e.date === 'string' || e.date instanceof Date) {
|
|
357
|
+
return TokenError.notActiveBefore(e.date);
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case 'JsonWebTokenError':
|
|
362
|
+
{
|
|
363
|
+
if (typeof e.message === 'string') {
|
|
364
|
+
return TokenError.payloadInvalid(e.message);
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// @see https://github.com/Keats/jsonwebtoken/blob/master/src/errors.rs
|
|
371
|
+
switch(e.message){
|
|
372
|
+
case 'ExpiredSignature':
|
|
266
373
|
{
|
|
267
374
|
return TokenError.expired();
|
|
268
375
|
}
|
|
269
|
-
case '
|
|
376
|
+
case 'ImmatureSignature':
|
|
270
377
|
{
|
|
271
|
-
|
|
272
|
-
return TokenError.notActiveBefore(e.date);
|
|
273
|
-
}
|
|
274
|
-
break;
|
|
378
|
+
return TokenError.notActiveBefore();
|
|
275
379
|
}
|
|
276
|
-
case '
|
|
380
|
+
case 'InvalidToken':
|
|
381
|
+
case 'InvalidSignature':
|
|
277
382
|
{
|
|
278
|
-
|
|
279
|
-
return TokenError.payloadInvalid(e.message);
|
|
280
|
-
}
|
|
281
|
-
break;
|
|
383
|
+
return TokenError.payloadInvalid();
|
|
282
384
|
}
|
|
283
385
|
}
|
|
284
386
|
}
|
|
@@ -288,101 +390,140 @@ function createErrorForJWTError(e) {
|
|
|
288
390
|
message: 'The JWT error could not be determined.'
|
|
289
391
|
});
|
|
290
392
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
393
|
+
function transformJWTAlgorithmToInternal(algorithm) {
|
|
394
|
+
switch(algorithm){
|
|
395
|
+
case JWTAlgorithm.HS256:
|
|
396
|
+
{
|
|
397
|
+
return Algorithm.HS256;
|
|
398
|
+
}
|
|
399
|
+
case JWTAlgorithm.HS384:
|
|
400
|
+
{
|
|
401
|
+
return Algorithm.HS384;
|
|
402
|
+
}
|
|
403
|
+
case JWTAlgorithm.HS512:
|
|
404
|
+
{
|
|
405
|
+
return Algorithm.HS512;
|
|
406
|
+
}
|
|
407
|
+
case JWTAlgorithm.RS256:
|
|
408
|
+
{
|
|
409
|
+
return Algorithm.RS256;
|
|
410
|
+
}
|
|
411
|
+
case JWTAlgorithm.RS384:
|
|
412
|
+
{
|
|
413
|
+
return Algorithm.RS384;
|
|
414
|
+
}
|
|
415
|
+
case JWTAlgorithm.RS512:
|
|
416
|
+
{
|
|
417
|
+
return Algorithm.RS512;
|
|
418
|
+
}
|
|
419
|
+
case JWTAlgorithm.ES256:
|
|
420
|
+
{
|
|
421
|
+
return Algorithm.ES256;
|
|
422
|
+
}
|
|
423
|
+
case JWTAlgorithm.ES384:
|
|
424
|
+
{
|
|
425
|
+
return Algorithm.ES384;
|
|
426
|
+
}
|
|
427
|
+
case JWTAlgorithm.PS256:
|
|
428
|
+
{
|
|
429
|
+
return Algorithm.PS256;
|
|
430
|
+
}
|
|
431
|
+
case JWTAlgorithm.PS384:
|
|
432
|
+
{
|
|
433
|
+
return Algorithm.PS384;
|
|
434
|
+
}
|
|
435
|
+
case JWTAlgorithm.PS512:
|
|
436
|
+
{
|
|
437
|
+
return Algorithm.PS512;
|
|
438
|
+
}
|
|
304
439
|
}
|
|
305
|
-
|
|
440
|
+
throw new Error(`The algorithm ${algorithm} is not supported.`);
|
|
306
441
|
}
|
|
307
442
|
|
|
308
|
-
|
|
309
|
-
|
|
443
|
+
const getUtcTimestamp = ()=>Math.floor(new Date().getTime() / 1000);
|
|
444
|
+
async function signToken(claims, context) {
|
|
445
|
+
if (typeof claims.exp !== 'number') {
|
|
446
|
+
claims.exp = getUtcTimestamp() + 3600;
|
|
447
|
+
}
|
|
448
|
+
if (typeof claims.iat !== 'number') {
|
|
449
|
+
claims.iat = getUtcTimestamp();
|
|
450
|
+
}
|
|
310
451
|
switch(context.type){
|
|
311
|
-
case
|
|
312
|
-
case
|
|
452
|
+
case JWKType.RSA:
|
|
453
|
+
case JWKType.EC:
|
|
313
454
|
{
|
|
314
|
-
const {
|
|
315
|
-
|
|
316
|
-
if (type ===
|
|
317
|
-
|
|
455
|
+
const { privateKey } = isKeyPair(context.keyPair) ? context.keyPair : await useKeyPair(context.keyPair);
|
|
456
|
+
let algorithm;
|
|
457
|
+
if (context.type === JWKType.RSA) {
|
|
458
|
+
algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : Algorithm.RS256;
|
|
318
459
|
} else {
|
|
319
|
-
|
|
460
|
+
algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : Algorithm.ES256;
|
|
320
461
|
}
|
|
321
|
-
return sign(
|
|
462
|
+
return sign(claims, privateKey, {
|
|
463
|
+
algorithm,
|
|
464
|
+
keyId: context.keyId
|
|
465
|
+
});
|
|
322
466
|
}
|
|
323
|
-
case
|
|
467
|
+
case JWKType.OCT:
|
|
324
468
|
{
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
469
|
+
const algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : Algorithm.HS256;
|
|
470
|
+
return sign(claims, context.key, {
|
|
471
|
+
algorithm,
|
|
472
|
+
keyId: context.keyId
|
|
473
|
+
});
|
|
328
474
|
}
|
|
329
475
|
}
|
|
330
476
|
throw new TokenError();
|
|
331
477
|
}
|
|
332
478
|
|
|
333
|
-
|
|
479
|
+
/**
|
|
480
|
+
* Verify JWT.
|
|
481
|
+
*
|
|
482
|
+
* @param token
|
|
483
|
+
* @param context
|
|
484
|
+
*
|
|
485
|
+
* @throws TokenError
|
|
486
|
+
*/ async function verifyToken(token, context) {
|
|
334
487
|
let promise;
|
|
335
488
|
let output;
|
|
336
489
|
try {
|
|
337
490
|
switch(context.type){
|
|
338
|
-
case
|
|
339
|
-
case
|
|
491
|
+
case JWKType.RSA:
|
|
492
|
+
case JWKType.EC:
|
|
340
493
|
{
|
|
341
|
-
const {
|
|
342
|
-
|
|
343
|
-
if (type ===
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
494
|
+
const { publicKey } = isKeyPairWithPublicKey(context.keyPair) ? context.keyPair : await useKeyPair(context.keyPair);
|
|
495
|
+
let algorithms;
|
|
496
|
+
if (context.type === JWKType.RSA) {
|
|
497
|
+
algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
498
|
+
Algorithm.RS256,
|
|
499
|
+
Algorithm.RS384,
|
|
500
|
+
Algorithm.RS512,
|
|
501
|
+
Algorithm.PS256,
|
|
502
|
+
Algorithm.PS384,
|
|
503
|
+
Algorithm.PS512
|
|
351
504
|
];
|
|
352
505
|
} else {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
'ES512'
|
|
506
|
+
algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
507
|
+
Algorithm.ES256,
|
|
508
|
+
Algorithm.ES384
|
|
357
509
|
];
|
|
358
510
|
}
|
|
359
|
-
promise =
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
reject(err);
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
resolve(decoded);
|
|
366
|
-
});
|
|
511
|
+
promise = verify(token, publicKey, {
|
|
512
|
+
algorithms,
|
|
513
|
+
validateNbf: true
|
|
367
514
|
});
|
|
368
515
|
break;
|
|
369
516
|
}
|
|
370
|
-
case
|
|
517
|
+
case JWKType.OCT:
|
|
371
518
|
{
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
'HS512'
|
|
519
|
+
const algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
520
|
+
Algorithm.HS256,
|
|
521
|
+
Algorithm.HS384,
|
|
522
|
+
Algorithm.HS512
|
|
377
523
|
];
|
|
378
|
-
promise =
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
reject(err);
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
resolve(decoded);
|
|
385
|
-
});
|
|
524
|
+
promise = verify(token, context.key, {
|
|
525
|
+
algorithms,
|
|
526
|
+
validateNbf: true
|
|
386
527
|
});
|
|
387
528
|
}
|
|
388
529
|
}
|
|
@@ -398,6 +539,87 @@ async function verifyToken(token, context) {
|
|
|
398
539
|
return output;
|
|
399
540
|
}
|
|
400
541
|
|
|
542
|
+
function createLogger(context) {
|
|
543
|
+
let items;
|
|
544
|
+
const cwd = context.directory || process$1.cwd();
|
|
545
|
+
if (context.env === 'production') {
|
|
546
|
+
items = [
|
|
547
|
+
new transports.Console({
|
|
548
|
+
level: 'info'
|
|
549
|
+
}),
|
|
550
|
+
new transports.File({
|
|
551
|
+
filename: path.join(cwd, 'access.log'),
|
|
552
|
+
level: 'http',
|
|
553
|
+
maxsize: 10 * 1024 * 1024,
|
|
554
|
+
maxFiles: 5
|
|
555
|
+
}),
|
|
556
|
+
new transports.File({
|
|
557
|
+
filename: path.join(cwd, 'error.log'),
|
|
558
|
+
level: 'warn',
|
|
559
|
+
maxsize: 10 * 1024 * 1024,
|
|
560
|
+
maxFiles: 5
|
|
561
|
+
})
|
|
562
|
+
];
|
|
563
|
+
} else {
|
|
564
|
+
items = [
|
|
565
|
+
new transports.Console({
|
|
566
|
+
level: 'debug'
|
|
567
|
+
})
|
|
568
|
+
];
|
|
569
|
+
}
|
|
570
|
+
return createLogger$1({
|
|
571
|
+
format: format.combine(format.errors({
|
|
572
|
+
stack: true
|
|
573
|
+
}), format.timestamp(), format.colorize(), format.simple()),
|
|
574
|
+
transports: items
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const instance$2 = singa({
|
|
579
|
+
name: 'logger'
|
|
580
|
+
});
|
|
581
|
+
function setLoggerFactory(factory) {
|
|
582
|
+
instance$2.setFactory(factory);
|
|
583
|
+
}
|
|
584
|
+
function isLoggerUsable() {
|
|
585
|
+
return instance$2.has() || instance$2.hasFactory();
|
|
586
|
+
}
|
|
587
|
+
function setLogger(input) {
|
|
588
|
+
instance$2.set(input);
|
|
589
|
+
}
|
|
590
|
+
function useLogger() {
|
|
591
|
+
return instance$2.use();
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const instance$1 = singa({
|
|
595
|
+
name: 'redis'
|
|
596
|
+
});
|
|
597
|
+
function setRedisFactory(factory) {
|
|
598
|
+
instance$1.setFactory(factory);
|
|
599
|
+
}
|
|
600
|
+
function isRedisClientUsable() {
|
|
601
|
+
return instance$1.has() || instance$1.hasFactory();
|
|
602
|
+
}
|
|
603
|
+
function setRedisClient(input) {
|
|
604
|
+
instance$1.set(input);
|
|
605
|
+
}
|
|
606
|
+
function useRedisClient() {
|
|
607
|
+
return instance$1.use();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
const instance = singa({
|
|
611
|
+
name: 'vault'
|
|
612
|
+
});
|
|
613
|
+
function setVaultFactory(factory) {
|
|
614
|
+
instance.setFactory(factory);
|
|
615
|
+
}
|
|
616
|
+
function isVaultClientUsable() {
|
|
617
|
+
return instance.has() || instance.hasFactory();
|
|
618
|
+
}
|
|
619
|
+
function useVaultClient() {
|
|
620
|
+
return instance.use();
|
|
621
|
+
}
|
|
622
|
+
|
|
401
623
|
function transformDomainEventData(input) {
|
|
402
624
|
if (isObject(input)) {
|
|
403
625
|
const keys = Object.keys(input);
|
|
@@ -417,187 +639,81 @@ function buildDomainEventChannelName(input, id) {
|
|
|
417
639
|
return input(id);
|
|
418
640
|
}
|
|
419
641
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if (context.event !== DomainEventName.CREATED && typeof destinations[i].channel === 'function') {
|
|
434
|
-
key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
|
|
435
|
-
pipeline.publish(key, json);
|
|
642
|
+
class DomainEventRedisPublisher {
|
|
643
|
+
async publish(ctx) {
|
|
644
|
+
const data = JSON.stringify(transformDomainEventData(ctx.content));
|
|
645
|
+
const pipeline = this.driver.pipeline();
|
|
646
|
+
for(let i = 0; i < ctx.destinations.length; i++){
|
|
647
|
+
const { namespace } = ctx.destinations[i];
|
|
648
|
+
const keyPrefix = namespace ? `${namespace}:` : '';
|
|
649
|
+
let key = keyPrefix + buildDomainEventChannelName(ctx.destinations[i].channel);
|
|
650
|
+
pipeline.publish(key, data);
|
|
651
|
+
if (typeof ctx.destinations[i].channel === 'function') {
|
|
652
|
+
key = keyPrefix + buildDomainEventChannelName(ctx.destinations[i].channel, ctx.content.data.id);
|
|
653
|
+
pipeline.publish(key, data);
|
|
654
|
+
}
|
|
436
655
|
}
|
|
656
|
+
await pipeline.exec();
|
|
437
657
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
let instance$3;
|
|
442
|
-
function useSocketEmitter() {
|
|
443
|
-
if (typeof instance$3 !== 'undefined') {
|
|
444
|
-
return instance$3;
|
|
658
|
+
constructor(client){
|
|
659
|
+
this.driver = client;
|
|
445
660
|
}
|
|
446
|
-
instance$3 = new Emitter(useClient());
|
|
447
|
-
return instance$3;
|
|
448
661
|
}
|
|
449
662
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
let roomName = buildDomainEventChannelName(destinations[i].channel);
|
|
461
|
-
const fullEventName = buildDomainEventFullName(context.type, context.event);
|
|
462
|
-
emitter.in(roomName)// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
463
|
-
// @ts-ignore
|
|
464
|
-
.emit(fullEventName, {
|
|
465
|
-
...context,
|
|
466
|
-
meta: {
|
|
467
|
-
roomName
|
|
663
|
+
class DomainEventSocketPublisher {
|
|
664
|
+
async publish(ctx) {
|
|
665
|
+
ctx.content = transformDomainEventData(ctx.content);
|
|
666
|
+
for(let i = 0; i < ctx.destinations.length; i++){
|
|
667
|
+
let emitter;
|
|
668
|
+
if (ctx.destinations[i].namespace) {
|
|
669
|
+
emitter = this.driver.of(ctx.destinations[i].namespace);
|
|
670
|
+
} else {
|
|
671
|
+
emitter = this.driver;
|
|
468
672
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
roomName
|
|
472
|
-
|
|
473
|
-
// @ts-ignore
|
|
474
|
-
.emit(fullEventName, {
|
|
475
|
-
...context,
|
|
673
|
+
let roomName = buildDomainEventChannelName(ctx.destinations[i].channel);
|
|
674
|
+
const fullEventName = buildEventFullName(ctx.content.type, ctx.content.event);
|
|
675
|
+
emitter.in(roomName).emit(fullEventName, {
|
|
676
|
+
...ctx.content,
|
|
476
677
|
meta: {
|
|
477
|
-
roomName
|
|
478
|
-
roomId: context.data.id
|
|
678
|
+
roomName
|
|
479
679
|
}
|
|
480
680
|
});
|
|
681
|
+
if (typeof ctx.destinations[i].channel === 'function') {
|
|
682
|
+
roomName = buildDomainEventChannelName(ctx.destinations[i].channel, ctx.content.data.id);
|
|
683
|
+
emitter.in(roomName).emit(fullEventName, {
|
|
684
|
+
...ctx.content,
|
|
685
|
+
meta: {
|
|
686
|
+
roomName,
|
|
687
|
+
roomId: ctx.content.data.id
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
}
|
|
481
691
|
}
|
|
482
692
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
async function publishDomainEvent(context, destinations) {
|
|
486
|
-
await publishDomainRedisEvent(context, destinations);
|
|
487
|
-
publishDomainSocketEvent(context, destinations);
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/*
|
|
491
|
-
* Copyright (c) 2022.
|
|
492
|
-
* Author Peter Placzek (tada5hi)
|
|
493
|
-
* For the full copyright and license information,
|
|
494
|
-
* view the LICENSE file that was distributed with this source code.
|
|
495
|
-
*/ class VoidLogger {
|
|
496
|
-
error() {
|
|
497
|
-
return this;
|
|
498
|
-
}
|
|
499
|
-
warn() {
|
|
500
|
-
return this;
|
|
501
|
-
}
|
|
502
|
-
info() {
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
http() {
|
|
506
|
-
return this;
|
|
507
|
-
}
|
|
508
|
-
verbose() {
|
|
509
|
-
return this;
|
|
510
|
-
}
|
|
511
|
-
debug() {
|
|
512
|
-
return this;
|
|
693
|
+
constructor(client){
|
|
694
|
+
this.driver = new Emitter(client);
|
|
513
695
|
}
|
|
514
696
|
}
|
|
515
697
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
function setLogger(logger) {
|
|
525
|
-
instance$2 = logger;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/*
|
|
529
|
-
* Copyright (c) 2022.
|
|
530
|
-
* Author Peter Placzek (tada5hi)
|
|
531
|
-
* For the full copyright and license information,
|
|
532
|
-
* view the LICENSE file that was distributed with this source code.
|
|
533
|
-
*/ let instance$1;
|
|
534
|
-
function hasSmtpConfig() {
|
|
535
|
-
return !!instance$1;
|
|
536
|
-
}
|
|
537
|
-
function setSmtpConfig(value) {
|
|
538
|
-
instance$1 = value;
|
|
539
|
-
}
|
|
540
|
-
function useSmtpConfig() {
|
|
541
|
-
if (typeof instance$1 !== 'undefined') {
|
|
542
|
-
return instance$1;
|
|
543
|
-
}
|
|
544
|
-
instance$1 = {};
|
|
545
|
-
return instance$1;
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
function createSmtpClient(options) {
|
|
549
|
-
let transport;
|
|
550
|
-
options = options || {};
|
|
551
|
-
if (typeof options === 'string') {
|
|
552
|
-
transport = createTransport(options);
|
|
553
|
-
} else if (options.connectionString) {
|
|
554
|
-
transport = createTransport(options.connectionString);
|
|
555
|
-
} else {
|
|
556
|
-
let auth;
|
|
557
|
-
if (options.user && options.password) {
|
|
558
|
-
auth = {
|
|
559
|
-
type: 'login',
|
|
560
|
-
user: options.user,
|
|
561
|
-
pass: options.password
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
|
-
transport = createTransport({
|
|
565
|
-
host: options.host,
|
|
566
|
-
port: options.port,
|
|
567
|
-
auth,
|
|
568
|
-
secure: options.ssl,
|
|
569
|
-
opportunisticTLS: options.starttls,
|
|
570
|
-
tls: {
|
|
571
|
-
rejectUnauthorized: false
|
|
698
|
+
class DomainEventPublisher {
|
|
699
|
+
async publish(ctx) {
|
|
700
|
+
const publishers = this.publishers.values();
|
|
701
|
+
while(true){
|
|
702
|
+
const it = publishers.next();
|
|
703
|
+
if (it.done) {
|
|
704
|
+
return;
|
|
572
705
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
transport.on('error', (e)=>{
|
|
576
|
-
useLogger().error(e.message);
|
|
577
|
-
});
|
|
578
|
-
return transport;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
let instance;
|
|
582
|
-
async function useSMTPClient() {
|
|
583
|
-
if (typeof instance !== 'undefined') {
|
|
584
|
-
return instance;
|
|
706
|
+
await it.value.publish(ctx);
|
|
707
|
+
}
|
|
585
708
|
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
user: testAccount.user,
|
|
594
|
-
password: testAccount.pass
|
|
595
|
-
};
|
|
596
|
-
} else {
|
|
597
|
-
options = useSmtpConfig();
|
|
709
|
+
constructor(){
|
|
710
|
+
this.publishers = new Set();
|
|
711
|
+
if (isRedisClientUsable()) {
|
|
712
|
+
const client = useRedisClient();
|
|
713
|
+
this.publishers.add(new DomainEventRedisPublisher(client));
|
|
714
|
+
this.publishers.add(new DomainEventSocketPublisher(client));
|
|
715
|
+
}
|
|
598
716
|
}
|
|
599
|
-
instance = createSmtpClient(options);
|
|
600
|
-
return instance;
|
|
601
717
|
}
|
|
602
718
|
|
|
603
719
|
/*
|
|
@@ -609,5 +725,5 @@ async function useSMTPClient() {
|
|
|
609
725
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
610
726
|
}
|
|
611
727
|
|
|
612
|
-
export {
|
|
728
|
+
export { DomainEventPublisher, DomainEventRedisPublisher, DomainEventSocketPublisher, KeyPairKind, buildKeyFileName, compare, createKeyPair, createLogger, decryptRSAPrivateKey, deleteKeyPair, extendKeyPairOptions, extractTokenHeader, extractTokenPayload, hasOwnProperty, hash, isKeyPair, isKeyPairWithPublicKey, isLoggerUsable, isRedisClientUsable, isVaultClientUsable, loadKeyPair, saveKeyPair, setLogger, setLoggerFactory, setRedisClient, setRedisFactory, setVaultFactory, signToken, unwrapPrivateKeyPem, unwrapPublicKeyPem, useKeyPair, useLogger, useRedisClient, useVaultClient, verifyToken, wrapPrivateKeyPem, wrapPublicKeyPem };
|
|
613
729
|
//# sourceMappingURL=index.mjs.map
|