Skip to content

Commit

Permalink
Remove Linq + validator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed May 13, 2021
1 parent 6a390fe commit aa03950
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ internal static string ConvertToString(object value)

private const string ApplicationIntentReadWriteString = "ReadWrite";
private const string ApplicationIntentReadOnlyString = "ReadOnly";
const string NotSpecifiedString = "Not Specified";
const string SqlPasswordString = "Sql Password";
const string ActiveDirectoryPasswordString = "Active Directory Password";
const string ActiveDirectoryIntegratedString = "Active Directory Integrated";
Expand All @@ -108,12 +109,41 @@ internal static string ConvertToString(object value)
internal const string ActiveDirectoryMSIString = "Active Directory MSI";
internal const string ActiveDirectoryDefaultString = "Active Directory Default";

private static bool IsValidAuthenticationMethodEnum()
{
string[] names = Enum.GetNames(typeof(SqlAuthenticationMethod));
string[] supportedList =
{
NotSpecifiedString,
SqlPasswordString,
ActiveDirectoryPasswordString,
ActiveDirectoryIntegratedString,
ActiveDirectoryInteractiveString,
ActiveDirectoryServicePrincipalString,
ActiveDirectoryDeviceCodeFlowString,
ActiveDirectoryManagedIdentityString,
ActiveDirectoryMSIString,
ActiveDirectoryDefaultString
};
bool listValid;
if (listValid = names.Length == supportedList.Length)
{
for (int i = 0; i < supportedList.Length; i++)
{
if (supportedList[i].CompareTo(names[i]) == 0)
{
listValid = false;
}
}
}
return listValid;
}

internal static bool TryConvertToAuthenticationType(string value, out SqlAuthenticationMethod result)
{
Debug.Assert(Enum.GetNames(typeof(SqlAuthenticationMethod)).Length == 10, "SqlAuthenticationMethod enum has changed, update needed");
Debug.Assert(IsValidAuthenticationMethodEnum(), "SqlAuthenticationMethod enum has changed, update needed");

bool isSuccess = false;

if (StringComparer.InvariantCultureIgnoreCase.Equals(value, SqlPasswordString)
|| StringComparer.InvariantCultureIgnoreCase.Equals(value, Convert.ToString(SqlAuthenticationMethod.SqlPassword, CultureInfo.InvariantCulture)))
{
Expand Down Expand Up @@ -508,7 +538,7 @@ internal static ApplicationIntent ConvertToApplicationIntent(string keyword, obj

internal static bool IsValidAuthenticationTypeValue(SqlAuthenticationMethod value)
{
Debug.Assert(Enum.GetNames(typeof(SqlAuthenticationMethod)).Length == 9, "SqlAuthenticationMethod enum has changed, update needed");
Debug.Assert(Enum.GetNames(typeof(SqlAuthenticationMethod)).Length == 10, "SqlAuthenticationMethod enum has changed, update needed");
return value == SqlAuthenticationMethod.SqlPassword
|| value == SqlAuthenticationMethod.ActiveDirectoryPassword
|| value == SqlAuthenticationMethod.ActiveDirectoryIntegrated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ internal static ApplicationIntent ConvertToApplicationIntent(string keyword, obj
}
}

const string NotSpecifiedString = "Not Specified";
const string SqlPasswordString = "Sql Password";
const string ActiveDirectoryPasswordString = "Active Directory Password";
const string ActiveDirectoryIntegratedString = "Active Directory Integrated";
Expand All @@ -525,11 +526,41 @@ internal static ApplicationIntent ConvertToApplicationIntent(string keyword, obj
internal const string ActiveDirectoryManagedIdentityString = "Active Directory Managed Identity";
internal const string ActiveDirectoryMSIString = "Active Directory MSI";
internal const string ActiveDirectoryDefaultString = "Active Directory Default";
const string SqlCertificateString = "Sql Certificate";
// const string SqlCertificateString = "Sql Certificate";

private static bool IsValidAuthenticationMethodEnum()
{
string[] names = Enum.GetNames(typeof(SqlAuthenticationMethod));
string[] supportedList =
{
NotSpecifiedString,
SqlPasswordString,
ActiveDirectoryPasswordString,
ActiveDirectoryIntegratedString,
ActiveDirectoryInteractiveString,
ActiveDirectoryServicePrincipalString,
ActiveDirectoryDeviceCodeFlowString,
ActiveDirectoryManagedIdentityString,
ActiveDirectoryMSIString,
ActiveDirectoryDefaultString
};
bool listValid;
if (listValid = names.Length == supportedList.Length)
{
for (int i = 0; i < supportedList.Length; i++)
{
if (supportedList[i].CompareTo(names[i]) == 0)
{
listValid = false;
}
}
}
return listValid;
}

internal static bool TryConvertToAuthenticationType(string value, out SqlAuthenticationMethod result)
{
Debug.Assert(Enum.GetNames(typeof(SqlAuthenticationMethod)).Length == 10, "SqlAuthenticationMethod enum has changed, update needed");
Debug.Assert(IsValidAuthenticationMethodEnum(), "SqlAuthenticationMethod enum has changed, update needed");

bool isSuccess = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -215,15 +214,28 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
parameters.AuthenticationMethod == SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow)
{
// Fetch available accounts from 'app' instance
System.Collections.Generic.IEnumerable<IAccount> accounts = await app.GetAccountsAsync();
IAccount account;
if (!string.IsNullOrEmpty(parameters.UserId))
System.Collections.Generic.IEnumerator<IAccount> accounts = (await app.GetAccountsAsync()).GetEnumerator();

IAccount account = default;
if (accounts.MoveNext())
{
account = accounts.FirstOrDefault(a => parameters.UserId.Equals(a.Username, StringComparison.InvariantCultureIgnoreCase));
}
else
{
account = accounts.FirstOrDefault();
if (!string.IsNullOrEmpty(parameters.UserId))
{
do
{
IAccount currentVal = accounts.Current;
if (string.Compare(parameters.UserId, currentVal.Username, StringComparison.InvariantCultureIgnoreCase) == 0)
{
account = currentVal;
break;
}
}
while (accounts.MoveNext());
}
else
{
account = accounts.Current;
}
}

if (null != account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ public static void ActiveDirectoryDefaultWithPasswordMustFail()
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDefaultMustPass()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=ActiveDirectoryDefault;";
Expand Down

0 comments on commit aa03950

Please sign in to comment.