diff --git a/src/Console/Program.cs b/src/Console/Program.cs index 1ca9a5c0..31369b5d 100644 --- a/src/Console/Program.cs +++ b/src/Console/Program.cs @@ -1,4 +1,5 @@ using Bit.Core.Models; +using Bit.Core.Utilities; using System; using System.Collections.Generic; using System.Linq; @@ -185,7 +186,9 @@ namespace Bit.Console if(Core.Services.AuthService.Instance.Authenticated) { Core.Services.AuthService.Instance.LogOut(); + Con.ForegroundColor = ConsoleColor.Green; Con.WriteLine("You have successfully logged out!"); + Con.ResetColor(); } else { @@ -269,7 +272,7 @@ namespace Bit.Console return Task.FromResult(0); } - private static Task SyncAsync() + private static async Task SyncAsync() { if(!Core.Services.AuthService.Instance.Authenticated) { @@ -282,9 +285,11 @@ namespace Bit.Console else { Con.WriteLine("Syncing..."); + await Sync.SyncAllAsync(); + Con.ForegroundColor = ConsoleColor.Green; + Con.WriteLine("Syncing complete."); + Con.ResetColor(); } - - return Task.FromResult(0); } private static string ReadSecureLine() diff --git a/src/Core/Models/ServerConfiguration.cs b/src/Core/Models/ServerConfiguration.cs index 77ede252..3a63ce3d 100644 --- a/src/Core/Models/ServerConfiguration.cs +++ b/src/Core/Models/ServerConfiguration.cs @@ -1,5 +1,7 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.DirectoryServices; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,5 +15,13 @@ namespace Bit.Core.Models public string Path { get; set; } public string Username { get; set; } public EncryptedData Password { get; set; } + [JsonIgnore] + public string ServerPath => $"LDAP://{Address}:{Port}/{Path}"; + + public DirectoryEntry GetDirectoryEntry() + { + var entry = new DirectoryEntry(ServerPath, Username, Password.DecryptToString(), AuthenticationTypes.None); + return entry; + } } } diff --git a/src/Core/Utilities/Sync.cs b/src/Core/Utilities/Sync.cs index 23c6953b..844a5f73 100644 --- a/src/Core/Utilities/Sync.cs +++ b/src/Core/Utilities/Sync.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.DirectoryServices; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,5 +10,71 @@ namespace Bit.Core.Utilities { public static class Sync { + public static Task SyncGroupsAsync() + { + if(Services.SettingsService.Instance.Server == null) + { + throw new ApplicationException("No configuration for directory server."); + } + + if(!Services.AuthService.Instance.Authenticated) + { + throw new ApplicationException("Not authenticated."); + } + + var entry = Services.SettingsService.Instance.Server.GetDirectoryEntry(); + var searcher = new DirectorySearcher(entry); + var result = searcher.FindAll(); + + PrintSearchResults(result); + + return Task.FromResult(0); + } + + public static Task SyncUsersAsync() + { + if(Services.SettingsService.Instance.Server == null) + { + throw new ApplicationException("No configuration for directory server."); + } + + if(!Services.AuthService.Instance.Authenticated) + { + throw new ApplicationException("Not authenticated."); + } + + return Task.FromResult(0); + } + + public static async Task SyncAllAsync() + { + await SyncGroupsAsync(); + await SyncUsersAsync(); + } + + private static void PrintSearchResults(SearchResultCollection result) + { + foreach(SearchResult item in result) + { + Console.WriteLine(item.Path); + + foreach(DictionaryEntry prop in item.Properties) + { + Console.Write(" " + prop.Key + ": "); + + var vals = prop.Value as ResultPropertyValueCollection; + for(int i = 0; i < vals.Count; i++) + { + Console.Write(vals[i]); + if(i != vals.Count - 1) + { + Console.Write(" | "); + } + } + + Console.Write("\n"); + } + } + } } }