Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from MultifactorLab/netbios_name_resolving
Netbios name resolving
- Loading branch information
Showing
20 changed files
with
482 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
MultiFactor.Ldap.Adapter/Core/NameResolving/LdapIdentityFormat.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving | ||
{ | ||
public enum LdapIdentityFormat | ||
{ | ||
None = 0, | ||
Upn = 1, | ||
UidAndNetbios = 2, // uid@netbios | ||
SamAccountName = 3, | ||
NetBIOSAndUid = 4, // NETBIOS\uid | ||
DistinguishedName = 5 | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
MultiFactor.Ldap.Adapter/Core/NameResolving/NameResolverContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MultiFactor.Ldap.Adapter.Services; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving | ||
{ | ||
public class NameResolverContext | ||
{ | ||
public NetbiosDomainName[] Domains; | ||
public LdapProfile Profile; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
MultiFactor.Ldap.Adapter/Core/NameResolving/NameResolverService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using MultiFactor.Ldap.Adapter.Core.NameResolving; | ||
using MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators; | ||
using Serilog; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving | ||
{ | ||
public class NameResolverService | ||
{ | ||
private ILogger _logger; | ||
|
||
public NameResolverService(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public string Resolve(NameResolverContext context, string name, LdapIdentityFormat to) | ||
{ | ||
var from = NameTypeDetector.GetType(name); | ||
if(from == null) | ||
{ | ||
return name; | ||
} | ||
|
||
var resolver = GetTranslator(context, (LdapIdentityFormat)from, to); | ||
if(resolver == null) | ||
{ | ||
return name; | ||
} | ||
return resolver.Translate(context, name); | ||
} | ||
|
||
|
||
public INameTranslator GetTranslator(NameResolverContext context, LdapIdentityFormat from, LdapIdentityFormat to) | ||
{ | ||
if (from == LdapIdentityFormat.UidAndNetbios && to == LdapIdentityFormat.Upn) | ||
{ | ||
return new sAMAccountNameAndNetbiosToUpnNameTranslator(); | ||
} | ||
else if (from == LdapIdentityFormat.NetBIOSAndUid && to == LdapIdentityFormat.Upn) | ||
{ | ||
return new NetbiosToUpnNameTranslator(); | ||
} | ||
else if (from == LdapIdentityFormat.DistinguishedName && to == LdapIdentityFormat.Upn) | ||
{ | ||
return new DistinguishedNameToUpnTranslator(); | ||
} | ||
// There are a case when sAMAccountName@domain.local looks exactly like UPN | ||
// Let's try an UPN we got from the profile | ||
if (from == LdapIdentityFormat.Upn && to == LdapIdentityFormat.Upn && context.Profile != null) | ||
{ | ||
return new UpnFromProfileNameTranslator(); | ||
} | ||
if(from == LdapIdentityFormat.SamAccountName && to == LdapIdentityFormat.Upn) | ||
{ | ||
return new sAMAccountNameToUpnNameTranslator(); | ||
} | ||
_logger.Error($"Suitable username format was not found"); | ||
return null; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...actor.Ldap.Adapter/Core/NameResolving/NameTranslators/DistinguishedNameToUpnTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public class DistinguishedNameToUpnTranslator : INameTranslator | ||
{ | ||
public string Translate(NameResolverContext nameTranslatorContext, string from) | ||
{ | ||
if (nameTranslatorContext.Profile != null) | ||
{ | ||
return nameTranslatorContext.Profile.Upn; | ||
} | ||
return from; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
MultiFactor.Ldap.Adapter/Core/NameResolving/NameTranslators/INameTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public interface INameTranslator | ||
{ | ||
string Translate(NameResolverContext context, string from); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
MultiFactor.Ldap.Adapter/Core/NameResolving/NameTranslators/NetbiosToUpnNameTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public class NetbiosToUpnNameTranslator : INameTranslator | ||
{ | ||
public string Translate(NameResolverContext nameTranslatorContext, string from) | ||
{ | ||
if(nameTranslatorContext.Profile != null) | ||
{ | ||
return nameTranslatorContext.Profile.Upn; | ||
} | ||
|
||
foreach (var domain in nameTranslatorContext.Domains) | ||
{ | ||
var regex = new Regex("^" + domain.NetbiosName.ToLower() + "\\\\", RegexOptions.IgnoreCase); | ||
if (regex.IsMatch(from)) | ||
{ | ||
var result = regex.Replace(from + "@" + domain.Domain.ToLower(), ""); | ||
return result; | ||
} | ||
} | ||
return from; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
MultiFactor.Ldap.Adapter/Core/NameResolving/NameTranslators/UpnFromProfileNameTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
| ||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public class UpnFromProfileNameTranslator : INameTranslator | ||
{ | ||
public string Translate(NameResolverContext nameResolverContext, string from) | ||
{ | ||
if (nameResolverContext.Profile != null) | ||
{ | ||
return nameResolverContext.Profile.Upn; | ||
} | ||
return from; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...Adapter/Core/NameResolving/NameTranslators/sAMAccountNameAndNetbiosToUpnNameTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public class sAMAccountNameAndNetbiosToUpnNameTranslator : INameTranslator | ||
{ | ||
public string Translate(NameResolverContext nameTranslatorContext, string from) | ||
{ | ||
if (nameTranslatorContext.Profile != null) | ||
{ | ||
return nameTranslatorContext.Profile.Upn; | ||
} | ||
foreach(var domain in nameTranslatorContext.Domains) | ||
{ | ||
var regex = new Regex("@" + domain.NetbiosName.ToLower() + "$", RegexOptions.IgnoreCase); | ||
if(regex.IsMatch(from)) | ||
{ | ||
var result = regex.Replace(from, "@" + domain.Domain.ToLower()); | ||
return result; | ||
} | ||
} | ||
return from; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ctor.Ldap.Adapter/Core/NameResolving/NameTranslators/sAMAccountNameToUpnNameTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving.NameTranslators | ||
{ | ||
public class sAMAccountNameToUpnNameTranslator : INameTranslator | ||
{ | ||
public string Translate(NameResolverContext nameTranslatorContext, string from) | ||
{ | ||
if (nameTranslatorContext.Profile != null) | ||
{ | ||
return nameTranslatorContext.Profile.Upn; | ||
} | ||
return from; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
MultiFactor.Ldap.Adapter/Core/NameResolving/NameTypeDetector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving | ||
{ | ||
public class NameTypeDetector | ||
{ | ||
public static LdapIdentityFormat? GetType(string name) | ||
{ | ||
if (name.Contains('\\')) | ||
{ | ||
return LdapIdentityFormat.NetBIOSAndUid; | ||
} | ||
if (name.IndexOf("CN=", StringComparison.OrdinalIgnoreCase) >= 0) | ||
{ | ||
return LdapIdentityFormat.DistinguishedName; | ||
} | ||
var domainRegex = new Regex("^[^@]+@(.+)$"); | ||
var domainMatch = domainRegex.Match(name); | ||
if (!domainMatch.Success || domainMatch.Groups.Count < 2) | ||
{ | ||
return LdapIdentityFormat.SamAccountName; | ||
} | ||
return domainMatch.Groups[1].Value.Count(x => x == '.') == 0 | ||
? LdapIdentityFormat.UidAndNetbios | ||
: LdapIdentityFormat.Upn; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
MultiFactor.Ldap.Adapter/Core/NameResolving/NetbiosDomainName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace MultiFactor.Ldap.Adapter.Core.NameResolving | ||
{ | ||
public class NetbiosDomainName | ||
{ | ||
public string Domain; | ||
public string NetbiosName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.