From 023f8c15aedc33e60b9836c2dcc42aa5e34a50b5 Mon Sep 17 00:00:00 2001 From: Manaf941 Date: Thu, 21 Nov 2024 16:14:58 +0100 Subject: [PATCH] feat: connect event + shortcuts for UserAuthRequest fields --- src/ServerClient.ts | 9 ++++++++- src/auth/none.ts | 3 +++ src/auth/password.ts | 3 +++ src/auth/publickey.ts | 3 +++ src/index_server.ts | 6 ++++++ src/packets/UserAuthRequest.ts | 18 ++++++++++++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/ServerClient.ts b/src/ServerClient.ts index d01f5ca..047ef2f 100644 --- a/src/ServerClient.ts +++ b/src/ServerClient.ts @@ -129,6 +129,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE hasReceivedNewKeys: boolean = false hasSentNewKeys: boolean = false hasAuthenticated: boolean = false + credentials: UserAuthRequest | undefined localChannelIndex = 0 channels = new Map() @@ -368,6 +369,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE async handleAuthentication() { let allowLogin = false + let authRequest: Packet authentication: { const userAuthFailure = new UserAuthFailure({ auth_methods: [ @@ -382,7 +384,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE // and the server should be able to handle them. This current implementation // does not respect that and waits sequencially. this.debug("Waiting for authentication request...") - const [authRequest] = (await this.waitEvent("packet")) as [Packet] + ;[authRequest] = (await this.waitEvent("packet")) as [Packet] assert(authRequest instanceof UserAuthRequest, "Invalid packet type") this.debug(`Received authentication request:`, authRequest) @@ -496,6 +498,11 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE } } + // redo the assert for type checking, otherwise it should + // never throw. + assert(authRequest instanceof UserAuthRequest, "Invalid packet type") + this.credentials = authRequest + if (allowLogin) { this.sendPacket(new UserAuthSuccess({})) this.hasAuthenticated = true diff --git a/src/auth/none.ts b/src/auth/none.ts index c050f1c..57d6ea2 100644 --- a/src/auth/none.ts +++ b/src/auth/none.ts @@ -9,6 +9,9 @@ import UserAuthFailure from "../packets/UserAuthFailure.js" export interface NoneAuthMethodData {} export default class NoneAuthMethod implements AuthMethod { static method_name = SSHAuthenticationMethods.None + get method_name() { + return NoneAuthMethod.method_name + } data: NoneAuthMethodData constructor(data: NoneAuthMethodData) { diff --git a/src/auth/password.ts b/src/auth/password.ts index 7c0152b..5fde227 100644 --- a/src/auth/password.ts +++ b/src/auth/password.ts @@ -13,6 +13,9 @@ export interface PasswordAuthMethodData { } export default class PasswordAuthMethod implements AuthMethod { static method_name = SSHAuthenticationMethods.Password + get method_name() { + return PasswordAuthMethod.method_name + } data: PasswordAuthMethodData constructor(data: PasswordAuthMethodData) { diff --git a/src/auth/publickey.ts b/src/auth/publickey.ts index 0bc7cdc..ef6c363 100644 --- a/src/auth/publickey.ts +++ b/src/auth/publickey.ts @@ -17,6 +17,9 @@ export interface PublicKeyAuthMethodData { } export default class PublicKeyAuthMethod implements AuthMethod { static method_name = SSHAuthenticationMethods.PublicKey + get method_name() { + return PublicKeyAuthMethod.method_name + } data: PublicKeyAuthMethodData constructor(data: PublicKeyAuthMethodData) { diff --git a/src/index_server.ts b/src/index_server.ts index 7b415aa..8105572 100644 --- a/src/index_server.ts +++ b/src/index_server.ts @@ -20,6 +20,12 @@ server.listen(3022, () => { server.on("connection", (client) => { client.on("error", console.error) + + client.on("connect", () => { + console.log( + `User ${client.credentials!.username} logged in with ${client.credentials!.method_name}`, + ) + }) }) const allowedUser = "manaf" diff --git a/src/packets/UserAuthRequest.ts b/src/packets/UserAuthRequest.ts index 7095764..5d64211 100644 --- a/src/packets/UserAuthRequest.ts +++ b/src/packets/UserAuthRequest.ts @@ -31,6 +31,23 @@ export default class UserAuthRequest implements Packet { this.data = data } + get username() { + return this.data.username + } + get publicKey() { + return this.data.method instanceof PublicKeyAuthMethod + ? this.data.method.data.publicKey + : undefined + } + get password() { + return this.data.method instanceof PasswordAuthMethod + ? this.data.method.data.password + : undefined + } + get method_name() { + return this.data.method.method_name + } + serialize(): Buffer { const buffers = [] @@ -88,6 +105,7 @@ export default class UserAuthRequest implements Packet { export abstract class AuthMethod { static method_name: SSHAuthenticationMethods + method_name: SSHAuthenticationMethods // eslint-disable-next-line @typescript-eslint/no-unused-vars constructor(data: any) {