Skip to content

Commit f8b7f2d

Browse files
authored
Merge pull request #26 from orlandos-nl/feature/jo-command-stream
Don't stop collecting on stderr chunks. Provide an AsyncThrowingStream for executeCommand data
2 parents 2e6a7fa + 9e40248 commit f8b7f2d

File tree

3 files changed

+248
-56
lines changed

3 files changed

+248
-56
lines changed

Sources/Citadel/Client.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NIO
2+
import Logging
23
import NIOSSH
34

45
public struct SSHAlgorithms {
@@ -70,10 +71,11 @@ public final class SSHClient {
7071
private let algorithms: SSHAlgorithms
7172
private let protocolOptions: Set<SSHProtocolOption>
7273
private var onDisconnect: (@Sendable () -> ())?
74+
public let logger = Logger(label: "nl.orlandos.citadel.client")
7375
public var isConnected: Bool {
7476
session.channel.isActive
7577
}
76-
78+
7779
/// The event loop that this SSH connection is running on.
7880
public var eventLoop: EventLoop {
7981
session.channel.eventLoop

Sources/Citadel/SSHAuthenticationMethod.swift

+41-23
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ import Crypto
44

55
/// Represents an authentication method.
66
public struct SSHAuthenticationMethod: NIOSSHClientUserAuthenticationDelegate {
7-
private let username: String
8-
private let offer: NIOSSHUserAuthenticationOffer.Offer
7+
private enum Implementation {
8+
case custom(NIOSSHClientUserAuthenticationDelegate)
9+
case user(String, offer: NIOSSHUserAuthenticationOffer.Offer)
10+
}
11+
12+
private let implementation: Implementation
913

1014
internal init(
1115
username: String,
1216
offer: NIOSSHUserAuthenticationOffer.Offer
1317
) {
14-
self.username = username
15-
self.offer = offer
18+
self.implementation = .user(username, offer: offer)
19+
}
20+
21+
internal init(
22+
custom: NIOSSHClientUserAuthenticationDelegate
23+
) {
24+
self.implementation = .custom(custom)
1625
}
1726

1827
/// Creates a password based authentication method.
@@ -63,30 +72,39 @@ public struct SSHAuthenticationMethod: NIOSSHClientUserAuthenticationDelegate {
6372
return SSHAuthenticationMethod(username: username, offer: .privateKey(.init(privateKey: .init(p521Key: privateKey))))
6473
}
6574

75+
public static func custom(_ auth: NIOSSHClientUserAuthenticationDelegate) -> SSHAuthenticationMethod {
76+
return SSHAuthenticationMethod(custom: auth)
77+
}
78+
6679
public func nextAuthenticationType(
6780
availableMethods: NIOSSHAvailableUserAuthenticationMethods,
6881
nextChallengePromise: EventLoopPromise<NIOSSHUserAuthenticationOffer?>
6982
) {
70-
switch offer {
71-
case .password:
72-
guard availableMethods.contains(.password) else {
73-
nextChallengePromise.fail(SSHClientError.unsupportedPasswordAuthentication)
74-
return
75-
}
76-
case .hostBased:
77-
guard availableMethods.contains(.hostBased) else {
78-
nextChallengePromise.fail(SSHClientError.unsupportedHostBasedAuthentication)
79-
return
80-
}
81-
case .privateKey:
82-
guard availableMethods.contains(.publicKey) else {
83-
nextChallengePromise.fail(SSHClientError.unsupportedPrivateKeyAuthentication)
84-
return
83+
switch implementation {
84+
case .user(let username, offer: let offer):
85+
switch offer {
86+
case .password:
87+
guard availableMethods.contains(.password) else {
88+
nextChallengePromise.fail(SSHClientError.unsupportedPasswordAuthentication)
89+
return
90+
}
91+
case .hostBased:
92+
guard availableMethods.contains(.hostBased) else {
93+
nextChallengePromise.fail(SSHClientError.unsupportedHostBasedAuthentication)
94+
return
95+
}
96+
case .privateKey:
97+
guard availableMethods.contains(.publicKey) else {
98+
nextChallengePromise.fail(SSHClientError.unsupportedPrivateKeyAuthentication)
99+
return
100+
}
101+
case .none:
102+
()
85103
}
86-
case .none:
87-
()
104+
105+
nextChallengePromise.succeed(NIOSSHUserAuthenticationOffer(username: username, serviceName: "", offer: offer))
106+
case .custom(let implementation):
107+
implementation.nextAuthenticationType(availableMethods: availableMethods, nextChallengePromise: nextChallengePromise)
88108
}
89-
90-
nextChallengePromise.succeed(NIOSSHUserAuthenticationOffer(username: username, serviceName: "", offer: offer))
91109
}
92110
}

0 commit comments

Comments
 (0)