-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MobileAuthClaimsHandler.cs
60 lines (48 loc) · 2.59 KB
/
MobileAuthClaimsHandler.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
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
namespace AvantiPoint.MobileAuth;
public class MobileAuthClaimsHandler : IMobileAuthClaimsHandler
{
public virtual ValueTask<IEnumerable<Claim>> GenerateClaims(HttpContext context, AuthenticateResult auth, string scheme)
{
if (auth.Principal is null)
throw new NullReferenceException("The Authentication Result Principal is null.");
var claims = GetClaims(auth.Principal);
claims["provider"] = scheme;
if(auth.Properties is not null)
{
claims["access_token"] = auth.Properties.GetTokenValue("access_token") ?? string.Empty;
claims["id_token"] = auth.Properties.GetTokenValue("id_token") ?? string.Empty;
claims["refresh_token"] = auth.Properties.GetTokenValue("refresh_token") ?? string.Empty;
claims["expires_in"] = (auth.Properties.ExpiresUtc?.ToUnixTimeSeconds() ?? -1).ToString();
}
ConfigureName(ref claims);
return ValueTask.FromResult(claims.Where(x => !string.IsNullOrEmpty(x.Value) && x.Value != "-1")
.Select(x => new Claim(x.Key, x.Value)));
}
private static void ConfigureName(ref Dictionary<string, string> claims)
{
if (claims.TryGetValue("name", out var name) && !string.IsNullOrEmpty(name))
return;
else if (claims.TryGetValue("surname", out var surname) && claims.TryGetValue("given_name", out var givenname) && !string.IsNullOrEmpty(surname) && !string.IsNullOrEmpty(givenname))
claims["name"] = $"{givenname} {surname}".Trim();
}
private static Dictionary<string, string> GetClaims(ClaimsPrincipal principal)
{
var claims = new Dictionary<string, string>();
AddClaim(ref claims, "email", principal.FindFirstValue(ClaimTypes.Email));
AddClaim(ref claims, "name", principal.FindFirstValue(ClaimTypes.Name));
AddClaim(ref claims, "given_name", principal.FindFirstValue(ClaimTypes.GivenName));
AddClaim(ref claims, "surname", principal.FindFirstValue(ClaimTypes.Surname));
AddClaim(ref claims, "provider_id", principal.FindFirstValue(ClaimTypes.NameIdentifier));
return claims;
}
private static void AddClaim(ref Dictionary<string, string> claims, string claim, string? value)
{
if (!claims.ContainsKey(claim) && !string.IsNullOrEmpty(value))
claims[claim] = value;
}
protected static string? FindFirstValue(IEnumerable<Claim> claims, string type) =>
claims.FirstOrDefault(x => x.Type== type)?.Value;
}