@authup/server-kit 1.0.0-beta.27 → 1.0.0-beta.29
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.cjs
DELETED
|
@@ -1,1005 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var bcrypt = require('@node-rs/bcrypt');
|
|
4
|
-
var uncrypto = require('uncrypto');
|
|
5
|
-
var kit = require('@authup/kit');
|
|
6
|
-
var specs = require('@authup/specs');
|
|
7
|
-
var jsonwebtoken = require('@node-rs/jsonwebtoken');
|
|
8
|
-
var smob = require('smob');
|
|
9
|
-
var redisExtension = require('redis-extension');
|
|
10
|
-
var singa = require('singa');
|
|
11
|
-
var TTLCache = require('@isaacs/ttlcache');
|
|
12
|
-
var path = require('node:path');
|
|
13
|
-
var process = require('node:process');
|
|
14
|
-
var winston = require('winston');
|
|
15
|
-
var vault = require('@hapic/vault');
|
|
16
|
-
var coreRealtimeKit = require('@authup/core-realtime-kit');
|
|
17
|
-
var redisEmitter = require('@socket.io/redis-emitter');
|
|
18
|
-
|
|
19
|
-
async function compare(value, hashedValue) {
|
|
20
|
-
return bcrypt.compare(value, hashedValue);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function hash(str, rounds = 10) {
|
|
24
|
-
return bcrypt.hash(str, rounds);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/*
|
|
28
|
-
* Copyright (c) 2022-2024.
|
|
29
|
-
* Author Peter Placzek (tada5hi)
|
|
30
|
-
* For the full copyright and license information,
|
|
31
|
-
* view the LICENSE file that was distributed with this source code.
|
|
32
|
-
*/ var CryptoAsymmetricAlgorithm = /*#__PURE__*/ function(CryptoAsymmetricAlgorithm) {
|
|
33
|
-
CryptoAsymmetricAlgorithm["RSA_PSS"] = "RSA-PSS";
|
|
34
|
-
CryptoAsymmetricAlgorithm["RSASSA_PKCS1_V1_5"] = "RSASSA-PKCS1-v1_5";
|
|
35
|
-
CryptoAsymmetricAlgorithm["RSA_OAEP"] = "RSA-OAEP";
|
|
36
|
-
CryptoAsymmetricAlgorithm["ECDSA"] = "ECDSA";
|
|
37
|
-
CryptoAsymmetricAlgorithm["ECDH"] = "ECDH";
|
|
38
|
-
return CryptoAsymmetricAlgorithm;
|
|
39
|
-
}({});
|
|
40
|
-
|
|
41
|
-
function isAsymmetricAlgorithm(input) {
|
|
42
|
-
return Object.values(CryptoAsymmetricAlgorithm).indexOf(input) !== -1;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @see https://nodejs.org/api/webcrypto.html#cryptokeyusages
|
|
47
|
-
*/ function getKeyUsagesForAsymmetricAlgorithm(name, format) {
|
|
48
|
-
if (name === CryptoAsymmetricAlgorithm.RSA_PSS || name === CryptoAsymmetricAlgorithm.ECDSA || name === CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5) {
|
|
49
|
-
if (format === 'spki') {
|
|
50
|
-
return [
|
|
51
|
-
'verify'
|
|
52
|
-
];
|
|
53
|
-
}
|
|
54
|
-
if (format === 'pkcs8') {
|
|
55
|
-
return [
|
|
56
|
-
'sign'
|
|
57
|
-
];
|
|
58
|
-
}
|
|
59
|
-
return [
|
|
60
|
-
'sign',
|
|
61
|
-
'verify'
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
if (name === CryptoAsymmetricAlgorithm.ECDH) {
|
|
65
|
-
if (format === 'spki') {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
return [
|
|
69
|
-
'deriveKey',
|
|
70
|
-
'deriveBits'
|
|
71
|
-
];
|
|
72
|
-
}
|
|
73
|
-
if (name === CryptoAsymmetricAlgorithm.RSA_OAEP) {
|
|
74
|
-
if (format === 'spki') {
|
|
75
|
-
return [
|
|
76
|
-
'encrypt'
|
|
77
|
-
];
|
|
78
|
-
}
|
|
79
|
-
if (format === 'pkcs8') {
|
|
80
|
-
return [
|
|
81
|
-
'decrypt'
|
|
82
|
-
];
|
|
83
|
-
}
|
|
84
|
-
return [
|
|
85
|
-
'encrypt',
|
|
86
|
-
'decrypt'
|
|
87
|
-
];
|
|
88
|
-
}
|
|
89
|
-
throw new SyntaxError(`Key usages can not be determined for asymmetric algorithm: ${name}`);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function normalizeAsymmetricKeyPairCreateOptions(options) {
|
|
93
|
-
let optionsNormalized;
|
|
94
|
-
switch(options.name){
|
|
95
|
-
case CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5:
|
|
96
|
-
case CryptoAsymmetricAlgorithm.RSA_PSS:
|
|
97
|
-
case CryptoAsymmetricAlgorithm.RSA_OAEP:
|
|
98
|
-
{
|
|
99
|
-
optionsNormalized = {
|
|
100
|
-
modulusLength: 2048,
|
|
101
|
-
publicExponent: new Uint8Array([
|
|
102
|
-
0x01,
|
|
103
|
-
0x00,
|
|
104
|
-
0x01
|
|
105
|
-
]),
|
|
106
|
-
hash: 'SHA-256',
|
|
107
|
-
...options
|
|
108
|
-
};
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
case CryptoAsymmetricAlgorithm.ECDSA:
|
|
112
|
-
case CryptoAsymmetricAlgorithm.ECDH:
|
|
113
|
-
{
|
|
114
|
-
optionsNormalized = {
|
|
115
|
-
namedCurve: 'P-256',
|
|
116
|
-
...options
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return optionsNormalized;
|
|
121
|
-
}
|
|
122
|
-
function normalizeAsymmetricKeyImportOptions(options) {
|
|
123
|
-
let optionsNormalized;
|
|
124
|
-
switch(options.name){
|
|
125
|
-
case CryptoAsymmetricAlgorithm.RSASSA_PKCS1_V1_5:
|
|
126
|
-
case CryptoAsymmetricAlgorithm.RSA_PSS:
|
|
127
|
-
case CryptoAsymmetricAlgorithm.RSA_OAEP:
|
|
128
|
-
{
|
|
129
|
-
optionsNormalized = {
|
|
130
|
-
hash: 'SHA-256',
|
|
131
|
-
...options
|
|
132
|
-
};
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
case CryptoAsymmetricAlgorithm.ECDSA:
|
|
136
|
-
case CryptoAsymmetricAlgorithm.ECDH:
|
|
137
|
-
{
|
|
138
|
-
optionsNormalized = {
|
|
139
|
-
namedCurve: 'P-256',
|
|
140
|
-
...options
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return optionsNormalized;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function createAsymmetricKeyPair(options) {
|
|
148
|
-
const optionsNormalized = normalizeAsymmetricKeyPairCreateOptions(options);
|
|
149
|
-
return uncrypto.subtle.generateKey(optionsNormalized, true, getKeyUsagesForAsymmetricAlgorithm(optionsNormalized.name));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/*
|
|
153
|
-
* Copyright (c) 2024-2024.
|
|
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);
|
|
176
|
-
}
|
|
177
|
-
function decodePemToSpki(input) {
|
|
178
|
-
return dec('PUBLIC KEY', input);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/*
|
|
182
|
-
* Copyright (c) 2024.
|
|
183
|
-
* Author Peter Placzek (tada5hi)
|
|
184
|
-
* For the full copyright and license information,
|
|
185
|
-
* view the LICENSE file that was distributed with this source code.
|
|
186
|
-
*/ var SymmetricAlgorithm = /*#__PURE__*/ function(SymmetricAlgorithm) {
|
|
187
|
-
SymmetricAlgorithm["HMAC"] = "HMAC";
|
|
188
|
-
SymmetricAlgorithm["AES_CTR"] = "AES-CTR";
|
|
189
|
-
SymmetricAlgorithm["AES_CBC"] = "AES-CBC";
|
|
190
|
-
SymmetricAlgorithm["AES_GCM"] = "AES-GCM";
|
|
191
|
-
return SymmetricAlgorithm;
|
|
192
|
-
}({});
|
|
193
|
-
|
|
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
|
-
function isSymmetricAlgorithm(input) {
|
|
217
|
-
return Object.values(SymmetricAlgorithm).indexOf(input) !== -1;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function getKeyUsagesForSymmetricAlgorithm(name) {
|
|
221
|
-
/**
|
|
222
|
-
* @see https://nodejs.org/api/webcrypto.html#cryptokeyusages
|
|
223
|
-
*/ if (name === SymmetricAlgorithm.HMAC) {
|
|
224
|
-
return [
|
|
225
|
-
'sign',
|
|
226
|
-
'verify'
|
|
227
|
-
];
|
|
228
|
-
}
|
|
229
|
-
if (name === SymmetricAlgorithm.AES_CBC || name === SymmetricAlgorithm.AES_GCM || name === SymmetricAlgorithm.AES_CTR) {
|
|
230
|
-
return [
|
|
231
|
-
'encrypt',
|
|
232
|
-
'decrypt'
|
|
233
|
-
];
|
|
234
|
-
}
|
|
235
|
-
throw new SyntaxError(`Key usages can not be determined for symmetric algorithm: ${name}`);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async function createSymmetricKey(input) {
|
|
239
|
-
const optionsNormalized = normalizeSymmetricKeyCreateOptions(input);
|
|
240
|
-
return uncrypto.subtle.generateKey(optionsNormalized, true, getKeyUsagesForSymmetricAlgorithm(optionsNormalized.name));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function getKeyUsagesForAlgorithm(name, format) {
|
|
244
|
-
if (isAsymmetricAlgorithm(name)) {
|
|
245
|
-
return getKeyUsagesForAsymmetricAlgorithm(name, format);
|
|
246
|
-
}
|
|
247
|
-
if (isSymmetricAlgorithm(name)) {
|
|
248
|
-
return getKeyUsagesForSymmetricAlgorithm(name);
|
|
249
|
-
}
|
|
250
|
-
throw new SyntaxError(`Key usages can not be determined for algorithm: ${name}`);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
class CryptoKeyContainer {
|
|
254
|
-
// ----------------------------------------------
|
|
255
|
-
async toArrayBuffer() {
|
|
256
|
-
if (this.key.type === 'private') {
|
|
257
|
-
return uncrypto.subtle.exportKey('pkcs8', this.key);
|
|
258
|
-
}
|
|
259
|
-
if (this.key.type === 'public') {
|
|
260
|
-
return uncrypto.subtle.exportKey('spki', this.key);
|
|
261
|
-
}
|
|
262
|
-
return uncrypto.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 kit.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 uncrypto.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
|
-
}
|
|
298
|
-
static async fromBase64(ctx) {
|
|
299
|
-
const arrayBuffer = kit.base64ToArrayBuffer(ctx.key);
|
|
300
|
-
return CryptoKeyContainer.fromArrayBuffer({
|
|
301
|
-
...ctx,
|
|
302
|
-
key: arrayBuffer
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
static async fromArrayBuffer(ctx) {
|
|
306
|
-
let normalizedOptions;
|
|
307
|
-
if (ctx.format === 'spki' || ctx.format === 'pkcs8') {
|
|
308
|
-
normalizedOptions = normalizeAsymmetricKeyImportOptions(ctx.options);
|
|
309
|
-
} else if (ctx.format === 'raw') {
|
|
310
|
-
normalizedOptions = normalizeSymmetricKeyImportOptions(ctx.options);
|
|
311
|
-
} else {
|
|
312
|
-
throw new SyntaxError(`Format ${ctx.format} is not supported.`);
|
|
313
|
-
}
|
|
314
|
-
const cryptoKey = await uncrypto.subtle.importKey(ctx.format, ctx.key, normalizedOptions, true, getKeyUsagesForAlgorithm(normalizedOptions.name, ctx.format));
|
|
315
|
-
return new CryptoKeyContainer(cryptoKey);
|
|
316
|
-
}
|
|
317
|
-
constructor(cryptoKey){
|
|
318
|
-
this.key = cryptoKey;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Decode a JWT token with no verification.
|
|
324
|
-
*
|
|
325
|
-
* @param token
|
|
326
|
-
*
|
|
327
|
-
* @throws JWTError
|
|
328
|
-
*/ function extractTokenHeader(token) {
|
|
329
|
-
const parts = token.split('.');
|
|
330
|
-
if (parts.length !== 3) {
|
|
331
|
-
throw specs.JWTError.invalid();
|
|
332
|
-
}
|
|
333
|
-
const [headerBase64] = parts;
|
|
334
|
-
try {
|
|
335
|
-
const payload = atob(headerBase64);
|
|
336
|
-
return JSON.parse(payload);
|
|
337
|
-
/*
|
|
338
|
-
return {
|
|
339
|
-
typ: 'JWT',
|
|
340
|
-
alg: transformInternalToJWTAlgorithm(header.algorithm),
|
|
341
|
-
cty: header.contentType,
|
|
342
|
-
jku: header.jsonKeyUrl,
|
|
343
|
-
kid: header.keyId,
|
|
344
|
-
x5u: header.x5Url,
|
|
345
|
-
x5c: header.x5CertChain,
|
|
346
|
-
x5t: header.x5CertThumbprint,
|
|
347
|
-
'x5t#S256': header.x5TS256CertThumbprint,
|
|
348
|
-
};
|
|
349
|
-
*/ } catch (e) {
|
|
350
|
-
throw specs.JWTError.headerInvalid('The token header could not be extracted.');
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* @param token
|
|
355
|
-
*
|
|
356
|
-
* @throws JWTError
|
|
357
|
-
*/ function extractTokenPayload(token) {
|
|
358
|
-
const parts = token.split('.');
|
|
359
|
-
if (parts.length !== 3) {
|
|
360
|
-
throw specs.JWTError.invalid();
|
|
361
|
-
}
|
|
362
|
-
const [, payloadBase64] = parts;
|
|
363
|
-
try {
|
|
364
|
-
const payload = atob(payloadBase64);
|
|
365
|
-
return JSON.parse(payload);
|
|
366
|
-
} catch (e) {
|
|
367
|
-
throw specs.JWTError.payloadInvalid('The token payload could not be extracted.');
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function createErrorForJWTError(e) {
|
|
372
|
-
if (smob.isObject(e)) {
|
|
373
|
-
if (typeof e.name === 'string') {
|
|
374
|
-
switch(e.name){
|
|
375
|
-
case 'TokenExpiredError':
|
|
376
|
-
{
|
|
377
|
-
return specs.JWTError.expired();
|
|
378
|
-
}
|
|
379
|
-
case 'NotBeforeError':
|
|
380
|
-
{
|
|
381
|
-
if (typeof e.date === 'string' || e.date instanceof Date) {
|
|
382
|
-
return specs.JWTError.notActiveBefore(e.date);
|
|
383
|
-
}
|
|
384
|
-
break;
|
|
385
|
-
}
|
|
386
|
-
case 'JsonWebTokenError':
|
|
387
|
-
{
|
|
388
|
-
if (typeof e.message === 'string') {
|
|
389
|
-
return specs.JWTError.payloadInvalid(e.message);
|
|
390
|
-
}
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
// @see https://github.com/Keats/jsonwebtoken/blob/master/src/errors.rs
|
|
396
|
-
switch(e.message){
|
|
397
|
-
case 'ExpiredSignature':
|
|
398
|
-
{
|
|
399
|
-
return specs.JWTError.expired();
|
|
400
|
-
}
|
|
401
|
-
case 'ImmatureSignature':
|
|
402
|
-
{
|
|
403
|
-
return specs.JWTError.notActiveBefore();
|
|
404
|
-
}
|
|
405
|
-
case 'InvalidToken':
|
|
406
|
-
case 'InvalidSignature':
|
|
407
|
-
{
|
|
408
|
-
return specs.JWTError.payloadInvalid();
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return new specs.JWTError({
|
|
413
|
-
cause: e,
|
|
414
|
-
logMessage: true,
|
|
415
|
-
message: 'The JWT error could not be determined.'
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
function transformJWTAlgorithmToInternal(algorithm) {
|
|
419
|
-
switch(algorithm){
|
|
420
|
-
case specs.JWTAlgorithm.HS256:
|
|
421
|
-
{
|
|
422
|
-
return jsonwebtoken.Algorithm.HS256;
|
|
423
|
-
}
|
|
424
|
-
case specs.JWTAlgorithm.HS384:
|
|
425
|
-
{
|
|
426
|
-
return jsonwebtoken.Algorithm.HS384;
|
|
427
|
-
}
|
|
428
|
-
case specs.JWTAlgorithm.HS512:
|
|
429
|
-
{
|
|
430
|
-
return jsonwebtoken.Algorithm.HS512;
|
|
431
|
-
}
|
|
432
|
-
case specs.JWTAlgorithm.RS256:
|
|
433
|
-
{
|
|
434
|
-
return jsonwebtoken.Algorithm.RS256;
|
|
435
|
-
}
|
|
436
|
-
case specs.JWTAlgorithm.RS384:
|
|
437
|
-
{
|
|
438
|
-
return jsonwebtoken.Algorithm.RS384;
|
|
439
|
-
}
|
|
440
|
-
case specs.JWTAlgorithm.RS512:
|
|
441
|
-
{
|
|
442
|
-
return jsonwebtoken.Algorithm.RS512;
|
|
443
|
-
}
|
|
444
|
-
case specs.JWTAlgorithm.ES256:
|
|
445
|
-
{
|
|
446
|
-
return jsonwebtoken.Algorithm.ES256;
|
|
447
|
-
}
|
|
448
|
-
case specs.JWTAlgorithm.ES384:
|
|
449
|
-
{
|
|
450
|
-
return jsonwebtoken.Algorithm.ES384;
|
|
451
|
-
}
|
|
452
|
-
case specs.JWTAlgorithm.PS256:
|
|
453
|
-
{
|
|
454
|
-
return jsonwebtoken.Algorithm.PS256;
|
|
455
|
-
}
|
|
456
|
-
case specs.JWTAlgorithm.PS384:
|
|
457
|
-
{
|
|
458
|
-
return jsonwebtoken.Algorithm.PS384;
|
|
459
|
-
}
|
|
460
|
-
case specs.JWTAlgorithm.PS512:
|
|
461
|
-
{
|
|
462
|
-
return jsonwebtoken.Algorithm.PS512;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
throw new Error(`The algorithm ${algorithm} is not supported.`);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const getUtcTimestamp = ()=>Math.floor(new Date().getTime() / 1000);
|
|
469
|
-
async function signToken(claims, context) {
|
|
470
|
-
if (typeof claims.exp !== 'number') {
|
|
471
|
-
claims.exp = getUtcTimestamp() + 3600;
|
|
472
|
-
}
|
|
473
|
-
if (typeof claims.iat !== 'number') {
|
|
474
|
-
claims.iat = getUtcTimestamp();
|
|
475
|
-
}
|
|
476
|
-
switch(context.type){
|
|
477
|
-
case specs.JWKType.RSA:
|
|
478
|
-
case specs.JWKType.EC:
|
|
479
|
-
{
|
|
480
|
-
let algorithm;
|
|
481
|
-
let key;
|
|
482
|
-
if (typeof context.key === 'string') {
|
|
483
|
-
key = encodePKCS8ToPEM(context.key);
|
|
484
|
-
} else {
|
|
485
|
-
const keyContainer = new CryptoKeyContainer(context.key);
|
|
486
|
-
key = await keyContainer.toPem();
|
|
487
|
-
}
|
|
488
|
-
if (context.type === specs.JWKType.RSA) {
|
|
489
|
-
algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : jsonwebtoken.Algorithm.RS256;
|
|
490
|
-
} else {
|
|
491
|
-
algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : jsonwebtoken.Algorithm.ES256;
|
|
492
|
-
}
|
|
493
|
-
return jsonwebtoken.sign(claims, key, {
|
|
494
|
-
algorithm,
|
|
495
|
-
keyId: context.keyId
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
case specs.JWKType.OCT:
|
|
499
|
-
{
|
|
500
|
-
const algorithm = context.algorithm ? transformJWTAlgorithmToInternal(context.algorithm) : jsonwebtoken.Algorithm.HS256;
|
|
501
|
-
let key;
|
|
502
|
-
if (typeof context.key === 'string') {
|
|
503
|
-
key = context.key;
|
|
504
|
-
} else {
|
|
505
|
-
const keyContainer = new CryptoKeyContainer(context.key);
|
|
506
|
-
key = await keyContainer.toUint8Array();
|
|
507
|
-
}
|
|
508
|
-
return jsonwebtoken.sign(claims, key, {
|
|
509
|
-
algorithm,
|
|
510
|
-
keyId: context.keyId
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
throw new specs.OAuth2Error();
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Verify JWT.
|
|
519
|
-
*
|
|
520
|
-
* @param token
|
|
521
|
-
* @param context
|
|
522
|
-
*
|
|
523
|
-
* @throws OAuth2Error
|
|
524
|
-
*/ async function verifyToken(token, context) {
|
|
525
|
-
let promise;
|
|
526
|
-
let output;
|
|
527
|
-
try {
|
|
528
|
-
switch(context.type){
|
|
529
|
-
case specs.JWKType.RSA:
|
|
530
|
-
case specs.JWKType.EC:
|
|
531
|
-
{
|
|
532
|
-
let algorithms;
|
|
533
|
-
if (context.type === specs.JWKType.RSA) {
|
|
534
|
-
algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
535
|
-
jsonwebtoken.Algorithm.RS256,
|
|
536
|
-
jsonwebtoken.Algorithm.RS384,
|
|
537
|
-
jsonwebtoken.Algorithm.RS512,
|
|
538
|
-
jsonwebtoken.Algorithm.PS256,
|
|
539
|
-
jsonwebtoken.Algorithm.PS384,
|
|
540
|
-
jsonwebtoken.Algorithm.PS512
|
|
541
|
-
];
|
|
542
|
-
} else {
|
|
543
|
-
algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
544
|
-
jsonwebtoken.Algorithm.ES256,
|
|
545
|
-
jsonwebtoken.Algorithm.ES384
|
|
546
|
-
];
|
|
547
|
-
}
|
|
548
|
-
let key;
|
|
549
|
-
if (typeof context.key === 'string') {
|
|
550
|
-
key = encodeSPKIToPem(context.key);
|
|
551
|
-
} else {
|
|
552
|
-
const keyContainer = new CryptoKeyContainer(context.key);
|
|
553
|
-
key = await keyContainer.toPem();
|
|
554
|
-
}
|
|
555
|
-
promise = jsonwebtoken.verify(token, key, {
|
|
556
|
-
algorithms,
|
|
557
|
-
validateNbf: true
|
|
558
|
-
});
|
|
559
|
-
break;
|
|
560
|
-
}
|
|
561
|
-
case specs.JWKType.OCT:
|
|
562
|
-
{
|
|
563
|
-
const algorithms = context.algorithms ? context.algorithms.map((algorithm)=>transformJWTAlgorithmToInternal(algorithm)) : [
|
|
564
|
-
jsonwebtoken.Algorithm.HS256,
|
|
565
|
-
jsonwebtoken.Algorithm.HS384,
|
|
566
|
-
jsonwebtoken.Algorithm.HS512
|
|
567
|
-
];
|
|
568
|
-
let key;
|
|
569
|
-
if (typeof context.key === 'string') {
|
|
570
|
-
key = context.key;
|
|
571
|
-
} else {
|
|
572
|
-
const keyContainer = new CryptoKeyContainer(context.key);
|
|
573
|
-
key = await keyContainer.toUint8Array();
|
|
574
|
-
}
|
|
575
|
-
promise = jsonwebtoken.verify(token, key, {
|
|
576
|
-
algorithms,
|
|
577
|
-
validateNbf: true
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
output = await promise;
|
|
582
|
-
} catch (e) {
|
|
583
|
-
throw createErrorForJWTError(e);
|
|
584
|
-
}
|
|
585
|
-
if (typeof output === 'undefined') {
|
|
586
|
-
throw new specs.OAuth2Error({
|
|
587
|
-
message: 'Invalid type.'
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
return output;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
const instance$3 = singa.singa({
|
|
594
|
-
name: 'redis'
|
|
595
|
-
});
|
|
596
|
-
function setRedisFactory(factory) {
|
|
597
|
-
instance$3.setFactory(factory);
|
|
598
|
-
}
|
|
599
|
-
function isRedisClientUsable() {
|
|
600
|
-
return instance$3.has() || instance$3.hasFactory();
|
|
601
|
-
}
|
|
602
|
-
function setRedisClient(input) {
|
|
603
|
-
instance$3.set(input);
|
|
604
|
-
}
|
|
605
|
-
function useRedisClient() {
|
|
606
|
-
return instance$3.use();
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
class MemoryCacheAdapter {
|
|
610
|
-
async has(key) {
|
|
611
|
-
return this.instance.has(key);
|
|
612
|
-
}
|
|
613
|
-
async get(key) {
|
|
614
|
-
return this.instance.get(key);
|
|
615
|
-
}
|
|
616
|
-
async set(key, value, options) {
|
|
617
|
-
this.instance.set(key, value, {
|
|
618
|
-
ttl: options.ttl
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
async drop(key) {
|
|
622
|
-
this.instance.delete(key);
|
|
623
|
-
}
|
|
624
|
-
async dropMany(keys) {
|
|
625
|
-
for(let i = 0; i < keys.length; i++){
|
|
626
|
-
this.instance.delete(keys[i]);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
async clear(options = {}) {
|
|
630
|
-
if (options.prefix) {
|
|
631
|
-
const keys = this.instance.keys();
|
|
632
|
-
let iterator = keys.next();
|
|
633
|
-
while(!iterator.done){
|
|
634
|
-
if (iterator.value.startsWith(options.prefix)) {
|
|
635
|
-
this.instance.delete(iterator.value);
|
|
636
|
-
}
|
|
637
|
-
iterator = keys.next();
|
|
638
|
-
}
|
|
639
|
-
return;
|
|
640
|
-
}
|
|
641
|
-
this.instance.clear();
|
|
642
|
-
}
|
|
643
|
-
constructor(options = {}){
|
|
644
|
-
this.instance = new TTLCache({
|
|
645
|
-
checkAgeOnGet: true,
|
|
646
|
-
ttl: Infinity,
|
|
647
|
-
...options || {}
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
class RedisCacheAdapter {
|
|
653
|
-
async get(key) {
|
|
654
|
-
return this.instance.get(key);
|
|
655
|
-
}
|
|
656
|
-
async has(key) {
|
|
657
|
-
const output = await this.get(key);
|
|
658
|
-
return typeof output !== 'undefined';
|
|
659
|
-
}
|
|
660
|
-
async set(key, value, options) {
|
|
661
|
-
await this.instance.set(key, value, {
|
|
662
|
-
milliseconds: options.ttl
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
async drop(key) {
|
|
666
|
-
await this.instance.drop(key);
|
|
667
|
-
}
|
|
668
|
-
async dropMany(keys) {
|
|
669
|
-
const pipeline = this.client.pipeline();
|
|
670
|
-
for(let i = 0; i < keys.length; i++){
|
|
671
|
-
pipeline.del(keys[i]);
|
|
672
|
-
}
|
|
673
|
-
await pipeline.exec();
|
|
674
|
-
}
|
|
675
|
-
async clear(options = {}) {
|
|
676
|
-
if (options.prefix) {
|
|
677
|
-
const pipeline = this.client.pipeline();
|
|
678
|
-
const keys = await this.client.keys(`${options.prefix}*`);
|
|
679
|
-
for(let i = 0; i < keys.length; i++){
|
|
680
|
-
pipeline.del(keys[i]);
|
|
681
|
-
}
|
|
682
|
-
await pipeline.exec();
|
|
683
|
-
return;
|
|
684
|
-
}
|
|
685
|
-
await this.client.flushdb();
|
|
686
|
-
}
|
|
687
|
-
constructor(){
|
|
688
|
-
this.client = useRedisClient();
|
|
689
|
-
this.instance = new redisExtension.JsonAdapter(this.client);
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function createCacheAdapter() {
|
|
694
|
-
if (isRedisClientUsable()) {
|
|
695
|
-
return new RedisCacheAdapter();
|
|
696
|
-
}
|
|
697
|
-
return new MemoryCacheAdapter();
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
function buildCacheKey(options) {
|
|
701
|
-
return redisExtension.buildKeyPath(options);
|
|
702
|
-
}
|
|
703
|
-
|
|
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.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
|
-
function createLogger(context) {
|
|
747
|
-
let items;
|
|
748
|
-
const cwd = context.directory || process.cwd();
|
|
749
|
-
if (context.env === 'production') {
|
|
750
|
-
items = [
|
|
751
|
-
new winston.transports.Console({
|
|
752
|
-
level: 'info'
|
|
753
|
-
}),
|
|
754
|
-
new winston.transports.File({
|
|
755
|
-
filename: path.join(cwd, 'http.log'),
|
|
756
|
-
level: 'http',
|
|
757
|
-
maxsize: 10 * 1024 * 1024,
|
|
758
|
-
maxFiles: 5
|
|
759
|
-
}),
|
|
760
|
-
new winston.transports.File({
|
|
761
|
-
filename: path.join(cwd, 'error.log'),
|
|
762
|
-
level: 'warn',
|
|
763
|
-
maxsize: 10 * 1024 * 1024,
|
|
764
|
-
maxFiles: 5
|
|
765
|
-
})
|
|
766
|
-
];
|
|
767
|
-
} else {
|
|
768
|
-
items = [
|
|
769
|
-
new winston.transports.Console({
|
|
770
|
-
level: 'debug'
|
|
771
|
-
})
|
|
772
|
-
];
|
|
773
|
-
}
|
|
774
|
-
// @see https://github.com/winstonjs/triple-beam/blob/master/config/npm.js
|
|
775
|
-
return winston.createLogger({
|
|
776
|
-
format: winston.format.combine(winston.format.errors({
|
|
777
|
-
stack: true
|
|
778
|
-
}), winston.format.timestamp(), winston.format.colorize(), winston.format.simple()),
|
|
779
|
-
transports: items
|
|
780
|
-
});
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
const instance$1 = singa.singa({
|
|
784
|
-
name: 'logger'
|
|
785
|
-
});
|
|
786
|
-
function setLoggerFactory(factory) {
|
|
787
|
-
instance$1.setFactory(factory);
|
|
788
|
-
}
|
|
789
|
-
function isLoggerUsable() {
|
|
790
|
-
return instance$1.has() || instance$1.hasFactory();
|
|
791
|
-
}
|
|
792
|
-
function setLogger(input) {
|
|
793
|
-
instance$1.set(input);
|
|
794
|
-
}
|
|
795
|
-
function useLogger() {
|
|
796
|
-
return instance$1.use();
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
const instance = singa.singa({
|
|
800
|
-
name: 'vault'
|
|
801
|
-
});
|
|
802
|
-
function setVaultFactory(factory) {
|
|
803
|
-
instance.setFactory(factory);
|
|
804
|
-
}
|
|
805
|
-
function isVaultClientUsable() {
|
|
806
|
-
return instance.has() || instance.hasFactory();
|
|
807
|
-
}
|
|
808
|
-
function useVaultClient() {
|
|
809
|
-
return instance.use();
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
function transformDomainEventData(input) {
|
|
813
|
-
if (kit.isObject(input)) {
|
|
814
|
-
const keys = Object.keys(input);
|
|
815
|
-
for(let i = 0; i < keys.length; i++){
|
|
816
|
-
const value = input[keys[i]];
|
|
817
|
-
if (value instanceof Date) {
|
|
818
|
-
input[keys[i]] = value.toISOString();
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
return input;
|
|
823
|
-
}
|
|
824
|
-
function buildDomainEventChannelName(input, id) {
|
|
825
|
-
if (typeof input === 'string') {
|
|
826
|
-
return input;
|
|
827
|
-
}
|
|
828
|
-
return input(id);
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
class DomainEventRedisPublisher {
|
|
832
|
-
async publish(ctx) {
|
|
833
|
-
const data = JSON.stringify(transformDomainEventData(ctx.content));
|
|
834
|
-
const pipeline = this.driver.pipeline();
|
|
835
|
-
for(let i = 0; i < ctx.destinations.length; i++){
|
|
836
|
-
const { namespace } = ctx.destinations[i];
|
|
837
|
-
const keyPrefix = namespace ? `${namespace}:` : '';
|
|
838
|
-
let key = keyPrefix + buildDomainEventChannelName(ctx.destinations[i].channel);
|
|
839
|
-
pipeline.publish(key, data);
|
|
840
|
-
if (typeof ctx.destinations[i].channel === 'function') {
|
|
841
|
-
key = keyPrefix + buildDomainEventChannelName(ctx.destinations[i].channel, ctx.content.data.id);
|
|
842
|
-
pipeline.publish(key, data);
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
await pipeline.exec();
|
|
846
|
-
}
|
|
847
|
-
constructor(client){
|
|
848
|
-
this.driver = client;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
class DomainEventSocketPublisher {
|
|
853
|
-
async publish(ctx) {
|
|
854
|
-
ctx.content = transformDomainEventData(ctx.content);
|
|
855
|
-
for(let i = 0; i < ctx.destinations.length; i++){
|
|
856
|
-
let emitter;
|
|
857
|
-
if (ctx.destinations[i].namespace) {
|
|
858
|
-
emitter = this.driver.of(ctx.destinations[i].namespace);
|
|
859
|
-
} else {
|
|
860
|
-
emitter = this.driver;
|
|
861
|
-
}
|
|
862
|
-
let roomName = buildDomainEventChannelName(ctx.destinations[i].channel);
|
|
863
|
-
const fullEventName = coreRealtimeKit.buildEventFullName(ctx.content.type, ctx.content.event);
|
|
864
|
-
emitter.in(roomName).emit(fullEventName, {
|
|
865
|
-
...ctx.content,
|
|
866
|
-
meta: {
|
|
867
|
-
roomName
|
|
868
|
-
}
|
|
869
|
-
});
|
|
870
|
-
if (typeof ctx.destinations[i].channel === 'function') {
|
|
871
|
-
roomName = buildDomainEventChannelName(ctx.destinations[i].channel, ctx.content.data.id);
|
|
872
|
-
emitter.in(roomName).emit(fullEventName, {
|
|
873
|
-
...ctx.content,
|
|
874
|
-
meta: {
|
|
875
|
-
roomName,
|
|
876
|
-
roomId: ctx.content.data.id
|
|
877
|
-
}
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
constructor(client){
|
|
883
|
-
this.driver = new redisEmitter.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
|
-
}
|
|
907
|
-
|
|
908
|
-
/*
|
|
909
|
-
* Copyright (c) 2022-2024.
|
|
910
|
-
* Author Peter Placzek (tada5hi)
|
|
911
|
-
* For the full copyright and license information,
|
|
912
|
-
* view the LICENSE file that was distributed with this source code.
|
|
913
|
-
*/ function hasOwnProperty(obj, prop) {
|
|
914
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
Object.defineProperty(exports, "RedisClient", {
|
|
918
|
-
enumerable: true,
|
|
919
|
-
get: function () { return redisExtension.Client; }
|
|
920
|
-
});
|
|
921
|
-
Object.defineProperty(exports, "RedisClientOptions", {
|
|
922
|
-
enumerable: true,
|
|
923
|
-
get: function () { return redisExtension.ClientOptions; }
|
|
924
|
-
});
|
|
925
|
-
Object.defineProperty(exports, "RedisJsonAdapter", {
|
|
926
|
-
enumerable: true,
|
|
927
|
-
get: function () { return redisExtension.JsonAdapter; }
|
|
928
|
-
});
|
|
929
|
-
Object.defineProperty(exports, "RedisWatcher", {
|
|
930
|
-
enumerable: true,
|
|
931
|
-
get: function () { return redisExtension.Watcher; }
|
|
932
|
-
});
|
|
933
|
-
Object.defineProperty(exports, "buildRedisKeyPath", {
|
|
934
|
-
enumerable: true,
|
|
935
|
-
get: function () { return redisExtension.buildKeyPath; }
|
|
936
|
-
});
|
|
937
|
-
Object.defineProperty(exports, "createRedisClient", {
|
|
938
|
-
enumerable: true,
|
|
939
|
-
get: function () { return redisExtension.createClient; }
|
|
940
|
-
});
|
|
941
|
-
Object.defineProperty(exports, "escapeRedisKey", {
|
|
942
|
-
enumerable: true,
|
|
943
|
-
get: function () { return redisExtension.escapeKey; }
|
|
944
|
-
});
|
|
945
|
-
Object.defineProperty(exports, "parseRedisKeyPath", {
|
|
946
|
-
enumerable: true,
|
|
947
|
-
get: function () { return redisExtension.parseKeyPath; }
|
|
948
|
-
});
|
|
949
|
-
Object.defineProperty(exports, "Logger", {
|
|
950
|
-
enumerable: true,
|
|
951
|
-
get: function () { return winston.Logger; }
|
|
952
|
-
});
|
|
953
|
-
Object.defineProperty(exports, "VaultClient", {
|
|
954
|
-
enumerable: true,
|
|
955
|
-
get: function () { return vault.VaultClient; }
|
|
956
|
-
});
|
|
957
|
-
Object.defineProperty(exports, "createVaultClient", {
|
|
958
|
-
enumerable: true,
|
|
959
|
-
get: function () { return vault.createClient; }
|
|
960
|
-
});
|
|
961
|
-
exports.Cache = Cache;
|
|
962
|
-
exports.CryptoAsymmetricAlgorithm = CryptoAsymmetricAlgorithm;
|
|
963
|
-
exports.CryptoKeyContainer = CryptoKeyContainer;
|
|
964
|
-
exports.DomainEventPublisher = DomainEventPublisher;
|
|
965
|
-
exports.DomainEventRedisPublisher = DomainEventRedisPublisher;
|
|
966
|
-
exports.DomainEventSocketPublisher = DomainEventSocketPublisher;
|
|
967
|
-
exports.MemoryCacheAdapter = MemoryCacheAdapter;
|
|
968
|
-
exports.RedisCacheAdapter = RedisCacheAdapter;
|
|
969
|
-
exports.SymmetricAlgorithm = SymmetricAlgorithm;
|
|
970
|
-
exports.buildCacheKey = buildCacheKey;
|
|
971
|
-
exports.compare = compare;
|
|
972
|
-
exports.createAsymmetricKeyPair = createAsymmetricKeyPair;
|
|
973
|
-
exports.createCacheAdapter = createCacheAdapter;
|
|
974
|
-
exports.createLogger = createLogger;
|
|
975
|
-
exports.createSymmetricKey = createSymmetricKey;
|
|
976
|
-
exports.decodePemToPKCS8 = decodePemToPKCS8;
|
|
977
|
-
exports.decodePemToSpki = decodePemToSpki;
|
|
978
|
-
exports.encodePKCS8ToPEM = encodePKCS8ToPEM;
|
|
979
|
-
exports.encodeSPKIToPem = encodeSPKIToPem;
|
|
980
|
-
exports.extractTokenHeader = extractTokenHeader;
|
|
981
|
-
exports.extractTokenPayload = extractTokenPayload;
|
|
982
|
-
exports.getKeyUsagesForAlgorithm = getKeyUsagesForAlgorithm;
|
|
983
|
-
exports.getKeyUsagesForAsymmetricAlgorithm = getKeyUsagesForAsymmetricAlgorithm;
|
|
984
|
-
exports.getKeyUsagesForSymmetricAlgorithm = getKeyUsagesForSymmetricAlgorithm;
|
|
985
|
-
exports.hasOwnProperty = hasOwnProperty;
|
|
986
|
-
exports.hash = hash;
|
|
987
|
-
exports.isAsymmetricAlgorithm = isAsymmetricAlgorithm;
|
|
988
|
-
exports.isLoggerUsable = isLoggerUsable;
|
|
989
|
-
exports.isRedisClientUsable = isRedisClientUsable;
|
|
990
|
-
exports.isSymmetricAlgorithm = isSymmetricAlgorithm;
|
|
991
|
-
exports.isVaultClientUsable = isVaultClientUsable;
|
|
992
|
-
exports.normalizeAsymmetricKeyImportOptions = normalizeAsymmetricKeyImportOptions;
|
|
993
|
-
exports.normalizeAsymmetricKeyPairCreateOptions = normalizeAsymmetricKeyPairCreateOptions;
|
|
994
|
-
exports.setLogger = setLogger;
|
|
995
|
-
exports.setLoggerFactory = setLoggerFactory;
|
|
996
|
-
exports.setRedisClient = setRedisClient;
|
|
997
|
-
exports.setRedisFactory = setRedisFactory;
|
|
998
|
-
exports.setVaultFactory = setVaultFactory;
|
|
999
|
-
exports.signToken = signToken;
|
|
1000
|
-
exports.useCache = useCache;
|
|
1001
|
-
exports.useLogger = useLogger;
|
|
1002
|
-
exports.useRedisClient = useRedisClient;
|
|
1003
|
-
exports.useVaultClient = useVaultClient;
|
|
1004
|
-
exports.verifyToken = verifyToken;
|
|
1005
|
-
//# sourceMappingURL=index.cjs.map
|