-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentityServerUser.cs
99 lines (82 loc) · 3.19 KB
/
IdentityServerUser.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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityModel;
using IdentityServer7.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace IdentityServer7
{
/// <summary>
/// Model properties of an IdentityServer user
/// </summary>
public class IdentityServerUser
{
/// <summary>
/// Subject ID (mandatory)
/// </summary>
public string SubjectId { get; }
/// <summary>
/// Display name (optional)
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// Identity provider (optional)
/// </summary>
public string IdentityProvider { get; set; }
/// <summary>
/// Authentication methods
/// </summary>
public ICollection<string> AuthenticationMethods { get; set; } = new HashSet<string>();
/// <summary>
/// Authentication time
/// </summary>
public DateTime? AuthenticationTime { get; set; }
/// <summary>
/// Additional claims
/// </summary>
public ICollection<Claim> AdditionalClaims { get; set; } = new HashSet<Claim>(new ClaimComparer());
/// <summary>
/// Initializes a user identity
/// </summary>
/// <param name="subjectId">The subject ID</param>
public IdentityServerUser(string subjectId)
{
if (subjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(subjectId));
SubjectId = subjectId;
}
/// <summary>
/// Creates an IdentityServer claims principal
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public ClaimsPrincipal CreatePrincipal()
{
if (SubjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(SubjectId));
var claims = new List<Claim> { new Claim(JwtClaimTypes.Subject, SubjectId) };
if (DisplayName.IsPresent())
{
claims.Add(new Claim(JwtClaimTypes.Name, DisplayName));
}
if (IdentityProvider.IsPresent())
{
claims.Add(new Claim(JwtClaimTypes.IdentityProvider, IdentityProvider));
}
if (AuthenticationTime.HasValue)
{
claims.Add(new Claim(JwtClaimTypes.AuthenticationTime, new DateTimeOffset(AuthenticationTime.Value).ToUnixTimeSeconds().ToString()));
}
if (AuthenticationMethods.Any())
{
foreach (var amr in AuthenticationMethods)
{
claims.Add(new Claim(JwtClaimTypes.AuthenticationMethod, amr));
}
}
claims.AddRange(AdditionalClaims);
var id = new ClaimsIdentity(claims.Distinct(new ClaimComparer()), Constants.IdentityServerAuthenticationType, JwtClaimTypes.Name, JwtClaimTypes.Role);
return new ClaimsPrincipal(id);
}
}
}