-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserProvisioningService.cs
More file actions
355 lines (310 loc) · 16.5 KB
/
Copy pathUserProvisioningService.cs
File metadata and controls
355 lines (310 loc) · 16.5 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System.Security.Claims;
using Gatherstead.Api.Contracts.Invitations;
using Gatherstead.Api.Contracts.Responses;
using Gatherstead.Api.Security;
using Gatherstead.Api.Services.Membership;
using Gatherstead.Api.Services.Observability;
using Gatherstead.Data;
using Gatherstead.Data.Entities;
using Microsoft.EntityFrameworkCore;
namespace Gatherstead.Api.Services.Provisioning;
public class UserProvisioningService : IUserProvisioningService
{
private readonly GathersteadDbContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ISecurityEventLogger _securityEventLogger;
private readonly IAuthCache _authCache;
public UserProvisioningService(
GathersteadDbContext dbContext,
IHttpContextAccessor httpContextAccessor,
ISecurityEventLogger securityEventLogger,
IAuthCache authCache)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
_securityEventLogger = securityEventLogger ?? throw new ArgumentNullException(nameof(securityEventLogger));
_authCache = authCache ?? throw new ArgumentNullException(nameof(authCache));
}
public async Task<UserBootstrapResponse> BootstrapAsync(CancellationToken cancellationToken = default)
{
var response = new UserBootstrapResponse();
var httpContext = _httpContextAccessor.HttpContext;
var principal = httpContext?.User;
if (httpContext is null || principal?.Identity?.IsAuthenticated != true)
{
response.AddResponseMessage(MessageType.ERROR, "Not authenticated.");
return response;
}
var externalId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? principal.FindFirst("sub")?.Value;
if (string.IsNullOrEmpty(externalId))
{
response.AddResponseMessage(MessageType.ERROR, "Authenticated user is missing a required identifier claim.");
return response;
}
var email = ResolveEmail(principal);
var user = await _dbContext.Users
.FirstOrDefaultAsync(u => u.ExternalId == externalId, cancellationToken);
Guid userId;
if (user is null)
{
// An access token can outlive an account erasure. Refuse to re-provision (resurrect) a
// recently-erased identity until every pre-erasure token has expired; a fresh sign-up
// after that window provisions a brand-new account as usual.
var externalIdHash = ErasedAccount.HashExternalId(externalId);
var recentlyErased = await _dbContext.ErasedAccounts
.AnyAsync(t => t.ExternalIdHash == externalIdHash && t.ExpiresAt > DateTime.UtcNow, cancellationToken);
if (recentlyErased)
{
await _securityEventLogger.LogAsync(
SecurityEventType.AuthFailure,
SecurityEventSeverity.Warning,
resource: "User:erased",
detail: "{\"reason\":\"bootstrapAfterErasure\"}",
tenantId: null,
userId: null,
cancellationToken: cancellationToken);
response.AddResponseMessage(MessageType.ERROR, "This account was recently deleted.");
return response;
}
userId = Guid.NewGuid();
// Pre-seed the resolved user id so the auditing interceptor can stamp the brand-new
// (self-created) row before it becomes queryable.
httpContext.Items[HttpContextCurrentUserContext.CacheKey] = (Guid?)userId;
// DisplayName is seeded once here from the token "name" claim. Unlike Email (refreshed
// below on every login), it is never re-synced afterwards so an in-app edit survives.
_dbContext.Users.Add(new User
{
Id = userId,
ExternalId = externalId,
Email = email,
DisplayName = ResolveDisplayName(principal),
});
await _dbContext.SaveChangesAsync(cancellationToken);
}
else
{
userId = user.Id;
httpContext.Items[HttpContextCurrentUserContext.CacheKey] = (Guid?)userId;
if (!string.IsNullOrEmpty(email) && !string.Equals(user.Email, email, StringComparison.Ordinal))
{
user.Email = email;
await _dbContext.SaveChangesAsync(cancellationToken);
}
// Intentionally do NOT touch DisplayName here: it is app-owned after first login.
}
// Seed the cross-request ExternalId → UserId mapping now that the row is durable.
await _authCache.SetUserIdAsync(externalId, userId, cancellationToken);
var claimed = 0;
if (!string.IsNullOrEmpty(email))
claimed = await ClaimInvitationsAsync(userId, email, cancellationToken);
// Bootstrap is not tenant-scoped, so the global tenant filter would hide tenant rows.
// Drop only the tenant filter; soft-delete stays enforced (the explicit !IsDeleted is
// redundant defense-in-depth).
var tenants = await _dbContext.TenantUsers
.IgnoreQueryFilters([GathersteadDbContext.TenantFilter])
.AsNoTracking()
.Where(tu => tu.UserId == userId && !tu.IsDeleted)
.Select(tu => new BootstrapTenantDto(tu.TenantId, tu.Role))
.ToListAsync(cancellationToken);
// A brand-new (self-created) user is never an app admin; only an existing row can carry the flag.
var isAppAdmin = user?.IsAppAdmin ?? false;
response.SetSuccess(new UserBootstrapDto(userId, isAppAdmin, claimed, tenants));
return response;
}
public async Task<MeResponse> GetMeAsync(CancellationToken cancellationToken = default)
{
var response = new MeResponse();
var user = await ResolveCurrentUserAsync(response, cancellationToken);
if (user is null)
return response;
response.SetSuccess(new MeDto(user.Id, ResolveDisplayEmail(user), user.DisplayName));
return response;
}
public async Task<MeResponse> UpdateDisplayNameAsync(string displayName, CancellationToken cancellationToken = default)
{
var response = new MeResponse();
var user = await ResolveCurrentUserAsync(response, cancellationToken);
if (user is null)
return response;
var normalized = (displayName ?? string.Empty).Trim();
if (string.IsNullOrWhiteSpace(normalized))
{
response.AddResponseMessage(MessageType.ERROR, "Display name is required.");
return response;
}
if (normalized.Length > 256)
{
response.AddResponseMessage(MessageType.ERROR, "Display name must be 256 characters or fewer.");
return response;
}
user.DisplayName = normalized;
await _dbContext.SaveChangesAsync(cancellationToken);
response.SetSuccess(new MeDto(user.Id, ResolveDisplayEmail(user), user.DisplayName));
return response;
}
/// <summary>
/// Email to surface on the caller's own profile: the stored (verified) email when present,
/// otherwise the caller's raw token email claim. Display-only — never feeds invitation matching.
/// </summary>
private string? ResolveDisplayEmail(User user)
{
if (!string.IsNullOrEmpty(user.Email))
return user.Email;
var principal = _httpContextAccessor.HttpContext?.User;
return principal is null ? null : ResolveEmailClaim(principal);
}
/// <summary>
/// Loads the authenticated caller's tracked <c>User</c> row, adding an error message to the
/// response when the caller is unauthenticated, missing the subject claim, or has not yet been
/// provisioned (bootstrap runs at login, so a missing row is an exceptional state).
/// </summary>
private async Task<User?> ResolveCurrentUserAsync<T>(BaseEntityResponse<T> response, CancellationToken cancellationToken)
{
var httpContext = _httpContextAccessor.HttpContext;
var principal = httpContext?.User;
if (httpContext is null || principal?.Identity?.IsAuthenticated != true)
{
response.AddResponseMessage(MessageType.ERROR, "Not authenticated.");
return null;
}
var externalId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? principal.FindFirst("sub")?.Value;
if (string.IsNullOrEmpty(externalId))
{
response.AddResponseMessage(MessageType.ERROR, "Authenticated user is missing a required identifier claim.");
return null;
}
var user = await _dbContext.Users
.FirstOrDefaultAsync(u => u.ExternalId == externalId, cancellationToken);
if (user is null)
{
response.AddResponseMessage(MessageType.ERROR, "User has not been provisioned.");
return null;
}
// Pre-seed the resolved id so the auditing interceptor doesn't issue a lazy lookup against
// this same DbContext mid-SaveChanges (this route isn't tenant-scoped, so nothing else
// populates the cache first). Mirrors the brand-new-user path in BootstrapAsync.
httpContext.Items[HttpContextCurrentUserContext.CacheKey] = (Guid?)user.Id;
return user;
}
private async Task<int> ClaimInvitationsAsync(Guid userId, string email, CancellationToken cancellationToken)
{
// Invitations are matched by email across every tenant (claim runs before any tenant is
// resolved), so drop only the tenant filter; soft-delete stays enforced.
var pending = await _dbContext.Invitations
.IgnoreQueryFilters([GathersteadDbContext.TenantFilter])
.Include(i => i.Households)
.Where(i => i.Email == email && i.Status == InvitationStatus.Pending && !i.IsDeleted)
.ToListAsync(cancellationToken);
if (pending.Count == 0) return 0;
var now = DateTimeOffset.UtcNow;
foreach (var invite in pending)
{
await MembershipGrant.GrantAsync(
_dbContext, invite.TenantId, userId, invite.Role,
invite.Households.Select(h => (h.HouseholdId, h.Role)).ToList(),
cancellationToken, invite.LinkedMemberId);
invite.Status = InvitationStatus.Accepted;
invite.AcceptedByUserId = userId;
invite.AcceptedAt = now;
}
await _dbContext.SaveChangesAsync(cancellationToken);
// Evict any cached "no membership" role result for this user in each granted tenant so the
// newly-claimed access is visible on the user's next request rather than after the TTL.
foreach (var invite in pending)
{
await _authCache.InvalidateTenantUserAsync(invite.TenantId, userId, cancellationToken);
await _authCache.InvalidateHouseholdUsersAsync(invite.TenantId, userId, cancellationToken);
}
// Emit attribution events after the grant is durable. Logged separately so a logging failure
// can never roll back a membership grant. Invitee email is omitted (PII) — the invitation row
// referenced by id already carries it.
foreach (var invite in pending)
await LogInvitationAcceptedAsync(invite, userId, cancellationToken);
return pending.Count;
}
private Task LogInvitationAcceptedAsync(Invitation invite, Guid acceptedByUserId, CancellationToken cancellationToken)
{
// A self-grant (acceptor invited themselves) is the abuse vector worth surfacing for review.
var selfGrant = invite.InvitedByUserId == acceptedByUserId;
return _securityEventLogger.LogAsync(
SecurityEventType.InvitationAccepted,
selfGrant ? SecurityEventSeverity.Warning : SecurityEventSeverity.Info,
resource: $"Invitation:{invite.Id}",
detail: $"{{\"role\":\"{invite.Role}\",\"invitedByUserId\":{JsonId(invite.InvitedByUserId)},\"selfGrant\":{(selfGrant ? "true" : "false")}}}",
tenantId: invite.TenantId,
userId: acceptedByUserId,
cancellationToken: cancellationToken);
}
private static string JsonId(Guid? id) => id is null ? "null" : $"\"{id}\"";
/// <summary>
/// Resolves the caller's email for invitation matching. Auto-claiming grants real tenant
/// membership, so the address must be trustworthy. The API validates the token issuer
/// (<c>ValidateIssuer</c>/<c>ValidIssuer</c>, see Program.cs), so every principal here was issued
/// by our single configured Entra External ID tenant, which owns the account's email — verified
/// via email OTP at self-service sign-up, or asserted by the federated social IdP — and is not
/// self-assertable. We therefore trust the issuer's email and block only when the IdP
/// <em>explicitly</em> marks it unverified (see <see cref="IsEmailExplicitlyUnverified"/>),
/// returning null then so the user is still provisioned, just without auto-claim.
/// </summary>
private static string? ResolveEmail(ClaimsPrincipal principal)
{
if (IsEmailExplicitlyUnverified(principal))
return null;
return ResolveEmailClaim(principal);
}
/// <summary>
/// Reads the caller's email claim (normalized) with no verification gate. Prefers the dedicated
/// email claims, then falls back to <c>preferred_username</c> only when it is email-shaped —
/// Entra External ID email accounts carry the address there, and the frontend session uses the
/// same fallback. Safe for display of the caller's own profile; invitation matching stays gated
/// behind <see cref="ResolveEmail"/>.
/// </summary>
private static string? ResolveEmailClaim(ClaimsPrincipal principal)
{
var email = principal.FindFirst(ClaimTypes.Email)?.Value
?? principal.FindFirst("email")?.Value
?? principal.FindFirst("emails")?.Value;
if (string.IsNullOrWhiteSpace(email))
{
// preferred_username is not guaranteed to be an email, so only accept it when it looks like one.
var preferredUsername = principal.FindFirst("preferred_username")?.Value;
if (!string.IsNullOrWhiteSpace(preferredUsername) && preferredUsername.Contains('@'))
email = preferredUsername;
}
return string.IsNullOrWhiteSpace(email) ? null : email.Trim().ToLowerInvariant();
}
/// <summary>
/// Resolves the caller's display name from the token "name" claim (the Entra user-flow
/// "Display Name" attribute). Used only to seed <see cref="User.DisplayName"/> at first login;
/// returns null when absent so the seeded value stays empty rather than blank-padded.
/// </summary>
private static string? ResolveDisplayName(ClaimsPrincipal principal)
{
var name = principal.FindFirst("name")?.Value
?? principal.FindFirst(ClaimTypes.Name)?.Value;
return string.IsNullOrWhiteSpace(name) ? null : name.Trim();
}
/// <summary>
/// True only when the IdP <em>explicitly</em> asserts the email is not verified — a verification
/// claim is present and equal to <c>false</c>. Every present claim is checked, so a negative signal
/// can't be masked by another claim taking precedence. An absent claim is NOT treated as unverified:
/// the token issuer is validated upstream, so we trust the issuer's email rather than blocking every
/// login whose token omits the claim. Entra External ID doesn't emit these natively today; they are
/// honored for a generic-OIDC / custom-claims-provider signal or a future federated IdP.
/// <para>
/// <c>xms_edov</c> is intentionally NOT consulted: it reports email <em>domain-owner</em>
/// verification, which is <c>false</c> for consumer-domain email+password accounts even when the
/// mailbox was OTP-verified at sign-up — gating on it would reject legitimate users. Revisit if a
/// federated/social IdP is added, where <c>xms_edov</c> becomes a meaningful discriminator.
/// </para>
/// </summary>
private static bool IsEmailExplicitlyUnverified(ClaimsPrincipal principal)
{
var verificationClaims = principal.FindAll("email_verified")
.Concat(principal.FindAll("verified_email"));
return verificationClaims.Any(
c => string.Equals(c.Value, "false", StringComparison.OrdinalIgnoreCase));
}
}