Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ChannelOption for a server to query the authenticated username. #58

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/NIOSSH/Child Channels/ChildChannelOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ extension SSHChildChannelOptions.Types {

public init() {}
}

/// `UsernameOption` allows users to query the authenticated username of the channel.
public struct UsernameOption: ChannelOption {
public typealias Value = String?

public init() {}
}
jimstudt marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 3 additions & 0 deletions Sources/NIOSSH/Child Channels/SSHChannelMultiplexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ extension SSHChannelMultiplexer {
self.erroredChannels.append(channelID)
}
}

// The username which the server accepted in authorization
var username : String? { (delegate as? NIOSSHHandler)?.username }
jimstudt marked this conversation as resolved.
Show resolved Hide resolved
}

// MARK: Calls from SSH handlers.
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOSSH/Child Channels/SSHChildChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ extension SSHChildChannel: Channel, ChannelCore {
// This force-unwrap is safe: we set type before we call the initializer, so
// users can only get this after this value is set.
return self.type! as! Option.Value
case _ as SSHChildChannelOptions.Types.UsernameOption:
return multiplexer.username as! Option.Value
case _ as ChannelOptions.Types.AutoReadOption:
return self.autoRead as! Option.Value
case _ as ChannelOptions.Types.AllowRemoteHalfClosureOption:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension AcceptsUserAuthMessages {
let result = try self.userAuthStateMachine.receiveUserAuthRequest(message)

if let future = result {
return .possibleFutureMessage(future.map(Self.transform(_:)))
return .possibleFutureMessage(future.map{ Self.transform($0, username: message.username) } )
jimstudt marked this conversation as resolved.
Show resolved Hide resolved
} else {
return .noMessage
}
Expand All @@ -65,10 +65,10 @@ extension AcceptsUserAuthMessages {
}
}

private static func transform(_ result: NIOSSHUserAuthenticationResponseMessage) -> SSHMultiMessage {
private static func transform(_ result: NIOSSHUserAuthenticationResponseMessage, username: String?) -> SSHMultiMessage {
switch result {
case .success:
return SSHMultiMessage(.userAuthSuccess)
return SSHMultiMessage(.userAuthSuccess(username))
case .failure(let message):
return SSHMultiMessage(.userAuthFailure(message))
case .publicKeyOK(let message):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension SendsUserAuthMessages {

mutating func writeUserAuthSuccess(into buffer: inout ByteBuffer) throws {
self.userAuthStateMachine.sendUserAuthSuccess()
try self.serializer.serialize(message: .userAuthSuccess, to: &buffer)
try self.serializer.serialize(message: .userAuthSuccess(nil), to: &buffer)
}

mutating func writeUserAuthFailure(_ message: SSHMessage.UserAuthFailureMessage, into buffer: inout ByteBuffer) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ struct SSHConnectionStateMachine {

/// The state of this state machine.
private var state: State


/// The username which authenticated to the server, if that has happened
private(set) var username: String? = nil
jimstudt marked this conversation as resolved.
Show resolved Hide resolved

private static let defaultTransportProtectionSchemes: [NIOSSHTransportProtection.Type] = [
AES256GCMOpenSSHTransportProtection.self, AES128GCMOpenSSHTransportProtection.self,
]
Expand Down Expand Up @@ -810,7 +813,8 @@ struct SSHConnectionStateMachine {
try state.writeUserAuthRequest(message, into: &buffer)
self.state = .userAuthentication(state)

case .userAuthSuccess:
case .userAuthSuccess(let username):
self.username = username
try state.writeUserAuthSuccess(into: &buffer)
// Ok we're good to go!
self.state = .active(ActiveState(state))
Expand Down
3 changes: 3 additions & 0 deletions Sources/NIOSSH/NIOSSHHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public final class NIOSSHHandler {

private var pendingGlobalRequestResponses: CircularBuffer<PendingGlobalRequestResponse?>

// The authenticated username, if there was one.
var username: String? { stateMachine.username }

public init(role: SSHConnectionRole, allocator: ByteBufferAllocator, inboundChildChannelInitializer: ((Channel, SSHChannelType) -> EventLoopFuture<Void>)?) {
self.stateMachine = SSHConnectionStateMachine(role: role)
self.pendingWrite = false
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOSSH/SSHMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum SSHMessage: Equatable {
case newKeys
case userAuthRequest(UserAuthRequestMessage)
case userAuthFailure(UserAuthFailureMessage)
case userAuthSuccess
case userAuthSuccess(String?)
jimstudt marked this conversation as resolved.
Show resolved Hide resolved
case userAuthPKOK(UserAuthPKOKMessage)
case globalRequest(GlobalRequestMessage)
case requestSuccess(RequestSuccessMessage)
Expand Down Expand Up @@ -418,7 +418,7 @@ extension ByteBuffer {
}
return .userAuthFailure(message)
case SSHMessage.UserAuthSuccessMessage.id:
return .userAuthSuccess
return .userAuthSuccess(nil)
case SSHMessage.UserAuthPKOKMessage.id:
guard let message = try self.readUserAuthPKOKMessage() else {
return nil
Expand Down
2 changes: 1 addition & 1 deletion Tests/NIOSSHTests/SSHMessagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ final class SSHMessagesTests: XCTestCase {

func testUserAuthSuccess() throws {
var buffer = ByteBufferAllocator().buffer(capacity: 100)
let message = SSHMessage.userAuthSuccess
let message = SSHMessage.userAuthSuccess(nil)

buffer.writeSSHMessage(message)
XCTAssertEqual(try buffer.readSSHMessage(), message)
Expand Down