From 3a5f742aa4d0fdc5acfbc7a1257637a092c04210 Mon Sep 17 00:00:00 2001 From: Chinmoy Date: Tue, 12 Sep 2023 14:17:16 +0530 Subject: [PATCH] Provide config to switch between in-memory and redis cache. (#35) --- src/app.module.ts | 6 +++--- src/config/config.ts | 1 + src/guard/ws.guard.ts | 13 +++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 073bb05..2f5d012 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -61,9 +61,9 @@ import * as redisStore from 'cache-manager-redis-store'; }), TerminusModule, CacheModule.register({ - store: redisStore, - host: process.env.TRANSPORT_SOCKET_CACHE_HOST, - port: process.env.TRANSPORT_SOCKET_CACHE_PORT, + store: config().app.cacheStore == 'redis' ? redisStore : 'memory', + host: config().app.cacheStore == 'redis' ? config().app.redisHost : '', + port: config().app.cacheStore == 'redis' ? config().app.redisPort : '', max: 200000, ttl: 86400, }), diff --git a/src/config/config.ts b/src/config/config.ts index fb1b5ae..1abf7f9 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -2,6 +2,7 @@ export const config = () => ({ app: { redisPort: process.env.REDIS_PORT || 6380, redisHost: process.env.REDIS_HOST || '0.0.0.0', + cacheStore: process.env.CACHE_STORE || 'memory', port: process.env.SERVER_PORT || 3005, bot_session_event: process.env.SKT_SESSION_EVT || 'session', socket_timeout: process.env.SKT_TIMEOUT || 60000, diff --git a/src/guard/ws.guard.ts b/src/guard/ws.guard.ts index 0db087e..e351ae4 100644 --- a/src/guard/ws.guard.ts +++ b/src/guard/ws.guard.ts @@ -5,15 +5,15 @@ import * as jwt from 'jsonwebtoken'; // eslint-disable-next-line @typescript-eslint/no-var-requires const jwksClient = require('jwks-rsa'); -const client = jwksClient({ - jwksUri: process.env.TRANSPORT_SOCKET_JWT_AUTH_URL, - requestHeaders: {}, // Optional - timeout: 30000, // Defaults to 30s -}); - const getKey = (header, callback) => { + let client = jwksClient({ + jwksUri: process.env.TRANSPORT_SOCKET_JWT_AUTH_URL, + requestHeaders: {}, // Optional + timeout: 30000, // Defaults to 30s + }); client.getSigningKey(header.kid, function (err, key) { if (err || !key || !(key.publicKey || key.rsaPublicKey)) { + console.error('User could not be resolved!'); callback(err, null); return; } @@ -37,6 +37,7 @@ export class WsGuard implements CanActivate { return new Promise(function (resolve, reject) { jwt.verify(bearerToken, getKey, function (err, decoded) { if (err || !decoded || !decoded['sub'] || !decoded['preferred_username']) { + console.error('User could not be resolved!'); resolve('User could not be resolved!'); return; }