chore(websocket): [WIP] Making JWT algos configurable (#25521)

This commit is contained in:
Craig Rueda 2023-10-23 11:28:41 -07:00 committed by GitHub
parent e4173d90c8
commit 861ee8b3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -16,6 +16,7 @@
"ssl": false "ssl": false
}, },
"redisStreamPrefix": "async-events-", "redisStreamPrefix": "async-events-",
"jwtAlgorithms": ["HS256"],
"jwtSecret": "CHANGE-ME", "jwtSecret": "CHANGE-ME",
"jwtCookieName": "async-token" "jwtCookieName": "async-token"
} }

View File

@ -36,6 +36,7 @@ type ConfigType = {
redisStreamPrefix: string; redisStreamPrefix: string;
redisStreamReadCount: number; redisStreamReadCount: number;
redisStreamReadBlockMs: number; redisStreamReadBlockMs: number;
jwtAlgorithms: string[];
jwtSecret: string; jwtSecret: string;
jwtCookieName: string; jwtCookieName: string;
jwtChannelIdKey: string; jwtChannelIdKey: string;
@ -53,6 +54,7 @@ function defaultConfig(): ConfigType {
redisStreamPrefix: 'async-events-', redisStreamPrefix: 'async-events-',
redisStreamReadCount: 100, redisStreamReadCount: 100,
redisStreamReadBlockMs: 5000, redisStreamReadBlockMs: 5000,
jwtAlgorithms: ['HS256'],
jwtSecret: '', jwtSecret: '',
jwtCookieName: 'async-token', jwtCookieName: 'async-token',
jwtChannelIdKey: 'channel', jwtChannelIdKey: 'channel',

View File

@ -20,7 +20,7 @@ import * as http from 'http';
import * as net from 'net'; import * as net from 'net';
import WebSocket from 'ws'; import WebSocket from 'ws';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import jwt from 'jsonwebtoken'; import jwt, { Algorithm } from 'jsonwebtoken';
import cookie from 'cookie'; import cookie from 'cookie';
import Redis from 'ioredis'; import Redis from 'ioredis';
import StatsD from 'hot-shots'; import StatsD from 'hot-shots';
@ -261,7 +261,10 @@ const readChannelId = (request: http.IncomingMessage): string => {
const token = cookies[opts.jwtCookieName]; const token = cookies[opts.jwtCookieName];
if (!token) throw new Error('JWT not present'); if (!token) throw new Error('JWT not present');
const jwtPayload = jwt.verify(token, opts.jwtSecret) as JwtPayload; const jwtPayload = jwt.verify(token, opts.jwtSecret, {
algorithms: opts.jwtAlgorithms as Algorithm[],
complete: false,
}) as JwtPayload;
const channelId = jwtPayload[opts.jwtChannelIdKey]; const channelId = jwtPayload[opts.jwtChannelIdKey];
if (!channelId) throw new Error('Channel ID not present in JWT'); if (!channelId) throw new Error('Channel ID not present in JWT');