| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.DirectoryServices.Protocols; |
| | | 8 | | using System.Net; |
| | | 9 | | using Microsoft.Extensions.Caching.Memory; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security |
| | | 12 | | { |
| | | 13 | | public class LdapSettings |
| | | 14 | | { |
| | | 15 | | private LdapConnection _ldapConnection; |
| | | 16 | | /// <summary> |
| | | 17 | | /// Default constructor. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="servers">List of ldap servers</param> |
| | | 20 | | /// <param name="domain">domain name to search for</param> |
| | | 21 | | /// <param name="orgUnit"> org unit to search for. This is required per the issue https://github.com/dotnet/runt |
| | 9 | 22 | | public LdapSettings(string server, string domain, string orgUnit) : this(new string[] { server }, domain, orgUni |
| | | 23 | | { |
| | 9 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Constructor. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="server"></param> |
| | | 30 | | /// <param name="domain"></param> |
| | | 31 | | /// <param name="machineAccountName"></param> |
| | | 32 | | /// <param name="machineAccountPassword"></param> |
| | | 33 | | /// <param name="orgUnit"></param> |
| | | 34 | | public LdapSettings(string server, string domain, string machineAccountName, string machineAccountPassword, stri |
| | 0 | 35 | | : this(new string[] { server }, domain, machineAccountName, machineAccountPassword, orgUnit) |
| | | 36 | | { |
| | | 37 | | |
| | 0 | 38 | | } |
| | | 39 | | /// <summary> |
| | | 40 | | /// Default constructor. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="servers">List of ldap servers</param> |
| | | 43 | | /// <param name="domain">domain name to search for</param> |
| | | 44 | | /// <param name="orgUnit"> This is required </param> |
| | 9 | 45 | | public LdapSettings(string[] servers, string domain, string orgUnit) : this(servers, domain, null, null, orgUnit |
| | | 46 | | { |
| | | 47 | | |
| | 9 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Constructor to pass machine account name and machine account password. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="servers"></param> |
| | | 54 | | /// <param name="domain"></param> |
| | | 55 | | /// <param name="machineAccountName"></param> |
| | | 56 | | /// <param name="machineAccountPassword"></param> |
| | 9 | 57 | | public LdapSettings(string[] servers, string domain, string machineAccountName, string machineAccountPassword, s |
| | | 58 | | { |
| | 9 | 59 | | Servers = servers; |
| | 9 | 60 | | Domain = domain; |
| | 9 | 61 | | MachineAccountName = machineAccountName; |
| | 9 | 62 | | MachineAccountPassword = machineAccountPassword; |
| | 9 | 63 | | OrgUnit = orgUnit; |
| | 9 | 64 | | Validate(); |
| | 9 | 65 | | EnableLdapClaimResolution = true; |
| | 9 | 66 | | ClaimsCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = ClaimsCacheSize }); |
| | 9 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// list of servers for ldap search |
| | | 71 | | /// </summary> |
| | 18 | 72 | | public string[] Servers { get; } |
| | | 73 | | /// <summary> |
| | | 74 | | /// Configure whether LDAP connection should be used to resolve claims. |
| | | 75 | | /// This is mainly used on Linux. |
| | | 76 | | /// </summary> |
| | | 77 | | [DefaultValue(true)] |
| | 27 | 78 | | public bool EnableLdapClaimResolution { get; set; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// The domain to use for the LDAP connection. This is a mandatory setting. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <example> |
| | | 84 | | /// DOMAIN.com |
| | | 85 | | /// </example> |
| | 9 | 86 | | public string Domain { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Organization unit to start with. If not provided, it will search from top domain. |
| | | 90 | | /// </summary> |
| | 0 | 91 | | public string OrgUnit { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// The machine account name to use when opening the LDAP connection. |
| | | 95 | | /// If this is not provided, the machine wide credentials of the |
| | | 96 | | /// domain joined machine will be used. |
| | | 97 | | /// </summary> |
| | 9 | 98 | | public string MachineAccountName { get; } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// The machine account password to use when opening the LDAP connection. |
| | | 102 | | /// This must be provided if a <see cref="MachineAccountName"/> is provided. |
| | | 103 | | /// </summary> |
| | 9 | 104 | | public string MachineAccountPassword { get; } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// This option indicates whether nested groups should be ignored when |
| | | 108 | | /// resolving Roles. The default is false. |
| | | 109 | | /// </summary> |
| | 0 | 110 | | public bool IgnoreNestedGroups { get; set; } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// The <see cref="LdapConnection"/> to be used to retrieve role claims. |
| | | 114 | | /// If no explicit connection is provided, an LDAP connection will be |
| | | 115 | | /// automatically created based on the <see cref="Domain"/>, |
| | | 116 | | /// <see cref="MachineAccountName"/> and <see cref="MachineAccountPassword"/> |
| | | 117 | | /// options. If provided, this connection will be used and the |
| | | 118 | | /// <see cref="Domain"/>, <see cref="MachineAccountName"/> and |
| | | 119 | | /// <see cref="MachineAccountPassword"/> options will not be used to create |
| | | 120 | | /// the <see cref="LdapConnection"/>. |
| | | 121 | | /// </summary> |
| | | 122 | | internal LdapConnection LdapConnection |
| | | 123 | | { |
| | | 124 | | get |
| | | 125 | | { |
| | 0 | 126 | | if (_ldapConnection == null) |
| | 0 | 127 | | _ldapConnection = ConnectLDAP(); |
| | 0 | 128 | | return _ldapConnection; |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// The sliding expiration that should be used for entries in the cache for user claims, defaults to 10 minutes. |
| | | 134 | | /// This is a sliding expiration that will extend each time claims for a user is retrieved. |
| | | 135 | | /// </summary> |
| | 9 | 136 | | public TimeSpan ClaimsCacheSlidingExpiration { get; set; } = TimeSpan.FromMinutes(10); |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// The absolute expiration that should be used for entries in the cache for user claims, defaults to 60 minutes |
| | | 140 | | /// This is an absolute expiration that starts when a claims for a user is retrieved for the first time. |
| | | 141 | | /// </summary> |
| | 9 | 142 | | public TimeSpan ClaimsCacheAbsoluteExpiration { get; set; } = TimeSpan.FromMinutes(60); |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// The maximum size of the claim results cache, defaults to 100 MB. |
| | | 146 | | /// </summary> |
| | 18 | 147 | | public int ClaimsCacheSize { get; set; } = 100 * 1024 * 1024; |
| | | 148 | | |
| | 9 | 149 | | internal MemoryCache ClaimsCache { get; set; } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Validates the <see cref="LdapSettings"/>. |
| | | 153 | | /// </summary> |
| | | 154 | | public void Validate() |
| | | 155 | | { |
| | 18 | 156 | | if (EnableLdapClaimResolution) |
| | | 157 | | { |
| | 9 | 158 | | if (Servers == null || Servers.Length == 0) |
| | | 159 | | { |
| | 0 | 160 | | throw new ArgumentException(SR.Format(SR.EnableLdapClaimResolutionServer, nameof(EnableLdapClaimReso |
| | | 161 | | } |
| | 9 | 162 | | if (string.IsNullOrEmpty(Domain)) |
| | | 163 | | { |
| | 0 | 164 | | throw new ArgumentException(SR.Format(SR.EnableLdapClaimResolutionDomain, nameof(EnableLdapClaimReso |
| | | 165 | | } |
| | | 166 | | |
| | 9 | 167 | | if (string.IsNullOrEmpty(MachineAccountName) && !string.IsNullOrEmpty(MachineAccountPassword)) |
| | | 168 | | { |
| | 0 | 169 | | throw new ArgumentException(SR.Format(SR.EnableLdapClaimResolutionMachine, nameof(MachineAccountPass |
| | | 170 | | } |
| | | 171 | | } |
| | 18 | 172 | | } |
| | | 173 | | |
| | | 174 | | private LdapConnection ConnectLDAP() |
| | | 175 | | { |
| | 0 | 176 | | if (EnableLdapClaimResolution) |
| | | 177 | | { |
| | 0 | 178 | | Validate(); |
| | 0 | 179 | | var di = new LdapDirectoryIdentifier(Servers, true, false); |
| | 0 | 180 | | if (string.IsNullOrEmpty(MachineAccountName)) |
| | | 181 | | { |
| | | 182 | | // Use default credentials |
| | 0 | 183 | | _ldapConnection = new LdapConnection(di, null, AuthType.Kerberos); |
| | | 184 | | } |
| | | 185 | | else |
| | | 186 | | { |
| | | 187 | | // Use specific specific machine account |
| | 0 | 188 | | var machineAccount = MachineAccountName + "@" + Domain; |
| | 0 | 189 | | var credentials = new NetworkCredential(machineAccount, MachineAccountPassword); |
| | 0 | 190 | | _ldapConnection = new LdapConnection(di, credentials); |
| | | 191 | | } |
| | 0 | 192 | | _ldapConnection.SessionOptions.ProtocolVersion = 3; //Setting LDAP Protocol to latest version |
| | 0 | 193 | | _ldapConnection.Timeout = TimeSpan.FromMinutes(1); |
| | | 194 | | |
| | 0 | 195 | | _ldapConnection.Bind(); // This line actually makes the connection. |
| | | 196 | | } |
| | 0 | 197 | | return _ldapConnection; |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | } |