-
Notifications
You must be signed in to change notification settings - Fork 462
/
DefaultIdentityFactory.cs
44 lines (38 loc) · 1.8 KB
/
DefaultIdentityFactory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.Extensions.Options;
namespace JWT.Extensions.AspNetCore.Factories
{
public sealed class DefaultIdentityFactory : IIdentityFactory
{
private readonly IOptionsMonitor<JwtAuthenticationOptions> _options;
public DefaultIdentityFactory(IOptionsMonitor<JwtAuthenticationOptions> options) =>
_options = options ?? throw new ArgumentNullException(nameof(options));
IIdentity IIdentityFactory.CreateIdentity(Type type, object payload)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
if (payload is null)
throw new ArgumentException(nameof(payload));
Type targetType = typeof(IDictionary<string, object>);
if (!targetType.IsAssignableFrom(type))
throw new ArgumentOutOfRangeException(nameof(type), $"Type {type} is not assignable to {targetType}");
return CreateIdentity((IDictionary<string, object>)payload);
}
/// <summary>
/// Creates user's identity from user's claims
/// </summary>
/// <param name="payload"><see cref="IDictionary{String,String}" /> of user's claims</param>
/// <returns><see cref="ClaimsIdentity" /></returns>
public IIdentity CreateIdentity(IDictionary<string, object> payload)
{
var claims = payload.Select(p => new Claim(p.Key, p.Value.ToString()));
return _options.CurrentValue.IncludeAuthenticationScheme ?
new ClaimsIdentity(claims, JwtAuthenticationDefaults.AuthenticationScheme) :
new ClaimsIdentity(claims);
}
}
}