@@ -2,6 +2,7 @@ import { verifyToken } from "@clerk/backend";
22import { createHash , createHmac } from "crypto" ;
33import { AuthenticatedWebSocket , WebSocketMessage , WebSocketConfig } from "../types" ;
44import { ConnectionManager } from "../middleware/connection-manager" ;
5+ import { logger } from "../../lib/logger" ;
56
67interface AuthenticatedMessage {
78 payload : WebSocketMessage ;
@@ -41,7 +42,10 @@ export class AuthHandler {
4142
4243 // Emergency cleanup if too many nonces (DoS protection)
4344 if ( this . usedNonces . size > this . MAX_NONCES ) {
44- console . warn ( `Nonce storage exceeded limit (${ this . MAX_NONCES } ), clearing all nonces` ) ;
45+ logger . warn ( "Nonce storage exceeded limit, clearing all nonces" , {
46+ type : "websocket_security" ,
47+ maxNonces : this . MAX_NONCES ,
48+ } ) ;
4549 this . usedNonces . clear ( ) ;
4650 }
4751 }
@@ -124,8 +128,11 @@ export class AuthHandler {
124128 console . log ( `User ${ ws . userId } authenticated via WebSocket` ) ;
125129 }
126130 } catch ( error : unknown ) {
127- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
128- console . error ( "WebSocket authentication failed" , { error : errorMessage } ) ;
131+ logger . error (
132+ "WebSocket authentication failed" ,
133+ { type : "websocket_auth_error" } ,
134+ error instanceof Error ? error : new Error ( String ( error ) )
135+ ) ;
129136
130137 const isTokenExpired = ( error as Record < string , unknown > ) ?. reason === "token-expired" ;
131138
@@ -161,21 +168,29 @@ export class AuthHandler {
161168 const MAX_MESSAGE_AGE = 5 * 60 * 1000 ; // 5 minutes
162169 if ( messageAge > MAX_MESSAGE_AGE || messageAge < - 60000 ) {
163170 // -60 seconds tolerance for clock skew
164- console . warn ( "Message rejected: timestamp out of range" ) ;
171+ logger . warn ( "Message rejected: timestamp out of range" , {
172+ type : "websocket_security" ,
173+ messageAge,
174+ maxAge : MAX_MESSAGE_AGE ,
175+ } ) ;
165176 return false ;
166177 }
167178
168179 // 2. Check for replay attack using nonce
169180 const nonceKey = `${ nonce } :${ timestamp } ` ;
170181 if ( this . usedNonces . has ( nonceKey ) ) {
171- console . warn ( "Message rejected: nonce already used (replay attack)" ) ;
182+ logger . warn ( "Message rejected: nonce already used (replay attack)" , {
183+ type : "websocket_security" ,
184+ } ) ;
172185 return false ;
173186 }
174187
175188 try {
176189 // 3. Validate required parameters
177190 if ( ! jwtToken || ! userId ) {
178- console . error ( "Missing JWT token or user ID for signature verification" ) ;
191+ logger . warn ( "Missing JWT token or user ID for signature verification" , {
192+ type : "websocket_security" ,
193+ } ) ;
179194 return false ;
180195 }
181196
@@ -207,9 +222,10 @@ export class AuthHandler {
207222 const isValid = isValidRegenerated || isValidStored ;
208223
209224 if ( ! isValid ) {
210- console . warn ( "Message signature verification failed for user" , userId ) ;
211- } else {
212- console . debug ( "Message signature verified successfully for user" , userId ) ;
225+ logger . warn ( "Message signature verification failed" , {
226+ type : "websocket_security" ,
227+ userId : userId || "unknown" ,
228+ } ) ;
213229 }
214230
215231 if ( isValid ) {
@@ -219,8 +235,11 @@ export class AuthHandler {
219235
220236 return isValid ;
221237 } catch ( error ) {
222- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
223- console . error ( "Error verifying message signature" , { error : errorMessage } ) ;
238+ logger . error (
239+ "Error verifying message signature" ,
240+ { type : "websocket_auth_error" } ,
241+ error instanceof Error ? error : new Error ( String ( error ) )
242+ ) ;
224243 return false ;
225244 }
226245 }
@@ -239,7 +258,9 @@ export class AuthHandler {
239258 if ( this . isAuthenticatedMessage ( rawMessage ) ) {
240259 // This is an authenticated message, verify signature
241260 if ( ! ws . sessionSecret ) {
242- console . warn ( "Authenticated message received but no session secret available" ) ;
261+ logger . warn ( "Authenticated message received but no session secret available" , {
262+ type : "websocket_security" ,
263+ } ) ;
243264 return null ;
244265 }
245266
@@ -250,7 +271,10 @@ export class AuthHandler {
250271 ws . userId
251272 ) ;
252273 if ( ! isValid ) {
253- console . warn ( "Message signature verification failed for user" , ws . userId ) ;
274+ logger . warn ( "Message signature verification failed" , {
275+ type : "websocket_security" ,
276+ userId : ws . userId || "unknown" ,
277+ } ) ;
254278 return null ;
255279 }
256280
0 commit comments