forked from UniStuttgart-VISUS/Visus.LdapAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLdapUserBase.cs
328 lines (291 loc) · 13.4 KB
/
LdapUserBase.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// <copyright file="LdapUserBase.cs" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2021 - 2024 Visualisierungsinstitut der Universität Stuttgart.
// Licensed under the MIT licence. See LICENCE file for details.
// </copyright>
// <author>Christoph Müller</author>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.Protocols;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text.Json.Serialization;
namespace Visus.DirectoryAuthentication {
/// <summary>
/// Base class for implementing custom users.
/// </summary>
public abstract class LdapUserBase : ILdapUser {
#region Public properties
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "sAMAccountName")]
[LdapAttribute(Schema.IdentityManagementForUnix, "sAMAccountName")]
[LdapAttribute(Schema.Rfc2307, "uid")]
[Claim(ClaimTypes.Name)]
[Claim(ClaimTypes.WindowsAccountName)]
public virtual string AccountName { get; internal set; }
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "givenName")]
[LdapAttribute(Schema.IdentityManagementForUnix, "givenName")]
[LdapAttribute(Schema.Rfc2307, "givenName")]
[Claim(ClaimTypes.GivenName)]
public virtual string ChristianName { get; internal set; }
/// <inheritdoc />
public virtual IEnumerable<Claim> Claims { get; } = new List<Claim>();
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "displayName")]
[LdapAttribute(Schema.IdentityManagementForUnix, "displayName")]
[LdapAttribute(Schema.Rfc2307, "displayName")]
public virtual string DisplayName { get; internal set; }
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "mail")]
[LdapAttribute(Schema.IdentityManagementForUnix, "mail")]
[LdapAttribute(Schema.Rfc2307, "mail")]
[Claim(ClaimTypes.Email)]
public virtual string EmailAddress { get; internal set; }
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "objectSid",
Converter = typeof(SidConverter))]
[LdapAttribute(Schema.IdentityManagementForUnix, "uidNumber")]
[LdapAttribute(Schema.Rfc2307, "uidNumber")]
[Claim(ClaimTypes.PrimarySid)]
[Claim(ClaimTypes.Sid)]
[Claim(ClaimTypes.NameIdentifier)]
public virtual string Identity { get; internal set; }
/// <inheritdoc />
[JsonIgnore]
public virtual IEnumerable<string> RequiredAttributes {
get {
var retval = (from p in this.GetType().GetProperties()
let a = p.GetCustomAttributes<LdapAttributeAttribute>()
where (a != null) && a.Any()
select a.Select(aa => aa.Name))
.SelectMany(a => a)
.Distinct();
return retval;
}
}
/// <inheritdoc />
[LdapAttribute(Schema.ActiveDirectory, "sn")]
[LdapAttribute(Schema.IdentityManagementForUnix, "sn")]
[LdapAttribute(Schema.Rfc2307, "sn")]
[Claim(ClaimTypes.Surname)]
public virtual string Surname { get; internal set; }
#endregion
#region Public methods
/// <inheritdoc />
public void Assign(SearchResultEntry entry, LdapConnection connection,
ILdapOptions options) {
entry.AssignTo(this, options);
this.AddGroupClaims(entry, connection, options);
this.AddPropertyClaims(entry, connection, options);
}
#endregion
#region Protected class methods
/// <summary>
/// Gets the group memberships of the specified (user or group) LDAP
/// entry.
/// </summary>
/// <param name="entry">The entry to retrieve the group memberships of.
/// </param>
/// <param name="connection">An <see cref="LdapConnection"/> to retrieve
/// the details about the groups.</param>
/// <param name="options">The <see cref="ILdapOptions"/> configuring the
/// mapping of attributes.</param>
/// <returns>The LDAP entries for the groups <paramref name="entry"/> is
/// member of.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="entry"/>
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="connection"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="options"/> is <c>null</c>.</exception>
protected static IEnumerable<SearchResultEntry> GetGroupMemberships(
SearchResultEntry entry,
LdapConnection connection,
ILdapOptions options) {
_ = entry
?? throw new ArgumentNullException(nameof(entry));
_ = connection
?? throw new ArgumentNullException(nameof(connection));
_ = options
?? throw new ArgumentNullException(nameof(options));
var mapping = options.Mapping;
var groups = (string[]) null;
try {
var att = entry.GetAttribute(options.Mapping.GroupsAttribute);
if (att != null) {
groups = att.GetValues(typeof(string))
.Cast<string>()
.ToArray();
}
} catch /* TODO: More specific exception? */ {
// Entry has no group memberships.
yield break;
}
if (groups != null) {
Debug.WriteLine($"Determining details of {groups.Length} "
+ $"groups that \"{entry.DistinguishedName}\" is member "
+ "of.");
foreach (var g in groups) {
var q = g.EscapeLdapFilterExpression();
foreach (var b in options.SearchBase) {
var req = new SearchRequest(b.Key,
$"({mapping.DistinguishedNameAttribute}={q})",
SearchScope.Subtree,
mapping.RequiredGroupAttributes);
var res = connection.SendRequest(req, options);
if (res is SearchResponse r) {
foreach (SearchResultEntry e in r.Entries) {
yield return e;
}
}
}
}
} /* if (groups != null) */
}
/// <summary>
/// Gets the direct and transitive group memberships of the specified
/// (user or group) LDAP entry.
/// </summary>
/// <param name="entry">The entry to retrieve the group memberships of.
/// </param>
/// <param name="connection">An <see cref="LdapConnection"/> to retrieve
/// the details about the groups.</param>
/// <param name="options">The <see cref="ILdapOptions"/> configuring the
/// mapping of attributes.</param>
/// <returns>The LDAP entries for the groups <paramref name="entry"/> is
/// member of.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="entry"/>
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="connection"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="options"/> is <c>null</c>.</exception>
private IEnumerable<SearchResultEntry> GetRecursiveGroupMemberships(
SearchResultEntry entry,
LdapConnection connection,
ILdapOptions options) {
var stack = new Stack<SearchResultEntry>();
stack.Push(entry);
while (stack.Count > 0) {
foreach (var g in GetGroupMemberships(stack.Pop(), connection,
options)) {
stack.Push(g);
yield return g;
}
}
}
#endregion
#region Protected methods
/// <summary>
/// Adds <see cref="Claims"/> from group memberships of
/// <paramref name="entry"/>.
/// </summary>
/// <param name="entry"></param>
/// <param name="connection"></param>
/// <param name="schema"></param>
protected virtual void AddGroupClaims(SearchResultEntry entry,
LdapConnection connection, ILdapOptions options) {
_ = entry
?? throw new ArgumentNullException(nameof(entry));
_ = connection
?? throw new ArgumentNullException(nameof(connection));
_ = options
?? throw new ArgumentNullException(nameof(options));
var claims = (IList<Claim>) this.Claims;
var mapping = options.Mapping;
try {
var a = entry.GetAttribute(mapping.PrimaryGroupAttribute);
var gid = a.GetValue((ILdapAttributeConverter) null) as string;
var endOfDomain = this.Identity.LastIndexOf('-');
if (endOfDomain > 0) {
// If we have an actual SID for the user, assume an AD and
// convert the RID of the primary group to a SID using the
// domain part extracted from the user.
var domain = this.Identity.Substring(0, endOfDomain);
gid = $"{domain}-{gid}";
}
claims.Add(new Claim(ClaimTypes.PrimaryGroupSid, gid));
claims.Add(new Claim(ClaimTypes.GroupSid, gid));
} catch {
// Ignore missing group, just set no claim then.
Debug.WriteLine("Could not set primary group claims.");
}
{
var conv = mapping.GetGroupIdentityConverter();
var groups = options.IsRecursiveGroupMembership
? GetRecursiveGroupMemberships(entry, connection, options)
: GetGroupMemberships(entry, connection, options);
foreach (var g in groups) {
try {
var a = g.GetAttribute(mapping.GroupIdentityAttribute);
var gid = a.GetValue(conv) as string;
claims.Add(new Claim(ClaimTypes.GroupSid, gid));
} catch {
// Ignore issue, just set no claim.
Debug.WriteLine("Could not set group claims.");
}
}
}
}
/// <summary>
/// Adds <see cref="Claims"/> from the properties annotated with a
/// <see cref="ClaimAttribute"/>.
/// </summary>
/// <param name="entry"></param>
/// <param name="connection"></param>
/// <param name="schema"></param>
protected virtual void AddPropertyClaims(SearchResultEntry entry,
LdapConnection connection, ILdapOptions options) {
_ = entry
?? throw new ArgumentNullException(nameof(entry));
_ = connection
?? throw new ArgumentNullException(nameof(connection));
_ = options
?? throw new ArgumentNullException(nameof(options));
var claims = (IList<Claim>) this.Claims;
var mapped = from p in this.GetType().GetProperties()
let a = p.GetCustomAttributes<ClaimAttribute>()
where (p.PropertyType == typeof(string)) && (a != null) && a.Any()
select new {
Claims = a.Select(aa => aa.Name),
Value = (string) p.GetValue(this)
};
foreach (var m in mapped) {
foreach (var c in m.Claims) {
if (m.Value != null) {
Debug.WriteLine($"Adding {c} claim as \"{m.Value}\".");
claims.Add(new Claim(c, m.Value));
}
}
}
}
/// <summary>
/// Gets the name of the LDAP attribute where the given property is
/// stored.
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
protected string GetLdapAttribute(string schema,
[CallerMemberName] string propertyName = null) {
var prop = this.GetType().GetProperty(propertyName);
if (prop == null) {
var msg = Properties.Resources.ErrorPropertyNotFound;
msg = string.Format(msg, propertyName);
throw new ArgumentException(msg, nameof(propertyName));
}
var att = (from a in prop.GetCustomAttributes<LdapAttributeAttribute>()
where (a.Schema == schema)
select a).FirstOrDefault();
if (att == null) {
var msg = Properties.Resources.ErrorPropertyNotMapped;
msg = string.Format(msg, propertyName, schema);
throw new ArgumentException(msg, nameof(propertyName));
}
return att.Name;
}
#endregion
}
}