mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-10 21:33:20 +00:00
stub out sync, connect to server, print results
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user