refactor: Improve socket authentication error handling and message

This commit is contained in:
Bill Church 2024-07-16 17:39:30 +00:00
parent 1ecf19c5df
commit d9931334de
No known key found for this signature in database

View file

@ -59,11 +59,11 @@ function handleConnection(socket, config) {
function handleAuthentication(socket, creds, config) {
console.log(`SOCKET AUTHENTICATE: ${socket.id}`)
if (isValidCredentials(creds)) {
console.log(`SOCKET AUTHENTICATE SUCCESS: ${socket.id}`)
console.log(`SOCKET CREDENTIALS VALID: ${socket.id}`)
initializeConnection(socket, creds, config)
} else {
console.log(`SOCKET AUTHENTICATE FAILED: ${socket.id}`)
socket.emit('auth_result', { success: false, message: 'Invalid credentials' })
console.log(`SOCKET CREDENTIALS INVALID: ${socket.id}`)
socket.emit('auth_result', { success: false, message: 'Invalid credentials format' })
}
}
@ -91,7 +91,11 @@ function handleConnection(socket, config) {
conn.on('error', err => {
console.log(`SSH CONNECTION ERROR: ${socket.id}`, err)
if (err.level === 'client-authentication') {
socket.emit('auth_result', { success: false, message: 'Authentication failed' })
} else {
handleError(socket, 'SSH CONNECTION ERROR', err)
}
})
conn.connect(getSSHConfig(creds, config))
@ -293,8 +297,12 @@ function handleConnection(socket, config) {
* @returns {boolean} Whether the credentials are valid
*/
function isValidCredentials(credentials) {
// Implement your credential validation logic here
return credentials && credentials.username && credentials.password
// Basic format validation
return credentials &&
typeof credentials.username === 'string' &&
typeof credentials.password === 'string' &&
typeof credentials.host === 'string' &&
typeof credentials.port === 'number'
}
/**