forked from CodeBeamOrg/UltimateAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialUserMappingBuilder.cs
More file actions
75 lines (62 loc) · 2.93 KB
/
CredentialUserMappingBuilder.cs
File metadata and controls
75 lines (62 loc) · 2.93 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
namespace CodeBeam.UltimateAuth.Credentials.EntityFrameworkCore
{
internal static class CredentialUserMappingBuilder
{
public static CredentialUserMapping<TUser, TUserId> Build<TUser, TUserId>(CredentialUserMappingOptions<TUser, TUserId> options)
{
if (options.UserId is null)
{
var expr = ConventionResolver.TryResolve<TUser, TUserId>("Id", "UserId");
if (expr != null)
options.ApplyUserId(expr);
}
if (options.Username is null)
{
var expr = ConventionResolver.TryResolve<TUser, string>(
"Username",
"UserName",
"Email",
"EmailAddress",
"Login");
if (expr != null)
options.ApplyUsername(expr);
}
// Never add "Password" as a convention to avoid accidental mapping to plaintext password properties
if (options.PasswordHash is null)
{
var expr = ConventionResolver.TryResolve<TUser, string>(
"PasswordHash",
"Passwordhash",
"PasswordHashV2");
if (expr != null)
options.ApplyPasswordHash(expr);
}
if (options.SecurityVersion is null)
{
var expr = ConventionResolver.TryResolve<TUser, long>(
"SecurityVersion",
"SecurityStamp",
"AuthVersion");
if (expr != null)
options.ApplySecurityVersion(expr);
}
if (options.UserId is null)
throw new InvalidOperationException("UserId mapping is required. Use MapUserId(...) or ensure a conventional property exists.");
if (options.Username is null)
throw new InvalidOperationException("Username mapping is required. Use MapUsername(...) or ensure a conventional property exists.");
if (options.PasswordHash is null)
throw new InvalidOperationException("PasswordHash mapping is required. Use MapPasswordHash(...) or ensure a conventional property exists.");
if (options.SecurityVersion is null)
throw new InvalidOperationException("SecurityVersion mapping is required. Use MapSecurityVersion(...) or ensure a conventional property exists.");
var canAuthenticateExpr = options.CanAuthenticate ?? (_ => true);
return new CredentialUserMapping<TUser, TUserId>
{
UserId = options.UserId.Compile(),
Username = options.Username.Compile(),
PasswordHash = options.PasswordHash.Compile(),
SecurityVersion = options.SecurityVersion.Compile(),
CanAuthenticate = canAuthenticateExpr.Compile()
};
}
}
}