Skip to content

Commit 03644f0

Browse files
committed
Change AuthenticationMethod.AllowedAuthentications from IList<string> to string[].
Remove usage of Linq.
1 parent 0e083cf commit 03644f0

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/Renci.SshNet/AuthenticationMethod.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Renci.SshNet.Common;
2+
using System;
23
using System.Collections.Generic;
34

45
namespace Renci.SshNet
@@ -24,7 +25,7 @@ public abstract class AuthenticationMethod : IAuthenticationMethod
2425
/// <summary>
2526
/// Gets list of allowed authentications.
2627
/// </summary>
27-
public IList<string> AllowedAuthentications { get; protected set; }
28+
public string[] AllowedAuthentications { get; protected set; }
2829

2930
/// <summary>
3031
/// Initializes a new instance of the <see cref="AuthenticationMethod"/> class.

src/Renci.SshNet/ClientAuthentication.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Renci.SshNet.Common;
54

65
namespace Renci.SshNet
@@ -48,10 +47,10 @@ public void Authenticate(IConnectionInfoInternal connectionInfo, ISession sessio
4847

4948
private static bool TryAuthenticate(ISession session,
5049
AuthenticationState authenticationState,
51-
ICollection<string> allowedAuthenticationMethods,
50+
string[] allowedAuthenticationMethods,
5251
ref SshAuthenticationException authenticationException)
5352
{
54-
if (allowedAuthenticationMethods.Count == 0)
53+
if (allowedAuthenticationMethods.Length == 0)
5554
{
5655
authenticationException = new SshAuthenticationException("No authentication methods defined on SSH server.");
5756
return false;
@@ -60,10 +59,10 @@ private static bool TryAuthenticate(ISession session,
6059
// we want to try authentication methods in the order in which they were
6160
// passed in the ctor, not the order in which the SSH server returns
6261
// the allowed authentication methods
63-
var matchingAuthenticationMethods = authenticationState.SupportedAuthenticationMethods.Where(a => allowedAuthenticationMethods.Contains(a.Name)).ToArray();
64-
if (matchingAuthenticationMethods.Length == 0)
62+
var matchingAuthenticationMethods = GetAllowedAuthenticationMethodsThatAreSupported(authenticationState, allowedAuthenticationMethods);
63+
if (matchingAuthenticationMethods.Count == 0)
6564
{
66-
authenticationException = new SshAuthenticationException(string.Format("No suitable authentication method found to complete authentication ({0}).", string.Join(",", allowedAuthenticationMethods.ToArray())));
65+
authenticationException = new SshAuthenticationException(string.Format("No suitable authentication method found to complete authentication ({0}).", string.Join(",", allowedAuthenticationMethods)));
6766
return false;
6867
}
6968

@@ -108,11 +107,31 @@ private static bool TryAuthenticate(ISession session,
108107
return false;
109108
}
110109

111-
private static IEnumerable<IAuthenticationMethod> GetOrderedAuthenticationMethods(AuthenticationState authenticationState, IAuthenticationMethod[] matchingAuthenticationMethods)
110+
private static List<IAuthenticationMethod> GetAllowedAuthenticationMethodsThatAreSupported(AuthenticationState authenticationState,
111+
string[] allowedAuthenticationMethods)
112+
{
113+
var result = new List<IAuthenticationMethod>();
114+
115+
foreach (var supportedAuthenticationMethod in authenticationState.SupportedAuthenticationMethods)
116+
{
117+
for (var i = 0; i < allowedAuthenticationMethods.Length; i++)
118+
{
119+
if (allowedAuthenticationMethods[i] == supportedAuthenticationMethod.Name)
120+
{
121+
result.Add(supportedAuthenticationMethod);
122+
break;
123+
}
124+
}
125+
}
126+
127+
return result;
128+
}
129+
130+
private static IEnumerable<IAuthenticationMethod> GetOrderedAuthenticationMethods(AuthenticationState authenticationState, List<IAuthenticationMethod> matchingAuthenticationMethods)
112131
{
113132
var skippedAuthenticationMethods = new List<IAuthenticationMethod>();
114133

115-
for (var i = 0; i < matchingAuthenticationMethods.Length; i++)
134+
for (var i = 0; i < matchingAuthenticationMethods.Count; i++)
116135
{
117136
var authenticationMethod = matchingAuthenticationMethods[i];
118137

@@ -162,7 +181,7 @@ public AuthenticationState(IList<IAuthenticationMethod> supportedAuthenticationM
162181
/// <value>
163182
/// The list of supported authentication methods.
164183
/// </value>
165-
public IEnumerable<IAuthenticationMethod> SupportedAuthenticationMethods
184+
public IList<IAuthenticationMethod> SupportedAuthenticationMethods
166185
{
167186
get { return _supportedAuthenticationMethods; }
168187
}

src/Renci.SshNet/IAuthenticationMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal interface IAuthenticationMethod
2222
/// <value>
2323
/// The list of allowed authentications.
2424
/// </value>
25-
IList<string> AllowedAuthentications { get; }
25+
string[] AllowedAuthentications { get; }
2626

2727
/// <summary>
2828
/// Gets the name of the authentication method.

0 commit comments

Comments
 (0)