-
Notifications
You must be signed in to change notification settings - Fork 2
/
NameResolverService.cs
61 lines (55 loc) · 2.13 KB
/
NameResolverService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}
}
}