@@ -4,15 +4,24 @@ import Crypto
4
4
5
5
/// Represents an authentication method.
6
6
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
9
13
10
14
internal init (
11
15
username: String ,
12
16
offer: NIOSSHUserAuthenticationOffer . Offer
13
17
) {
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)
16
25
}
17
26
18
27
/// Creates a password based authentication method.
@@ -63,30 +72,39 @@ public struct SSHAuthenticationMethod: NIOSSHClientUserAuthenticationDelegate {
63
72
return SSHAuthenticationMethod ( username: username, offer: . privateKey( . init( privateKey: . init( p521Key: privateKey) ) ) )
64
73
}
65
74
75
+ public static func custom( _ auth: NIOSSHClientUserAuthenticationDelegate ) -> SSHAuthenticationMethod {
76
+ return SSHAuthenticationMethod ( custom: auth)
77
+ }
78
+
66
79
public func nextAuthenticationType(
67
80
availableMethods: NIOSSHAvailableUserAuthenticationMethods ,
68
81
nextChallengePromise: EventLoopPromise < NIOSSHUserAuthenticationOffer ? >
69
82
) {
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
+ ( )
85
103
}
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)
88
108
}
89
-
90
- nextChallengePromise. succeed ( NIOSSHUserAuthenticationOffer ( username: username, serviceName: " " , offer: offer) )
91
109
}
92
110
}
0 commit comments