1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-16 00:04:34 +00:00

cleanup console code

This commit is contained in:
Kyle Spearrin
2017-05-18 08:01:06 -04:00
parent 1c0061bcef
commit a4c35bae44

View File

@@ -141,9 +141,9 @@ namespace Bit.Console
private static async Task LogInAsync() private static async Task LogInAsync()
{ {
if(Core.Services.AuthService.Instance.Authenticated) if(AuthService.Instance.Authenticated)
{ {
Con.WriteLine("You are already logged in as {0}.", Core.Services.TokenService.Instance.AccessTokenEmail); Con.WriteLine("You are already logged in as {0}.", TokenService.Instance.AccessTokenEmail);
return; return;
} }
@@ -181,9 +181,7 @@ namespace Bit.Console
{ {
Con.WriteLine(); Con.WriteLine();
Con.WriteLine(); Con.WriteLine();
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Invalid input parameters.");
Con.WriteLine("Invalid input parameters.");
Con.ResetColor();
return; return;
} }
@@ -192,11 +190,11 @@ namespace Bit.Console
LoginResult result = null; LoginResult result = null;
if(string.IsNullOrWhiteSpace(token)) if(string.IsNullOrWhiteSpace(token))
{ {
result = await Core.Services.AuthService.Instance.LogInAsync(email, masterPassword); result = await AuthService.Instance.LogInAsync(email, masterPassword);
} }
else else
{ {
result = await Core.Services.AuthService.Instance.LogInTwoFactorAsync(email, masterPassword, token); result = await AuthService.Instance.LogInTwoFactorAsync(email, masterPassword, token);
} }
if(string.IsNullOrWhiteSpace(token) && result.TwoFactorRequired) if(string.IsNullOrWhiteSpace(token) && result.TwoFactorRequired)
@@ -206,7 +204,7 @@ namespace Bit.Console
Con.WriteLine("Two-step login is enabled on this account. Please enter your verification code."); Con.WriteLine("Two-step login is enabled on this account. Please enter your verification code.");
Con.Write("Verification code: "); Con.Write("Verification code: ");
token = Con.ReadLine().Trim(); token = Con.ReadLine().Trim();
result = await Core.Services.AuthService.Instance.LogInTwoFactorWithHashAsync(token, email, result = await AuthService.Instance.LogInTwoFactorWithHashAsync(token, email,
result.MasterPasswordHash); result.MasterPasswordHash);
} }
@@ -239,11 +237,11 @@ namespace Bit.Console
{ {
result.Success = false; result.Success = false;
result.ErrorMessage = "Organization not found."; result.ErrorMessage = "Organization not found.";
Core.Services.AuthService.Instance.LogOut(); AuthService.Instance.LogOut();
} }
else else
{ {
Core.Services.SettingsService.Instance.Organization = org; SettingsService.Instance.Organization = org;
} }
} }
@@ -251,15 +249,12 @@ namespace Bit.Console
Con.WriteLine(); Con.WriteLine();
if(result.Success) if(result.Success)
{ {
Con.ForegroundColor = ConsoleColor.Green; WriteSuccessLine(string.Format("You have successfully logged in as {0}!",
Con.WriteLine("You have successfully logged in as {0}!", Core.Services.TokenService.Instance.AccessTokenEmail); TokenService.Instance.AccessTokenEmail));
Con.ResetColor();
} }
else else
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine(result.ErrorMessage);
Con.WriteLine(result.ErrorMessage);
Con.ResetColor();
} }
masterPassword = null; masterPassword = null;
@@ -267,16 +262,14 @@ namespace Bit.Console
private static Task LogOutAsync() private static Task LogOutAsync()
{ {
if(Core.Services.AuthService.Instance.Authenticated) if(AuthService.Instance.Authenticated)
{ {
Core.Services.AuthService.Instance.LogOut(); AuthService.Instance.LogOut();
Con.ForegroundColor = ConsoleColor.Green; WriteSuccessLine("You have successfully logged out!");
Con.WriteLine("You have successfully logged out!");
Con.ResetColor();
} }
else else
{ {
Con.WriteLine("You are not logged in."); WriteErrorLine("You are not logged in.");
} }
return Task.FromResult(0); return Task.FromResult(0);
@@ -284,7 +277,7 @@ namespace Bit.Console
private static Task ConfigDirectoryAsync() private static Task ConfigDirectoryAsync()
{ {
var config = Core.Services.SettingsService.Instance.Server ?? new ServerConfiguration(); var config = SettingsService.Instance.Server ?? new ServerConfiguration();
if(_usingArgs) if(_usingArgs)
{ {
@@ -298,9 +291,7 @@ namespace Bit.Console
} }
else else
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Unable to parse type parameter.");
Con.WriteLine("Unable to parse type parameter.");
Con.ResetColor();
return Task.FromResult(0); return Task.FromResult(0);
} }
} }
@@ -487,23 +478,17 @@ namespace Bit.Console
Con.WriteLine(); Con.WriteLine();
if(config.Ldap != null && string.IsNullOrWhiteSpace(config.Ldap.Address)) if(config.Ldap != null && string.IsNullOrWhiteSpace(config.Ldap.Address))
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Invalid input parameters.");
Con.WriteLine("Invalid input parameters.");
Con.ResetColor();
} }
else if(config.Azure != null && (string.IsNullOrWhiteSpace(config.Azure.Id) || else if(config.Azure != null && (string.IsNullOrWhiteSpace(config.Azure.Id) ||
config.Azure.Secret == null || string.IsNullOrWhiteSpace(config.Azure.Tenant))) config.Azure.Secret == null || string.IsNullOrWhiteSpace(config.Azure.Tenant)))
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Invalid input parameters.");
Con.WriteLine("Invalid input parameters.");
Con.ResetColor();
} }
else else
{ {
Core.Services.SettingsService.Instance.Server = config; SettingsService.Instance.Server = config;
Con.ForegroundColor = ConsoleColor.Green; WriteSuccessLine("Saved directory server configuration.");
Con.WriteLine("Saved directory server configuration.");
Con.ResetColor();
} }
return Task.FromResult(0); return Task.FromResult(0);
@@ -511,8 +496,8 @@ namespace Bit.Console
private static Task ConfigSyncAsync() private static Task ConfigSyncAsync()
{ {
var config = Core.Services.SettingsService.Instance.Sync ?? var config = SettingsService.Instance.Sync ??
new SyncConfiguration(Core.Services.SettingsService.Instance.Server.Type); new SyncConfiguration(SettingsService.Instance.Server.Type);
if(_usingArgs) if(_usingArgs)
{ {
@@ -527,7 +512,7 @@ namespace Bit.Console
config.IntervalMinutes = intervalMinutes; config.IntervalMinutes = intervalMinutes;
} }
if(Core.Services.SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory) if(SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory)
{ {
if(parameters.ContainsKey("gf")) if(parameters.ContainsKey("gf"))
{ {
@@ -586,7 +571,7 @@ namespace Bit.Console
config.SyncGroups = input == "y" || input == "yes"; config.SyncGroups = input == "y" || input == "yes";
} }
if(config.SyncGroups && if(config.SyncGroups &&
Core.Services.SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory) SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory)
{ {
Con.Write("Group object class [{0}]: ", config.Ldap.GroupObjectClass); Con.Write("Group object class [{0}]: ", config.Ldap.GroupObjectClass);
input = Con.ReadLine(); input = Con.ReadLine();
@@ -621,7 +606,7 @@ namespace Bit.Console
config.SyncUsers = input == "y" || input == "yes"; config.SyncUsers = input == "y" || input == "yes";
} }
if(config.SyncUsers && if(config.SyncUsers &&
Core.Services.SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory) SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory)
{ {
Con.Write("User path [{0}]: ", config.Ldap.UserPath); Con.Write("User path [{0}]: ", config.Ldap.UserPath);
input = Con.ReadLine(); input = Con.ReadLine();
@@ -649,7 +634,7 @@ namespace Bit.Console
config.UserFilter = input; config.UserFilter = input;
} }
if(Core.Services.SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory) if(SettingsService.Instance.Server.Type != Core.Enums.DirectoryType.AzureActiveDirectory)
{ {
Con.Write("Member Of Attribute [{0}]: ", config.Ldap.MemberAttribute); Con.Write("Member Of Attribute [{0}]: ", config.Ldap.MemberAttribute);
input = Con.ReadLine(); input = Con.ReadLine();
@@ -685,21 +670,19 @@ namespace Bit.Console
Con.WriteLine(); Con.WriteLine();
Con.WriteLine(); Con.WriteLine();
Core.Services.SettingsService.Instance.Sync = config; SettingsService.Instance.Sync = config;
Con.ForegroundColor = ConsoleColor.Green; WriteSuccessLine("Saved sync configuration.");
Con.WriteLine("Saved sync configuration.");
Con.ResetColor();
return Task.FromResult(0); return Task.FromResult(0);
} }
private static async Task SyncAsync() private static async Task SyncAsync()
{ {
if(!Core.Services.AuthService.Instance.Authenticated) if(!AuthService.Instance.Authenticated)
{ {
Con.WriteLine("You are not logged in."); Con.WriteLine("You are not logged in.");
} }
else if(Core.Services.SettingsService.Instance.Server == null) else if(SettingsService.Instance.Server == null)
{ {
Con.WriteLine("Server is not configured."); Con.WriteLine("Server is not configured.");
} }
@@ -717,27 +700,23 @@ namespace Bit.Console
if(result.Success) if(result.Success)
{ {
Con.ForegroundColor = ConsoleColor.Green; WriteSuccessLine(string.Format("Syncing complete ({0} users, {1} groups).",
Con.WriteLine("Syncing complete ({0} users, {1} groups).", result.Users.Count, result.Groups.Count); result.Users.Count, result.Groups.Count));
Con.ResetColor();
} }
else else
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Syncing failed.\n" + result.ErrorMessage);
Con.WriteLine("Syncing failed.");
Con.WriteLine(result.ErrorMessage);
Con.ResetColor();
} }
} }
} }
private static async Task PrintAsync() private static async Task PrintAsync()
{ {
if(!Core.Services.AuthService.Instance.Authenticated) if(!AuthService.Instance.Authenticated)
{ {
Con.WriteLine("You are not logged in."); Con.WriteLine("You are not logged in.");
} }
else if(Core.Services.SettingsService.Instance.Server == null) else if(SettingsService.Instance.Server == null)
{ {
Con.WriteLine("Server is not configured."); Con.WriteLine("Server is not configured.");
} }
@@ -775,10 +754,7 @@ namespace Bit.Console
} }
else else
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("Querying failed.\n" + result.ErrorMessage);
Con.WriteLine("Querying failed.");
Con.WriteLine(result.ErrorMessage);
Con.ResetColor();
} }
} }
} }
@@ -844,9 +820,7 @@ namespace Bit.Console
if((start || stop) && !Helpers.IsAdministrator()) if((start || stop) && !Helpers.IsAdministrator())
{ {
Con.ForegroundColor = ConsoleColor.Red; WriteErrorLine("You must be an administrator to control the service.");
Con.WriteLine("You must be an administrator to control the service.");
Con.ResetColor();
return Task.FromResult(0); return Task.FromResult(0);
} }
@@ -961,5 +935,19 @@ namespace Bit.Console
return dict; return dict;
} }
private static void WriteErrorLine(string message)
{
Con.ForegroundColor = ConsoleColor.Red;
Con.WriteLine(message);
Con.ResetColor();
}
private static void WriteSuccessLine(string message)
{
Con.ForegroundColor = ConsoleColor.Green;
Con.WriteLine(message);
Con.ResetColor();
}
} }
} }