mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-17 08:43:27 +00:00
66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
using Bit.Core.Services;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.DirectoryServices;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Bit.Core.Models
|
|
{
|
|
public class LdapConfiguration
|
|
{
|
|
public string Address { get; set; }
|
|
public string Port { get; set; } = "389";
|
|
public string Path { get; set; }
|
|
public string Username { get; set; }
|
|
public EncryptedData Password { get; set; }
|
|
public Enums.DirectoryType Type { get; set; } = Enums.DirectoryType.ActiveDirectory;
|
|
|
|
public DirectoryEntry GetUserDirectoryEntry()
|
|
{
|
|
return GetPathedDirectoryEntry(SettingsService.Instance.Sync.Ldap.UserPath);
|
|
}
|
|
|
|
public DirectoryEntry GetGroupDirectoryEntry()
|
|
{
|
|
return GetPathedDirectoryEntry(SettingsService.Instance.Sync.Ldap.GroupPath);
|
|
}
|
|
|
|
public DirectoryEntry GetPathedDirectoryEntry(string pathPrefix = null)
|
|
{
|
|
var path = Path;
|
|
if(!string.IsNullOrWhiteSpace(pathPrefix))
|
|
{
|
|
path = string.Concat(pathPrefix, ",", path);
|
|
}
|
|
|
|
return GetDirectoryEntry(path);
|
|
}
|
|
|
|
public DirectoryEntry GetBasePathDirectoryEntry()
|
|
{
|
|
var path = Path.Substring(Path.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase));
|
|
return GetDirectoryEntry(path);
|
|
}
|
|
|
|
public DirectoryEntry GetDirectoryEntry(string path = null)
|
|
{
|
|
if(Password == null && string.IsNullOrWhiteSpace(Username))
|
|
{
|
|
return new DirectoryEntry(ServerPath(path));
|
|
}
|
|
else
|
|
{
|
|
return new DirectoryEntry(ServerPath(path), Username, Password.DecryptToString(), AuthenticationTypes.None);
|
|
}
|
|
}
|
|
|
|
private string ServerPath(string path)
|
|
{
|
|
return $"LDAP://{Address}:{Port}/{path}";
|
|
}
|
|
}
|
|
}
|