A Node.js module providing secure authentication with end-to-end encryption using WebSocket connections and cryptographic functions.
- 🔐 End-to-End Encryption: Uses ECDH key exchange and AES-GCM encryption
- 🌐 WebSocket Communication: Secure real-time communication with authentication server
- 🔑 Ephemeral Key Generation: Fresh key pairs for each session
- 📝 Comprehensive Logging: Built-in logging for debugging and monitoring
- 🛠️ Modular Design: Use as a complete class or individual functions
npm install wsconst { SecureAuth } = require('./auth');
async function login() {
// Use default localhost server
const auth = new SecureAuth();
// Or specify a custom auth server
const auth = new SecureAuth('https://auth.hitboxgames.online');
try {
// Connect for login
await auth.connectForLogin(
(message) => {
if (message.type === 'success') {
console.log('Login successful!');
}
},
(error) => console.error('Error:', error),
() => console.log('Connection closed')
);
// Submit login credentials
await auth.submitLogin('username', 'password');
// Or connect for registration
await auth.connectForRegister(
(message) => {
if (message.type === 'success') {
console.log('Registration successful!');
}
},
(error) => console.error('Error:', error),
() => console.log('Connection closed')
);
// Submit registration credentials
await auth.submitRegister('newuser', 'password', 'user@example.com');
} catch (error) {
console.error('Authentication failed:', error);
} finally {
auth.close();
}
}const { SecureAuth } = require('./auth');
async function register() {
const auth = new SecureAuth('https://auth.hitboxgames.online');
try {
await auth.connectForRegister(
(message) => {
if (message.type === 'success') {
console.log('Registration successful!');
} else if (message.type === 'error') {
console.log('Registration failed:', message.message);
}
},
(error) => console.error('Error:', error),
() => console.log('Connection closed')
);
// Submit registration with email
await auth.submitRegister('newuser', 'password123', 'user@example.com');
// Or submit registration without email
await auth.submitRegister('anotheruser', 'password123');
} catch (error) {
console.error('Registration failed:', error);
} finally {
auth.close();
}
}const { encrypt, decrypt } = require('./auth');
const crypto = require('crypto');
// Generate a UUID
const uuid = crypto.randomUUID();
// Encrypt/decrypt data
const key = await crypto.subtle.generateKey(
{ name: "AES-GCM" },
false,
["encrypt", "decrypt"]
);
const encrypted = await encrypt('secret data', key);
const decrypted = await decrypt(encrypted, key);// Use default localhost server
const auth = new SecureAuth();
// Or specify a custom auth server
const auth = new SecureAuth('https://auth.hitboxgames.online');Parameters:
authServer(string, optional): The authentication server URL. Defaults to'http://localhost:3001'.
Establishes a secure WebSocket connection to the authentication server (legacy method, uses login endpoint).
uuid(string): The UUID for this sessiononMessage(message): Callback for received messagesonError(error): Callback for connection errorsonClose(): Callback when connection closes
Establishes a secure WebSocket connection to the login server endpoint.
uuid(string): The UUID for this sessiononMessage(message): Callback for received messagesonError(error): Callback for connection errorsonClose(): Callback when connection closes
Establishes a secure WebSocket connection to the register server endpoint.
uuid(string): The UUID for this sessiononMessage(message): Callback for received messagesonError(error): Callback for connection errorsonClose(): Callback when connection closes
Submits login credentials securely.
uuid(string): The UUID for this sessionusername(string): The username for loginpassword(string): The password for login
Submits registration credentials securely.
uuid(string): The UUID for this sessionusername(string): The username for registrationpassword(string): The password for registrationemail(string, optional): The email address for registration
Closes the WebSocket connection.
Returns true if connected to the server.
Returns array of log messages.
Clears the log array.
Generates an ECDH key pair for secure key exchange.
Generates an ECDH key pair for secure key exchange.
Exports a public key to PEM format.
Imports a public key from base64 format.
Derives a shared secret using ECDH.
Encrypts data using AES-GCM.
Decrypts data using AES-GCM.
The module can connect to any authentication server with the following endpoints:
GET /login/init?uuid={uuid}- Returns WebSocket URL for loginGET /register/init?uuid={uuid}- Returns WebSocket URL for registration- WebSocket endpoints for secure communication
Default Configuration:
- Default server:
http://localhost:3001 - Can be overridden by passing a custom URL to the constructor
Example:
// Use default localhost server
const auth = new SecureAuth();
// Use custom auth server
const auth = new SecureAuth('https://auth.hitboxgames.online');- ECDH Key Exchange: Secure key agreement protocol
- AES-GCM Encryption: Authenticated encryption for data protection
- Ephemeral Keys: Fresh keys for each session
- UUID-based Sessions: Unique session identifiers
- End-to-End Encryption: Server cannot decrypt user data
The module provides comprehensive error handling:
try {
await auth.connectWebSocket();
} catch (error) {
console.error('Connection failed:', error.message);
// Handle specific error types
if (error.message.includes('wsUrl')) {
// Server not available
}
}Built-in logging system for debugging:
const logs = auth.getLogs();
console.log('Authentication logs:', logs);See auth-example.js for complete usage examples including:
- Complete login flow
- Registration flow
- Individual crypto function usage
- Standalone function examples
- Node.js >= 16.0.0
wspackage for WebSocket support- Authentication server with the required endpoints (defaults to localhost:3001)
MIT License - see LICENSE file for details.