@authup/server-kit 1.0.0-beta.26 → 1.0.0-beta.28
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/crypto/index.d.ts +0 -2
- package/dist/crypto/index.d.ts.map +1 -1
- package/dist/crypto/json-web-token/sign/module.d.ts.map +1 -1
- package/dist/crypto/json-web-token/verify/module.d.ts.map +1 -1
- package/dist/crypto/key/asymmetric/check.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/constants.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/create.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/helpers/index.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/helpers/wrap.d.ts.map +1 -0
- package/dist/crypto/{key-asymmetric → key/asymmetric}/index.d.ts +1 -0
- package/dist/crypto/key/asymmetric/index.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/key-usages.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/module.d.ts +20 -0
- package/dist/crypto/key/asymmetric/module.d.ts.map +1 -0
- package/dist/crypto/key/asymmetric/normalize.d.ts.map +1 -0
- package/dist/crypto/{key-asymmetric → key/asymmetric}/types.d.ts +5 -0
- package/dist/crypto/key/asymmetric/types.d.ts.map +1 -0
- package/dist/crypto/key/base.d.ts +9 -0
- package/dist/crypto/key/base.d.ts.map +1 -0
- package/dist/crypto/key/index.d.ts +3 -2
- package/dist/crypto/key/index.d.ts.map +1 -1
- package/dist/crypto/key/symmetric/check.d.ts.map +1 -0
- package/dist/crypto/key/symmetric/constants.d.ts.map +1 -0
- package/dist/crypto/key/symmetric/create.d.ts.map +1 -0
- package/dist/crypto/{key-symmetric → key/symmetric}/index.d.ts +1 -0
- package/dist/crypto/key/symmetric/index.d.ts.map +1 -0
- package/dist/crypto/key/symmetric/key-usages.d.ts +2 -0
- package/dist/crypto/key/symmetric/key-usages.d.ts.map +1 -0
- package/dist/crypto/key/symmetric/module.d.ts +13 -0
- package/dist/crypto/key/symmetric/module.d.ts.map +1 -0
- package/dist/crypto/key/symmetric/normalize.d.ts.map +1 -0
- package/dist/crypto/{key-symmetric → key/symmetric}/types.d.ts +5 -0
- package/dist/crypto/key/symmetric/types.d.ts.map +1 -0
- package/dist/domain-event/module.d.ts +1 -0
- package/dist/domain-event/module.d.ts.map +1 -1
- package/dist/domain-event/redis/module.d.ts +2 -1
- package/dist/domain-event/redis/module.d.ts.map +1 -1
- package/dist/domain-event/socket/module.d.ts +2 -2
- package/dist/domain-event/socket/module.d.ts.map +1 -1
- package/dist/domain-event/utils.d.ts +2 -1
- package/dist/domain-event/utils.d.ts.map +1 -1
- package/dist/index.mjs +303 -257
- package/dist/index.mjs.map +1 -1
- package/dist/services/cache/adapters/index.d.ts +0 -2
- package/dist/services/cache/adapters/index.d.ts.map +1 -1
- package/dist/services/cache/adapters/memory.d.ts +8 -9
- package/dist/services/cache/adapters/memory.d.ts.map +1 -1
- package/dist/services/cache/adapters/redis.d.ts +5 -5
- package/dist/services/cache/adapters/redis.d.ts.map +1 -1
- package/dist/services/cache/index.d.ts +0 -2
- package/dist/services/cache/index.d.ts.map +1 -1
- package/dist/services/cache/types.d.ts +8 -0
- package/dist/services/cache/types.d.ts.map +1 -1
- package/dist/services/redis/check.d.ts +3 -0
- package/dist/services/redis/check.d.ts.map +1 -0
- package/dist/services/redis/factory.d.ts +4 -0
- package/dist/services/redis/factory.d.ts.map +1 -0
- package/dist/services/redis/index.d.ts +2 -1
- package/dist/services/redis/index.d.ts.map +1 -1
- package/dist/services/redis/module.d.ts +4 -2
- package/dist/services/redis/module.d.ts.map +1 -1
- package/package.json +25 -21
- package/dist/crypto/key/container.d.ts +0 -14
- package/dist/crypto/key/container.d.ts.map +0 -1
- package/dist/crypto/key/key-usages.d.ts +0 -2
- package/dist/crypto/key/key-usages.d.ts.map +0 -1
- package/dist/crypto/key/types.d.ts +0 -14
- package/dist/crypto/key/types.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/check.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/constants.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/create.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/helpers/index.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/helpers/wrap.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/index.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/key-usages.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/normalize.d.ts.map +0 -1
- package/dist/crypto/key-asymmetric/types.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/check.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/constants.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/create.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/index.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/key-usages.d.ts +0 -2
- package/dist/crypto/key-symmetric/key-usages.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/normalize.d.ts.map +0 -1
- package/dist/crypto/key-symmetric/types.d.ts.map +0 -1
- package/dist/index.cjs +0 -1005
- package/dist/index.cjs.map +0 -1
- package/dist/services/cache/adapters/factory.d.ts +0 -4
- package/dist/services/cache/adapters/factory.d.ts.map +0 -1
- package/dist/services/cache/adapters/types.d.ts +0 -10
- package/dist/services/cache/adapters/types.d.ts.map +0 -1
- package/dist/services/cache/module.d.ts +0 -12
- package/dist/services/cache/module.d.ts.map +0 -1
- package/dist/services/cache/singleton.d.ts +0 -3
- package/dist/services/cache/singleton.d.ts.map +0 -1
- package/dist/services/redis/singleton.d.ts +0 -7
- package/dist/services/redis/singleton.d.ts.map +0 -1
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/check.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/constants.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/create.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/helpers/index.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/helpers/wrap.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/key-usages.d.ts +0 -0
- /package/dist/crypto/{key-asymmetric → key/asymmetric}/normalize.d.ts +0 -0
- /package/dist/crypto/{key-symmetric → key/symmetric}/check.d.ts +0 -0
- /package/dist/crypto/{key-symmetric → key/symmetric}/constants.d.ts +0 -0
- /package/dist/crypto/{key-symmetric → key/symmetric}/create.d.ts +0 -0
- /package/dist/crypto/{key-symmetric → key/symmetric}/normalize.d.ts +0 -0
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { compare as compare$1, hash as hash$1 } from '@node-rs/bcrypt';
|
|
2
|
-
import { subtle } from 'uncrypto';
|
|
3
2
|
import { arrayBufferToBase64, base64ToArrayBuffer, isObject as isObject$1 } from '@authup/kit';
|
|
4
|
-
import {
|
|
3
|
+
import { JWTAlgorithm, JWTError, JWKType } from '@authup/specs';
|
|
4
|
+
import { subtle } from 'uncrypto';
|
|
5
5
|
import { Algorithm, sign, verify } from '@node-rs/jsonwebtoken';
|
|
6
6
|
import { isObject } from 'smob';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import TTLCache from '@isaacs/ttlcache';
|
|
7
|
+
import { TTLCache } from '@isaacs/ttlcache';
|
|
8
|
+
import { createClient, JsonAdapter, buildKeyPath } from 'redis-extension';
|
|
9
|
+
export { Client as RedisClient, JsonAdapter as RedisJsonAdapter, Watcher as RedisWatcher, buildKeyPath as buildRedisKeyPath, escapeKey as escapeRedisKey, parseKeyPath as parseRedisKeyPath } from 'redis-extension';
|
|
11
10
|
import path from 'node:path';
|
|
12
11
|
import process from 'node:process';
|
|
13
12
|
import { transports, createLogger as createLogger$1, format } from 'winston';
|
|
14
13
|
export { Logger } from 'winston';
|
|
14
|
+
import { singa } from 'singa';
|
|
15
15
|
export { VaultClient, createClient as createVaultClient } from '@hapic/vault';
|
|
16
16
|
import { buildEventFullName } from '@authup/core-realtime-kit';
|
|
17
17
|
import { Emitter } from '@socket.io/redis-emitter';
|
|
@@ -38,8 +38,62 @@ async function hash(str, rounds = 10) {
|
|
|
38
38
|
return CryptoAsymmetricAlgorithm;
|
|
39
39
|
}({});
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
class BaseKey {
|
|
42
|
+
key;
|
|
43
|
+
// ----------------------------------------------
|
|
44
|
+
constructor(cryptoKey){
|
|
45
|
+
this.key = cryptoKey;
|
|
46
|
+
}
|
|
47
|
+
// ----------------------------------------------
|
|
48
|
+
async toArrayBuffer() {
|
|
49
|
+
if (this.key.type === 'private') {
|
|
50
|
+
return subtle.exportKey('pkcs8', this.key);
|
|
51
|
+
}
|
|
52
|
+
if (this.key.type === 'public') {
|
|
53
|
+
return subtle.exportKey('spki', this.key);
|
|
54
|
+
}
|
|
55
|
+
return subtle.exportKey('raw', this.key);
|
|
56
|
+
}
|
|
57
|
+
async toUint8Array() {
|
|
58
|
+
const arrayBuffer = await this.toArrayBuffer();
|
|
59
|
+
return new Uint8Array(arrayBuffer);
|
|
60
|
+
}
|
|
61
|
+
async toBase64() {
|
|
62
|
+
const arrayBuffer = await this.toArrayBuffer();
|
|
63
|
+
return arrayBufferToBase64(arrayBuffer);
|
|
64
|
+
}
|
|
65
|
+
async toJWK() {
|
|
66
|
+
return subtle.exportKey('jwk', this.key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/*
|
|
71
|
+
* Copyright (c) 2024-2024.
|
|
72
|
+
* Author Peter Placzek (tada5hi)
|
|
73
|
+
* For the full copyright and license information,
|
|
74
|
+
* view the LICENSE file that was distributed with this source code.
|
|
75
|
+
*/ function enc(type, input) {
|
|
76
|
+
return `-----BEGIN ${type}-----\n${input}\n-----END ${type}-----`;
|
|
77
|
+
}
|
|
78
|
+
function encodePKCS8ToPEM(base64) {
|
|
79
|
+
return enc('PRIVATE KEY', base64);
|
|
80
|
+
}
|
|
81
|
+
function encodeSPKIToPem(input) {
|
|
82
|
+
return enc('PUBLIC KEY', input);
|
|
83
|
+
}
|
|
84
|
+
// ------------------------------------------------------------
|
|
85
|
+
function dec(type, input) {
|
|
86
|
+
input = input.replace(`-----BEGIN ${type}-----\n`, '');
|
|
87
|
+
input = input.replace(`\n-----END ${type}-----\n`, '');
|
|
88
|
+
input = input.replace(`-----END ${type}-----\n`, '');
|
|
89
|
+
input = input.replace(`\n-----END ${type}-----`, '');
|
|
90
|
+
return input;
|
|
91
|
+
}
|
|
92
|
+
function decodePemToPKCS8(input) {
|
|
93
|
+
return dec('PRIVATE KEY', input);
|
|
94
|
+
}
|
|
95
|
+
function decodePemToSpki(input) {
|
|
96
|
+
return dec('PUBLIC KEY', input);
|
|
43
97
|
}
|
|
44
98
|
|
|
45
99
|
/**
|
|
@@ -144,38 +198,91 @@ function normalizeAsymmetricKeyImportOptions(options) {
|
|
|
144
198
|
return optionsNormalized;
|
|
145
199
|
}
|
|
146
200
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
201
|
+
class AsymmetricKey extends BaseKey {
|
|
202
|
+
async toPem() {
|
|
203
|
+
const base64 = await this.toBase64();
|
|
204
|
+
if (this.key.type === 'public') {
|
|
205
|
+
return encodeSPKIToPem(base64);
|
|
206
|
+
}
|
|
207
|
+
if (this.key.type === 'private') {
|
|
208
|
+
return encodePKCS8ToPEM(base64);
|
|
209
|
+
}
|
|
210
|
+
throw new Error(`${this.key.type} can not be converted to pem.`);
|
|
211
|
+
}
|
|
212
|
+
// ----------------------------------------------------------------
|
|
213
|
+
static async fromPem(ctx) {
|
|
214
|
+
if (ctx.format === 'pkcs8') {
|
|
215
|
+
return AsymmetricKey.fromBase64({
|
|
216
|
+
...ctx,
|
|
217
|
+
key: decodePemToPKCS8(ctx.key)
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return AsymmetricKey.fromBase64({
|
|
221
|
+
...ctx,
|
|
222
|
+
key: decodePemToSpki(ctx.key)
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
static async fromBase64(ctx) {
|
|
226
|
+
const arrayBuffer = base64ToArrayBuffer(ctx.key);
|
|
227
|
+
return AsymmetricKey.fromArrayBuffer({
|
|
228
|
+
...ctx,
|
|
229
|
+
key: arrayBuffer
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
static async fromArrayBuffer(ctx) {
|
|
233
|
+
const normalizedOptions = normalizeAsymmetricKeyImportOptions(ctx.options);
|
|
234
|
+
const cryptoKey = await subtle.importKey(ctx.format, ctx.key, normalizedOptions, true, getKeyUsagesForAsymmetricAlgorithm(normalizedOptions.name, ctx.format));
|
|
235
|
+
return new AsymmetricKey(cryptoKey);
|
|
236
|
+
}
|
|
237
|
+
// ----------------------------------------------------------------
|
|
238
|
+
static buildImportOptionsForJWTAlgorithm(alg) {
|
|
239
|
+
if (alg === JWTAlgorithm.RS256) {
|
|
240
|
+
return {
|
|
241
|
+
name: CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5,
|
|
242
|
+
hash: 'SHA-256'
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
if (alg === JWTAlgorithm.RS384) {
|
|
246
|
+
return {
|
|
247
|
+
name: CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5,
|
|
248
|
+
hash: 'SHA-384'
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
if (alg === JWTAlgorithm.RS512) {
|
|
252
|
+
return {
|
|
253
|
+
name: CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5,
|
|
254
|
+
hash: 'SHA-512'
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
if (alg === JWTAlgorithm.ES256) {
|
|
258
|
+
return {
|
|
259
|
+
name: CryptoAsymmetricAlgorithm.ECDSA,
|
|
260
|
+
namedCurve: 'P-256'
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (alg === JWTAlgorithm.ES384) {
|
|
264
|
+
return {
|
|
265
|
+
name: CryptoAsymmetricAlgorithm.ECDSA,
|
|
266
|
+
namedCurve: 'P-384'
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
if (alg === JWTAlgorithm.ES512) {
|
|
270
|
+
return {
|
|
271
|
+
name: CryptoAsymmetricAlgorithm.ECDSA,
|
|
272
|
+
namedCurve: 'P-521'
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
throw new Error(`Signature algorithm ${alg} is not supported.`);
|
|
276
|
+
}
|
|
150
277
|
}
|
|
151
278
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
* Author Peter Placzek (tada5hi)
|
|
155
|
-
* For the full copyright and license information,
|
|
156
|
-
* view the LICENSE file that was distributed with this source code.
|
|
157
|
-
*/ function enc(type, input) {
|
|
158
|
-
return `-----BEGIN ${type}-----\n${input}\n-----END ${type}-----`;
|
|
159
|
-
}
|
|
160
|
-
function encodePKCS8ToPEM(base64) {
|
|
161
|
-
return enc('PRIVATE KEY', base64);
|
|
162
|
-
}
|
|
163
|
-
function encodeSPKIToPem(input) {
|
|
164
|
-
return enc('PUBLIC KEY', input);
|
|
165
|
-
}
|
|
166
|
-
// ------------------------------------------------------------
|
|
167
|
-
function dec(type, input) {
|
|
168
|
-
input = input.replace(`-----BEGIN ${type}-----\n`, '');
|
|
169
|
-
input = input.replace(`\n-----END ${type}-----\n`, '');
|
|
170
|
-
input = input.replace(`-----END ${type}-----\n`, '');
|
|
171
|
-
input = input.replace(`\n-----END ${type}-----`, '');
|
|
172
|
-
return input;
|
|
173
|
-
}
|
|
174
|
-
function decodePemToPKCS8(input) {
|
|
175
|
-
return dec('PRIVATE KEY', input);
|
|
279
|
+
function isAsymmetricAlgorithm(input) {
|
|
280
|
+
return Object.values(CryptoAsymmetricAlgorithm).indexOf(input) !== -1;
|
|
176
281
|
}
|
|
177
|
-
|
|
178
|
-
|
|
282
|
+
|
|
283
|
+
async function createAsymmetricKeyPair(options) {
|
|
284
|
+
const optionsNormalized = normalizeAsymmetricKeyPairCreateOptions(options);
|
|
285
|
+
return subtle.generateKey(optionsNormalized, true, getKeyUsagesForAsymmetricAlgorithm(optionsNormalized.name));
|
|
179
286
|
}
|
|
180
287
|
|
|
181
288
|
/*
|
|
@@ -191,28 +298,6 @@ function decodePemToSpki(input) {
|
|
|
191
298
|
return SymmetricAlgorithm;
|
|
192
299
|
}({});
|
|
193
300
|
|
|
194
|
-
function normalizeSymmetricKeyCreateOptions(input) {
|
|
195
|
-
if (input.name === SymmetricAlgorithm.HMAC) {
|
|
196
|
-
return {
|
|
197
|
-
hash: 'SHA-256',
|
|
198
|
-
...input
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
return {
|
|
202
|
-
length: 256,
|
|
203
|
-
name: input.name
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
function normalizeSymmetricKeyImportOptions(input) {
|
|
207
|
-
if (input.name === SymmetricAlgorithm.HMAC) {
|
|
208
|
-
return {
|
|
209
|
-
hash: 'SHA-256',
|
|
210
|
-
...input
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
return input;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
301
|
function isSymmetricAlgorithm(input) {
|
|
217
302
|
return Object.values(SymmetricAlgorithm).indexOf(input) !== -1;
|
|
218
303
|
}
|
|
@@ -235,90 +320,69 @@ function getKeyUsagesForSymmetricAlgorithm(name) {
|
|
|
235
320
|
throw new SyntaxError(`Key usages can not be determined for symmetric algorithm: ${name}`);
|
|
236
321
|
}
|
|
237
322
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (isAsymmetricAlgorithm(name)) {
|
|
245
|
-
return getKeyUsagesForAsymmetricAlgorithm(name, format);
|
|
323
|
+
function normalizeSymmetricKeyCreateOptions(input) {
|
|
324
|
+
if (input.name === SymmetricAlgorithm.HMAC) {
|
|
325
|
+
return {
|
|
326
|
+
hash: 'SHA-256',
|
|
327
|
+
...input
|
|
328
|
+
};
|
|
246
329
|
}
|
|
247
|
-
|
|
248
|
-
|
|
330
|
+
return {
|
|
331
|
+
length: 256,
|
|
332
|
+
name: input.name
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
function normalizeSymmetricKeyImportOptions(input) {
|
|
336
|
+
if (input.name === SymmetricAlgorithm.HMAC) {
|
|
337
|
+
return {
|
|
338
|
+
hash: 'SHA-256',
|
|
339
|
+
...input
|
|
340
|
+
};
|
|
249
341
|
}
|
|
250
|
-
|
|
342
|
+
return input;
|
|
251
343
|
}
|
|
252
344
|
|
|
253
|
-
class
|
|
254
|
-
// ----------------------------------------------
|
|
255
|
-
async toArrayBuffer() {
|
|
256
|
-
if (this.key.type === 'private') {
|
|
257
|
-
return subtle.exportKey('pkcs8', this.key);
|
|
258
|
-
}
|
|
259
|
-
if (this.key.type === 'public') {
|
|
260
|
-
return subtle.exportKey('spki', this.key);
|
|
261
|
-
}
|
|
262
|
-
return subtle.exportKey('raw', this.key);
|
|
263
|
-
}
|
|
264
|
-
async toUint8Array() {
|
|
265
|
-
const arrayBuffer = await this.toArrayBuffer();
|
|
266
|
-
return new Uint8Array(arrayBuffer);
|
|
267
|
-
}
|
|
268
|
-
async toBase64() {
|
|
269
|
-
const arrayBuffer = await this.toArrayBuffer();
|
|
270
|
-
return arrayBufferToBase64(arrayBuffer);
|
|
271
|
-
}
|
|
272
|
-
async toPem() {
|
|
273
|
-
const base64 = await this.toBase64();
|
|
274
|
-
if (this.key.type === 'public') {
|
|
275
|
-
return encodeSPKIToPem(base64);
|
|
276
|
-
}
|
|
277
|
-
if (this.key.type === 'private') {
|
|
278
|
-
return encodePKCS8ToPEM(base64);
|
|
279
|
-
}
|
|
280
|
-
throw new Error('A symmetric key can not be encoded as PEM');
|
|
281
|
-
}
|
|
282
|
-
async toJWK() {
|
|
283
|
-
return subtle.exportKey('jwk', this.key);
|
|
284
|
-
}
|
|
285
|
-
// ----------------------------------------------
|
|
286
|
-
static async fromPem(ctx) {
|
|
287
|
-
if (ctx.format === 'pkcs8') {
|
|
288
|
-
return CryptoKeyContainer.fromBase64({
|
|
289
|
-
...ctx,
|
|
290
|
-
key: decodePemToPKCS8(ctx.key)
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
return CryptoKeyContainer.fromBase64({
|
|
294
|
-
...ctx,
|
|
295
|
-
key: decodePemToSpki(ctx.key)
|
|
296
|
-
});
|
|
297
|
-
}
|
|
345
|
+
class SymmetricKey extends BaseKey {
|
|
298
346
|
static async fromBase64(ctx) {
|
|
299
347
|
const arrayBuffer = base64ToArrayBuffer(ctx.key);
|
|
300
|
-
return
|
|
348
|
+
return SymmetricKey.fromArrayBuffer({
|
|
301
349
|
...ctx,
|
|
302
350
|
key: arrayBuffer
|
|
303
351
|
});
|
|
304
352
|
}
|
|
305
353
|
static async fromArrayBuffer(ctx) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
354
|
+
const normalizedOptions = normalizeSymmetricKeyImportOptions(ctx.options);
|
|
355
|
+
const cryptoKey = await subtle.importKey(ctx.format, ctx.key, normalizedOptions, true, getKeyUsagesForSymmetricAlgorithm(normalizedOptions.name));
|
|
356
|
+
return new SymmetricKey(cryptoKey);
|
|
357
|
+
}
|
|
358
|
+
static buildImportOptionsForJWTAlgorithm(alg) {
|
|
359
|
+
if (alg === JWTAlgorithm.HS256) {
|
|
360
|
+
return {
|
|
361
|
+
name: SymmetricAlgorithm.HMAC,
|
|
362
|
+
hash: 'SHA-256'
|
|
363
|
+
};
|
|
313
364
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
365
|
+
if (alg === JWTAlgorithm.HS384) {
|
|
366
|
+
return {
|
|
367
|
+
name: SymmetricAlgorithm.HMAC,
|
|
368
|
+
hash: 'SHA-384'
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
if (alg === JWTAlgorithm.HS512) {
|
|
372
|
+
return {
|
|
373
|
+
name: SymmetricAlgorithm.HMAC,
|
|
374
|
+
hash: 'SHA-512'
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
throw new Error(`Signature algorithm ${alg} is not supported.`);
|
|
319
378
|
}
|
|
320
379
|
}
|
|
321
380
|
|
|
381
|
+
async function createSymmetricKey(input) {
|
|
382
|
+
const optionsNormalized = normalizeSymmetricKeyCreateOptions(input);
|
|
383
|
+
return subtle.generateKey(optionsNormalized, true, getKeyUsagesForSymmetricAlgorithm(optionsNormalized.name));
|
|
384
|
+
}
|
|
385
|
+
|
|
322
386
|
/**
|
|
323
387
|
* Decode a JWT token with no verification.
|
|
324
388
|
*
|
|
@@ -482,7 +546,7 @@ async function signToken(claims, context) {
|
|
|
482
546
|
if (typeof context.key === 'string') {
|
|
483
547
|
key = encodePKCS8ToPEM(context.key);
|
|
484
548
|
} else {
|
|
485
|
-
const keyContainer = new
|
|
549
|
+
const keyContainer = new AsymmetricKey(context.key);
|
|
486
550
|
key = await keyContainer.toPem();
|
|
487
551
|
}
|
|
488
552
|
if (context.type === JWKType.RSA) {
|
|
@@ -502,7 +566,7 @@ async function signToken(claims, context) {
|
|
|
502
566
|
if (typeof context.key === 'string') {
|
|
503
567
|
key = context.key;
|
|
504
568
|
} else {
|
|
505
|
-
const keyContainer = new
|
|
569
|
+
const keyContainer = new SymmetricKey(context.key);
|
|
506
570
|
key = await keyContainer.toUint8Array();
|
|
507
571
|
}
|
|
508
572
|
return sign(claims, key, {
|
|
@@ -511,7 +575,7 @@ async function signToken(claims, context) {
|
|
|
511
575
|
});
|
|
512
576
|
}
|
|
513
577
|
}
|
|
514
|
-
throw new
|
|
578
|
+
throw new JWTError();
|
|
515
579
|
}
|
|
516
580
|
|
|
517
581
|
/**
|
|
@@ -549,7 +613,7 @@ async function signToken(claims, context) {
|
|
|
549
613
|
if (typeof context.key === 'string') {
|
|
550
614
|
key = encodeSPKIToPem(context.key);
|
|
551
615
|
} else {
|
|
552
|
-
const keyContainer = new
|
|
616
|
+
const keyContainer = new AsymmetricKey(context.key);
|
|
553
617
|
key = await keyContainer.toPem();
|
|
554
618
|
}
|
|
555
619
|
promise = verify(token, key, {
|
|
@@ -569,7 +633,7 @@ async function signToken(claims, context) {
|
|
|
569
633
|
if (typeof context.key === 'string') {
|
|
570
634
|
key = context.key;
|
|
571
635
|
} else {
|
|
572
|
-
const keyContainer = new
|
|
636
|
+
const keyContainer = new SymmetricKey(context.key);
|
|
573
637
|
key = await keyContainer.toUint8Array();
|
|
574
638
|
}
|
|
575
639
|
promise = verify(token, key, {
|
|
@@ -578,40 +642,61 @@ async function signToken(claims, context) {
|
|
|
578
642
|
});
|
|
579
643
|
}
|
|
580
644
|
}
|
|
581
|
-
|
|
645
|
+
if (promise) {
|
|
646
|
+
output = await promise;
|
|
647
|
+
}
|
|
582
648
|
} catch (e) {
|
|
583
649
|
throw createErrorForJWTError(e);
|
|
584
650
|
}
|
|
585
|
-
if (
|
|
586
|
-
throw new
|
|
587
|
-
message: 'Invalid type.'
|
|
588
|
-
});
|
|
651
|
+
if (!output) {
|
|
652
|
+
throw new JWTError();
|
|
589
653
|
}
|
|
590
654
|
return output;
|
|
591
655
|
}
|
|
592
656
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
657
|
+
/*
|
|
658
|
+
* Copyright (c) 2024-2024.
|
|
659
|
+
* Author Peter Placzek (tada5hi)
|
|
660
|
+
* For the full copyright and license information,
|
|
661
|
+
* view the LICENSE file that was distributed with this source code.
|
|
662
|
+
*/ class DomainEventPublisher {
|
|
663
|
+
publishers;
|
|
664
|
+
constructor(){
|
|
665
|
+
this.publishers = new Set();
|
|
666
|
+
}
|
|
667
|
+
mount(publisher) {
|
|
668
|
+
this.publishers.add(publisher);
|
|
669
|
+
}
|
|
670
|
+
async publish(ctx) {
|
|
671
|
+
const publishers = this.publishers.values();
|
|
672
|
+
while(true){
|
|
673
|
+
const it = publishers.next();
|
|
674
|
+
if (it.done) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
await it.value.publish(ctx);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
607
680
|
}
|
|
608
681
|
|
|
609
|
-
class
|
|
682
|
+
class MemoryCache {
|
|
683
|
+
instance;
|
|
684
|
+
constructor(options = {}){
|
|
685
|
+
this.instance = new TTLCache({
|
|
686
|
+
checkAgeOnGet: true,
|
|
687
|
+
ttl: Infinity,
|
|
688
|
+
...options || {}
|
|
689
|
+
});
|
|
690
|
+
}
|
|
610
691
|
async has(key) {
|
|
611
692
|
return this.instance.has(key);
|
|
612
693
|
}
|
|
613
694
|
async get(key) {
|
|
614
|
-
|
|
695
|
+
const output = await this.instance.get(key);
|
|
696
|
+
if (output) {
|
|
697
|
+
return output;
|
|
698
|
+
}
|
|
699
|
+
return null;
|
|
615
700
|
}
|
|
616
701
|
async set(key, value, options) {
|
|
617
702
|
this.instance.set(key, value, {
|
|
@@ -631,6 +716,9 @@ class MemoryCacheAdapter {
|
|
|
631
716
|
const keys = this.instance.keys();
|
|
632
717
|
let iterator = keys.next();
|
|
633
718
|
while(!iterator.done){
|
|
719
|
+
if (typeof iterator.value !== 'string') {
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
634
722
|
if (iterator.value.startsWith(options.prefix)) {
|
|
635
723
|
this.instance.delete(iterator.value);
|
|
636
724
|
}
|
|
@@ -640,30 +728,56 @@ class MemoryCacheAdapter {
|
|
|
640
728
|
}
|
|
641
729
|
this.instance.clear();
|
|
642
730
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
function isRedisClient(data) {
|
|
734
|
+
return isObject(data) && typeof data.connect === 'function' && typeof data.disconnect === 'function';
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function createRedisClient(input) {
|
|
738
|
+
if (typeof input === 'boolean') {
|
|
739
|
+
return createClient({
|
|
740
|
+
connectionString: 'redis://127.0.0.1'
|
|
648
741
|
});
|
|
649
742
|
}
|
|
743
|
+
if (typeof input === 'string') {
|
|
744
|
+
return createClient({
|
|
745
|
+
connectionString: input
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
if (!isRedisClient(input)) {
|
|
749
|
+
return createClient({
|
|
750
|
+
options: input
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
return input;
|
|
650
754
|
}
|
|
651
755
|
|
|
652
|
-
class
|
|
756
|
+
class RedisCache {
|
|
757
|
+
client;
|
|
758
|
+
jsonAdapter;
|
|
759
|
+
constructor(input){
|
|
760
|
+
this.client = createRedisClient(input);
|
|
761
|
+
this.jsonAdapter = new JsonAdapter(this.client);
|
|
762
|
+
}
|
|
653
763
|
async get(key) {
|
|
654
|
-
|
|
764
|
+
const output = await this.jsonAdapter.get(key);
|
|
765
|
+
if (output) {
|
|
766
|
+
return output;
|
|
767
|
+
}
|
|
768
|
+
return null;
|
|
655
769
|
}
|
|
656
770
|
async has(key) {
|
|
657
771
|
const output = await this.get(key);
|
|
658
|
-
return
|
|
772
|
+
return !!output;
|
|
659
773
|
}
|
|
660
774
|
async set(key, value, options) {
|
|
661
|
-
await this.
|
|
775
|
+
await this.jsonAdapter.set(key, value, {
|
|
662
776
|
milliseconds: options.ttl
|
|
663
777
|
});
|
|
664
778
|
}
|
|
665
779
|
async drop(key) {
|
|
666
|
-
await this.
|
|
780
|
+
await this.jsonAdapter.drop(key);
|
|
667
781
|
}
|
|
668
782
|
async dropMany(keys) {
|
|
669
783
|
const pipeline = this.client.pipeline();
|
|
@@ -684,65 +798,12 @@ class RedisCacheAdapter {
|
|
|
684
798
|
}
|
|
685
799
|
await this.client.flushdb();
|
|
686
800
|
}
|
|
687
|
-
constructor(){
|
|
688
|
-
this.client = useRedisClient();
|
|
689
|
-
this.instance = new JsonAdapter(this.client);
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function createCacheAdapter() {
|
|
694
|
-
if (isRedisClientUsable()) {
|
|
695
|
-
return new RedisCacheAdapter();
|
|
696
|
-
}
|
|
697
|
-
return new MemoryCacheAdapter();
|
|
698
801
|
}
|
|
699
802
|
|
|
700
803
|
function buildCacheKey(options) {
|
|
701
804
|
return buildKeyPath(options);
|
|
702
805
|
}
|
|
703
806
|
|
|
704
|
-
/*
|
|
705
|
-
* Copyright (c) 2024-2024.
|
|
706
|
-
* Author Peter Placzek (tada5hi)
|
|
707
|
-
* For the full copyright and license information,
|
|
708
|
-
* view the LICENSE file that was distributed with this source code.
|
|
709
|
-
*/ class Cache {
|
|
710
|
-
async set(key, value, options = {}) {
|
|
711
|
-
await this.adapter.set(key, value, options);
|
|
712
|
-
}
|
|
713
|
-
async get(key) {
|
|
714
|
-
return this.adapter.get(key);
|
|
715
|
-
}
|
|
716
|
-
async drop(key) {
|
|
717
|
-
return this.adapter.drop(key);
|
|
718
|
-
}
|
|
719
|
-
async dropMany(keys) {
|
|
720
|
-
return this.adapter.dropMany(keys);
|
|
721
|
-
}
|
|
722
|
-
async clear(options = {}) {
|
|
723
|
-
return this.adapter.clear(options);
|
|
724
|
-
}
|
|
725
|
-
constructor(adapter){
|
|
726
|
-
this.adapter = adapter;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
const instance$2 = singa({
|
|
731
|
-
name: 'cache',
|
|
732
|
-
factory: ()=>{
|
|
733
|
-
let adapter;
|
|
734
|
-
if (isRedisClientUsable()) {
|
|
735
|
-
adapter = new RedisCacheAdapter();
|
|
736
|
-
} else {
|
|
737
|
-
adapter = new MemoryCacheAdapter();
|
|
738
|
-
}
|
|
739
|
-
return new Cache(adapter);
|
|
740
|
-
}
|
|
741
|
-
});
|
|
742
|
-
function useCache() {
|
|
743
|
-
return instance$2.use();
|
|
744
|
-
}
|
|
745
|
-
|
|
746
807
|
function createLogger(context) {
|
|
747
808
|
let items;
|
|
748
809
|
const cwd = context.directory || process.cwd();
|
|
@@ -810,13 +871,15 @@ function useVaultClient() {
|
|
|
810
871
|
}
|
|
811
872
|
|
|
812
873
|
function transformDomainEventData(input) {
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
874
|
+
const keys = Object.keys(input);
|
|
875
|
+
for(let i = 0; i < keys.length; i++){
|
|
876
|
+
const key = keys[i];
|
|
877
|
+
const value = input[key];
|
|
878
|
+
if (!isObject$1(value)) {
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (value instanceof Date) {
|
|
882
|
+
input[key] = value.toISOString();
|
|
820
883
|
}
|
|
821
884
|
}
|
|
822
885
|
return input;
|
|
@@ -829,6 +892,10 @@ function buildDomainEventChannelName(input, id) {
|
|
|
829
892
|
}
|
|
830
893
|
|
|
831
894
|
class DomainEventRedisPublisher {
|
|
895
|
+
driver;
|
|
896
|
+
constructor(input){
|
|
897
|
+
this.driver = createRedisClient(input);
|
|
898
|
+
}
|
|
832
899
|
async publish(ctx) {
|
|
833
900
|
const data = JSON.stringify(transformDomainEventData(ctx.content));
|
|
834
901
|
const pipeline = this.driver.pipeline();
|
|
@@ -844,22 +911,25 @@ class DomainEventRedisPublisher {
|
|
|
844
911
|
}
|
|
845
912
|
await pipeline.exec();
|
|
846
913
|
}
|
|
847
|
-
constructor(client){
|
|
848
|
-
this.driver = client;
|
|
849
|
-
}
|
|
850
914
|
}
|
|
851
915
|
|
|
852
916
|
class DomainEventSocketPublisher {
|
|
917
|
+
driver;
|
|
918
|
+
constructor(input){
|
|
919
|
+
const client = createRedisClient(input);
|
|
920
|
+
this.driver = new Emitter(client);
|
|
921
|
+
}
|
|
853
922
|
async publish(ctx) {
|
|
854
923
|
ctx.content = transformDomainEventData(ctx.content);
|
|
855
924
|
for(let i = 0; i < ctx.destinations.length; i++){
|
|
925
|
+
const destination = ctx.destinations[i];
|
|
856
926
|
let emitter;
|
|
857
|
-
if (
|
|
858
|
-
emitter = this.driver.of(
|
|
927
|
+
if (destination.namespace) {
|
|
928
|
+
emitter = this.driver.of(destination.namespace);
|
|
859
929
|
} else {
|
|
860
930
|
emitter = this.driver;
|
|
861
931
|
}
|
|
862
|
-
let roomName = buildDomainEventChannelName(
|
|
932
|
+
let roomName = buildDomainEventChannelName(destination.channel);
|
|
863
933
|
const fullEventName = buildEventFullName(ctx.content.type, ctx.content.event);
|
|
864
934
|
emitter.in(roomName).emit(fullEventName, {
|
|
865
935
|
...ctx.content,
|
|
@@ -867,8 +937,8 @@ class DomainEventSocketPublisher {
|
|
|
867
937
|
roomName
|
|
868
938
|
}
|
|
869
939
|
});
|
|
870
|
-
if (typeof
|
|
871
|
-
roomName = buildDomainEventChannelName(
|
|
940
|
+
if (typeof destination.channel === 'function') {
|
|
941
|
+
roomName = buildDomainEventChannelName(destination.channel, ctx.content.data.id);
|
|
872
942
|
emitter.in(roomName).emit(fullEventName, {
|
|
873
943
|
...ctx.content,
|
|
874
944
|
meta: {
|
|
@@ -879,30 +949,6 @@ class DomainEventSocketPublisher {
|
|
|
879
949
|
}
|
|
880
950
|
}
|
|
881
951
|
}
|
|
882
|
-
constructor(client){
|
|
883
|
-
this.driver = new Emitter(client);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
class DomainEventPublisher {
|
|
888
|
-
async publish(ctx) {
|
|
889
|
-
const publishers = this.publishers.values();
|
|
890
|
-
while(true){
|
|
891
|
-
const it = publishers.next();
|
|
892
|
-
if (it.done) {
|
|
893
|
-
return;
|
|
894
|
-
}
|
|
895
|
-
await it.value.publish(ctx);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
constructor(){
|
|
899
|
-
this.publishers = new Set();
|
|
900
|
-
if (isRedisClientUsable()) {
|
|
901
|
-
const client = useRedisClient();
|
|
902
|
-
this.publishers.add(new DomainEventRedisPublisher(client));
|
|
903
|
-
this.publishers.add(new DomainEventSocketPublisher(client));
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
952
|
}
|
|
907
953
|
|
|
908
954
|
/*
|
|
@@ -914,5 +960,5 @@ class DomainEventPublisher {
|
|
|
914
960
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
915
961
|
}
|
|
916
962
|
|
|
917
|
-
export {
|
|
963
|
+
export { AsymmetricKey, BaseKey, CryptoAsymmetricAlgorithm, DomainEventPublisher, DomainEventRedisPublisher, DomainEventSocketPublisher, MemoryCache, RedisCache, SymmetricAlgorithm, SymmetricKey, buildCacheKey, compare, createAsymmetricKeyPair, createLogger, createRedisClient, createSymmetricKey, decodePemToPKCS8, decodePemToSpki, encodePKCS8ToPEM, encodeSPKIToPem, extractTokenHeader, extractTokenPayload, getKeyUsagesForAsymmetricAlgorithm, getKeyUsagesForSymmetricAlgorithm, hasOwnProperty, hash, isAsymmetricAlgorithm, isLoggerUsable, isRedisClient, isSymmetricAlgorithm, isVaultClientUsable, normalizeAsymmetricKeyImportOptions, normalizeAsymmetricKeyPairCreateOptions, setLogger, setLoggerFactory, setVaultFactory, signToken, useLogger, useVaultClient, verifyToken };
|
|
918
964
|
//# sourceMappingURL=index.mjs.map
|