Skip to content

Commit

Permalink
feat: connect event + shortcuts for UserAuthRequest fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Manaf941 committed Nov 21, 2024
1 parent 152ce3c commit 023f8c1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, Channel>()
Expand Down Expand Up @@ -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: [
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/auth/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/auth/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/auth/publickey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/index_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions src/packets/UserAuthRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 023f8c1

Please sign in to comment.