Skip to content

Commit 023f8c1

Browse files
committed
feat: connect event + shortcuts for UserAuthRequest fields
1 parent 152ce3c commit 023f8c1

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

src/ServerClient.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE
129129
hasReceivedNewKeys: boolean = false
130130
hasSentNewKeys: boolean = false
131131
hasAuthenticated: boolean = false
132+
credentials: UserAuthRequest | undefined
132133

133134
localChannelIndex = 0
134135
channels = new Map<number, Channel>()
@@ -368,6 +369,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE
368369

369370
async handleAuthentication() {
370371
let allowLogin = false
372+
let authRequest: Packet
371373
authentication: {
372374
const userAuthFailure = new UserAuthFailure({
373375
auth_methods: [
@@ -382,7 +384,7 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE
382384
// and the server should be able to handle them. This current implementation
383385
// does not respect that and waits sequencially.
384386
this.debug("Waiting for authentication request...")
385-
const [authRequest] = (await this.waitEvent("packet")) as [Packet]
387+
;[authRequest] = (await this.waitEvent("packet")) as [Packet]
386388
assert(authRequest instanceof UserAuthRequest, "Invalid packet type")
387389

388390
this.debug(`Received authentication request:`, authRequest)
@@ -496,6 +498,11 @@ export default class ServerClient extends (EventEmitter as new () => TypedEventE
496498
}
497499
}
498500

501+
// redo the assert for type checking, otherwise it should
502+
// never throw.
503+
assert(authRequest instanceof UserAuthRequest, "Invalid packet type")
504+
this.credentials = authRequest
505+
499506
if (allowLogin) {
500507
this.sendPacket(new UserAuthSuccess({}))
501508
this.hasAuthenticated = true

src/auth/none.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import UserAuthFailure from "../packets/UserAuthFailure.js"
99
export interface NoneAuthMethodData {}
1010
export default class NoneAuthMethod implements AuthMethod {
1111
static method_name = SSHAuthenticationMethods.None
12+
get method_name() {
13+
return NoneAuthMethod.method_name
14+
}
1215

1316
data: NoneAuthMethodData
1417
constructor(data: NoneAuthMethodData) {

src/auth/password.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface PasswordAuthMethodData {
1313
}
1414
export default class PasswordAuthMethod implements AuthMethod {
1515
static method_name = SSHAuthenticationMethods.Password
16+
get method_name() {
17+
return PasswordAuthMethod.method_name
18+
}
1619

1720
data: PasswordAuthMethodData
1821
constructor(data: PasswordAuthMethodData) {

src/auth/publickey.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export interface PublicKeyAuthMethodData {
1717
}
1818
export default class PublicKeyAuthMethod implements AuthMethod {
1919
static method_name = SSHAuthenticationMethods.PublicKey
20+
get method_name() {
21+
return PublicKeyAuthMethod.method_name
22+
}
2023

2124
data: PublicKeyAuthMethodData
2225
constructor(data: PublicKeyAuthMethodData) {

src/index_server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ server.listen(3022, () => {
2020

2121
server.on("connection", (client) => {
2222
client.on("error", console.error)
23+
24+
client.on("connect", () => {
25+
console.log(
26+
`User ${client.credentials!.username} logged in with ${client.credentials!.method_name}`,
27+
)
28+
})
2329
})
2430

2531
const allowedUser = "manaf"

src/packets/UserAuthRequest.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ export default class UserAuthRequest implements Packet {
3131
this.data = data
3232
}
3333

34+
get username() {
35+
return this.data.username
36+
}
37+
get publicKey() {
38+
return this.data.method instanceof PublicKeyAuthMethod
39+
? this.data.method.data.publicKey
40+
: undefined
41+
}
42+
get password() {
43+
return this.data.method instanceof PasswordAuthMethod
44+
? this.data.method.data.password
45+
: undefined
46+
}
47+
get method_name() {
48+
return this.data.method.method_name
49+
}
50+
3451
serialize(): Buffer {
3552
const buffers = []
3653

@@ -88,6 +105,7 @@ export default class UserAuthRequest implements Packet {
88105

89106
export abstract class AuthMethod {
90107
static method_name: SSHAuthenticationMethods
108+
method_name: SSHAuthenticationMethods
91109

92110
// eslint-disable-next-line @typescript-eslint/no-unused-vars
93111
constructor(data: any) {

0 commit comments

Comments
 (0)