@triophore/falconjs 1.0.0 → 1.0.1
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/FalconAuthPlugin.js +31 -24
- package/package.json +1 -1
package/FalconAuthPlugin.js
CHANGED
|
@@ -63,7 +63,9 @@ class FalconAuth {
|
|
|
63
63
|
socketio: Joi.object({
|
|
64
64
|
enabled: Joi.boolean().default(true),
|
|
65
65
|
timeout: Joi.number().default(5000)
|
|
66
|
-
}).default({ enabled: true, timeout: 5000 })
|
|
66
|
+
}).default({ enabled: true, timeout: 5000 }),
|
|
67
|
+
|
|
68
|
+
default: Joi.string().optional()
|
|
67
69
|
});
|
|
68
70
|
|
|
69
71
|
const { error, value } = schema.validate(options);
|
|
@@ -95,10 +97,15 @@ class FalconAuth {
|
|
|
95
97
|
await this.registerJWKS(server);
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
// Set default strategy
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
// Set default strategy
|
|
101
|
+
if (this.options.default) {
|
|
102
|
+
server.auth.default(this.options.default);
|
|
103
|
+
} else {
|
|
104
|
+
// Auto-set default strategy if only one is configured (and no default specified)
|
|
105
|
+
const strategies = Object.keys(this.options).filter(k => k !== 'socketio' && k !== 'default');
|
|
106
|
+
if (strategies.length === 1) {
|
|
107
|
+
server.auth.default(strategies[0]);
|
|
108
|
+
}
|
|
102
109
|
}
|
|
103
110
|
}
|
|
104
111
|
|
|
@@ -279,15 +286,15 @@ class FalconAuth {
|
|
|
279
286
|
|
|
280
287
|
extractSocketToken(socket) {
|
|
281
288
|
const { token, auth, authorization } = socket.handshake.auth || {};
|
|
282
|
-
|
|
289
|
+
|
|
283
290
|
if (token) return token;
|
|
284
291
|
if (auth) return auth;
|
|
285
|
-
|
|
292
|
+
|
|
286
293
|
const authHeader = socket.handshake.headers.authorization;
|
|
287
294
|
if (authHeader?.startsWith('Bearer ')) {
|
|
288
295
|
return authHeader.substring(7);
|
|
289
296
|
}
|
|
290
|
-
|
|
297
|
+
|
|
291
298
|
return socket.handshake.query.token;
|
|
292
299
|
}
|
|
293
300
|
|
|
@@ -297,14 +304,14 @@ class FalconAuth {
|
|
|
297
304
|
try {
|
|
298
305
|
const cacheKey = `auth:custom:${this.hashToken(token)}`;
|
|
299
306
|
let user = await this.getFromCache(cacheKey);
|
|
300
|
-
|
|
307
|
+
|
|
301
308
|
if (!user) {
|
|
302
309
|
user = await this.options.custom.validate(token, null, this.context);
|
|
303
310
|
if (user) {
|
|
304
311
|
await this.setCache(cacheKey, user, 300);
|
|
305
312
|
}
|
|
306
313
|
}
|
|
307
|
-
|
|
314
|
+
|
|
308
315
|
if (user) return user;
|
|
309
316
|
} catch (error) {
|
|
310
317
|
this.logger.debug('Custom socket auth failed:', error.message);
|
|
@@ -318,21 +325,21 @@ class FalconAuth {
|
|
|
318
325
|
const decoded = jwt.verify(token, this.options.jwt.secret, {
|
|
319
326
|
algorithms: this.options.jwt.algorithms
|
|
320
327
|
});
|
|
321
|
-
|
|
328
|
+
|
|
322
329
|
const cacheKey = `auth:jwt:${decoded.sub || decoded.id}`;
|
|
323
330
|
let user = null;
|
|
324
|
-
|
|
331
|
+
|
|
325
332
|
if (this.options.jwt.cache.enabled) {
|
|
326
333
|
user = await this.getFromCache(cacheKey);
|
|
327
334
|
}
|
|
328
|
-
|
|
335
|
+
|
|
329
336
|
if (!user) {
|
|
330
337
|
user = await this.options.jwt.validate(decoded, null, this.context);
|
|
331
338
|
if (user && this.options.jwt.cache.enabled) {
|
|
332
339
|
await this.setCache(cacheKey, user, this.options.jwt.cache.ttl);
|
|
333
340
|
}
|
|
334
341
|
}
|
|
335
|
-
|
|
342
|
+
|
|
336
343
|
if (user) return user;
|
|
337
344
|
} catch (error) {
|
|
338
345
|
this.logger.debug('JWT socket auth failed:', error.message);
|
|
@@ -348,21 +355,21 @@ class FalconAuth {
|
|
|
348
355
|
issuer: this.options.jwks.issuer,
|
|
349
356
|
audience: this.options.jwks.audience
|
|
350
357
|
});
|
|
351
|
-
|
|
358
|
+
|
|
352
359
|
const cacheKey = `auth:jwks:${payload.sub}`;
|
|
353
360
|
let user = null;
|
|
354
|
-
|
|
361
|
+
|
|
355
362
|
if (this.options.jwks.cache.enabled) {
|
|
356
363
|
user = await this.getFromCache(cacheKey);
|
|
357
364
|
}
|
|
358
|
-
|
|
365
|
+
|
|
359
366
|
if (!user) {
|
|
360
367
|
user = await this.options.jwks.validate(payload, null, this.context);
|
|
361
368
|
if (user && this.options.jwks.cache.enabled) {
|
|
362
369
|
await this.setCache(cacheKey, user, this.options.jwks.cache.ttl);
|
|
363
370
|
}
|
|
364
371
|
}
|
|
365
|
-
|
|
372
|
+
|
|
366
373
|
if (user) return user;
|
|
367
374
|
}
|
|
368
375
|
} catch (error) {
|
|
@@ -378,12 +385,12 @@ class FalconAuth {
|
|
|
378
385
|
if (customExtractor) {
|
|
379
386
|
return customExtractor(request);
|
|
380
387
|
}
|
|
381
|
-
|
|
388
|
+
|
|
382
389
|
const authHeader = request.headers.authorization;
|
|
383
390
|
if (authHeader?.startsWith('Bearer ')) {
|
|
384
391
|
return authHeader.substring(7);
|
|
385
392
|
}
|
|
386
|
-
|
|
393
|
+
|
|
387
394
|
return request.query.token || request.payload?.token;
|
|
388
395
|
}
|
|
389
396
|
|
|
@@ -394,7 +401,7 @@ class FalconAuth {
|
|
|
394
401
|
|
|
395
402
|
async getFromCache(key) {
|
|
396
403
|
if (!this.redis) return null;
|
|
397
|
-
|
|
404
|
+
|
|
398
405
|
try {
|
|
399
406
|
const cached = await this.redis.get(key);
|
|
400
407
|
return cached ? JSON.parse(cached) : null;
|
|
@@ -406,7 +413,7 @@ class FalconAuth {
|
|
|
406
413
|
|
|
407
414
|
async setCache(key, value, ttl) {
|
|
408
415
|
if (!this.redis) return;
|
|
409
|
-
|
|
416
|
+
|
|
410
417
|
try {
|
|
411
418
|
await this.redis.setEx(key, ttl, JSON.stringify(value));
|
|
412
419
|
} catch (error) {
|
|
@@ -461,10 +468,10 @@ const plugin = {
|
|
|
461
468
|
register: async function (server, options) {
|
|
462
469
|
// Get context from server app (passed from Falcon.js)
|
|
463
470
|
const context = server.app.falconContext || {};
|
|
464
|
-
|
|
471
|
+
|
|
465
472
|
const falconAuth = new FalconAuth(options, context);
|
|
466
473
|
await falconAuth.registerHapi(server);
|
|
467
|
-
|
|
474
|
+
|
|
468
475
|
// Expose auth instance for Socket.IO use
|
|
469
476
|
server.app.falconAuth = falconAuth;
|
|
470
477
|
}
|