Skip to content

Commit

Permalink
LDAP.SearchObjects - New features
Browse files Browse the repository at this point in the history
  • Loading branch information
Riku Virtanen committed Nov 6, 2024
1 parent 21e42e0 commit 8db58ba
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 95 deletions.
5 changes: 5 additions & 0 deletions Frends.LDAP.SearchObjects/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [2.0.0] - 2024-11-06
### Added
- [Breaking] Added parameter for AnonymousBind to enable to connect without credentials.
- [Breaking] Added parameter for LDAPProtocolVersion to choose what LDAP version should be used.

## [1.0.0] - 2022-10-03
### Added
- Initial implementation
101 changes: 11 additions & 90 deletions Frends.LDAP.SearchObjects/Frends.LDAP.SearchObjects.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ Create a simple LDAP server to docker
[TestInitialize]
public void Setup()
{
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
LDAPProtocolVersion = LDAPVersion.V3
};

try
{
CreateTestUsers();
Expand All @@ -50,15 +61,6 @@ public void Search_ScopeSub_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -99,15 +101,6 @@ public void Search_ScopeOne_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -148,15 +141,6 @@ public void Search_DerefSearching_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -197,15 +181,6 @@ public void Search_DerefAlways_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -246,15 +221,6 @@ public void Search_DerefFinding_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -295,15 +261,6 @@ public void Search_BatchSize_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -344,15 +301,6 @@ public void Search_MaxResults_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true) && result.SearchResult.Count == 2);
Expand Down Expand Up @@ -387,15 +335,6 @@ public void Search_TypesOnly_Test()
TypesOnly = true,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down Expand Up @@ -436,15 +375,6 @@ public void Search_Filter_Test()
TypesOnly = default,
Attributes = null,
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true) && result.SearchResult.Count == 2);
Expand Down Expand Up @@ -492,15 +422,6 @@ public void Search_Attributes_Test()
TypesOnly = default,
Attributes = atr.ToArray(),
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.SearchObjects(input, connection, default);
Assert.IsTrue(result.Success.Equals(true));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Frends.LDAP.SearchObjects.Definitions;

/// <summary>
Expand Down Expand Up @@ -31,16 +32,39 @@ public class Connection
/// <example>true</example>
public bool TLS { get; set; }

/// <summary>
/// Used LDAP protocol version.
/// </summary>
/// <example>V2</example>
[DefaultValue(LDAPVersion.V3)]
public LDAPVersion LDAPProtocolVersion { get; set; }

/// <summary>
/// If enabled credentials are not used to create a bind to the LDAP server.
/// </summary>
/// <example>true</example>
[DefaultValue(false)]
public bool AnonymousBind { get; set; }

/// <summary>
/// User.
/// </summary>
/// <example>Foo</example>
[UIHint(nameof(AnonymousBind), "", false)]
public string User { get; set; }

/// <summary>
/// Password.
/// </summary>
/// <example>Bar123</example>
[UIHint(nameof(AnonymousBind), "", false)]
[PasswordPropertyText]
public string Password { get; set; }

/// <summary>
/// If enabled Task throws an exception when LDAP error happens.
/// </summary>
/// <example>true</example>
[DefaultValue(false)]
public bool ThrowExceptionOnError { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ public enum SearchDereference
/// </summary>
DerefAlways,
}

/// <summary>
/// LDAP protocol versions.
/// </summary>
public enum LDAPVersion
{
/// <summary>
/// LDAP Version 2
/// </summary>
V2,
/// <summary>
/// LDAP Version 3
/// </summary>
V3,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.0.0</Version>
<Version>2.0.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class LDAP
/// <returns>Object { bool Success, string Error, string CommonName, List&lt;SearchResult&gt; SearchResult }</returns>
public static Result SearchObjects([PropertyTab] Input input, [PropertyTab] Connection connection, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(connection.Host) || string.IsNullOrWhiteSpace(connection.User) || string.IsNullOrWhiteSpace(connection.Password))
throw new Exception("Connection parameters missing.");
if (string.IsNullOrWhiteSpace(connection.Host))
throw new Exception("Host is missing.");

var conn = new LdapConnection();
var defaultPort = connection.SecureSocketLayer ? 636 : 389;
Expand All @@ -43,12 +43,28 @@ public static Result SearchObjects([PropertyTab] Input input, [PropertyTab] Conn
foreach (var i in input.Attributes)
atr.Add(i.Key.ToString());

var ldapVersion = 0;
switch (connection.LDAPProtocolVersion)
{
case LDAPVersion.V2:
ldapVersion = 2;
break;
case LDAPVersion.V3:
ldapVersion = 3;
break;
}

try
{
conn.SecureSocketLayer = connection.SecureSocketLayer;
conn.Connect(connection.Host, connection.Port == 0 ? defaultPort : connection.Port);
if (connection.TLS) conn.StartTls();
conn.Bind(connection.User, connection.Password);
if (connection.TLS)
conn.StartTls();

if (connection.AnonymousBind)
conn.Bind(version: ldapVersion, dn: null, passwd: (string)null);
else
conn.Bind(version: ldapVersion, connection.User, connection.Password);

LdapSearchQueue queue = conn.Search(
input.SearchBase,
Expand Down Expand Up @@ -86,6 +102,8 @@ public static Result SearchObjects([PropertyTab] Input input, [PropertyTab] Conn
}
catch (LdapException ex)
{
if (connection.ThrowExceptionOnError)
throw;
return new Result(false, ex.Message, null);
}
catch (Exception ex)
Expand Down

0 comments on commit 8db58ba

Please sign in to comment.