5
5
using System . Security . Claims ;
6
6
using Utilities . Extensions ;
7
7
8
+ /// <summary>
9
+ /// Base class for creating mocked authenticated <see cref="ClaimsIdentity"/>.
10
+ /// </summary>
8
11
public abstract class BaseUserBuilder
9
12
{
13
+ /// <summary>
14
+ /// Default identifier (Id) of the built <see cref="ClaimsIdentity"/> - "TestId".
15
+ /// </summary>
10
16
protected const string DefaultIdentifier = "TestId" ;
17
+
18
+ /// <summary>
19
+ /// Default username of the built <see cref="ClaimsIdentity"/> - "TestUser".
20
+ /// </summary>
11
21
protected const string DefaultUsername = "TestUser" ;
22
+
23
+ /// <summary>
24
+ /// Default authentication type of the built <see cref="ClaimsIdentity"/> - "Passport".
25
+ /// </summary>
12
26
protected const string DefaultAuthenticationType = "Passport" ;
27
+
28
+ /// <summary>
29
+ /// Default type of the name claim - <see cref="ClaimTypes.Name"/>.
30
+ /// </summary>
13
31
protected const string DefaultNameType = ClaimTypes . Name ;
32
+
33
+ /// <summary>
34
+ /// Default type of the role claim - <see cref="ClaimTypes.Role"/>.
35
+ /// </summary>
14
36
protected const string DefaultRoleType = ClaimTypes . Role ;
15
37
16
38
private readonly ICollection < Claim > claims ;
@@ -19,6 +41,9 @@ public abstract class BaseUserBuilder
19
41
private string nameType ;
20
42
private string roleType ;
21
43
44
+ /// <summary>
45
+ /// Initializes a new instance of the <see cref="BaseUserBuilder" /> class.
46
+ /// </summary>
22
47
protected BaseUserBuilder ( )
23
48
{
24
49
this . authenticationType = DefaultAuthenticationType ;
@@ -27,7 +52,15 @@ protected BaseUserBuilder()
27
52
28
53
this . claims = new List < Claim > ( ) ;
29
54
}
30
-
55
+
56
+ /// <summary>
57
+ /// Creates new authenticated claims identity by using the provided claims and authentication type.
58
+ /// </summary>
59
+ /// <param name="claims">Collection of claims to add to the authenticated identity.</param>
60
+ /// <param name="authenticationType">Authentication type to use for the authenticated identity.</param>
61
+ /// <param name="nameType">Type of the username claim. Default is <see cref="ClaimTypes.Name"/>.</param>
62
+ /// <param name="roleType">Type of the role claim. Default is <see cref="ClaimTypes.Role"/>.</param>
63
+ /// <returns>Mocked <see cref="ClaimsIdentity"/>.</returns>
31
64
protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity (
32
65
ICollection < Claim > claims = null ,
33
66
string authenticationType = null ,
@@ -52,6 +85,10 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
52
85
return new ClaimsIdentity ( claims , authenticationType , nameType , roleType ) ;
53
86
}
54
87
88
+ /// <summary>
89
+ /// Creates new authenticated claims identity by using the accumulated claims and authentication type.
90
+ /// </summary>
91
+ /// <returns>Mocked <see cref="ClaimsIdentity"/>.</returns>
55
92
protected ClaimsIdentity GetAuthenticatedClaimsIdentity ( )
56
93
{
57
94
return CreateAuthenticatedClaimsIdentity (
@@ -61,51 +98,92 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
61
98
this . roleType ) ;
62
99
}
63
100
101
+ /// <summary>
102
+ /// Sets type of the username claim. Default is <see cref="ClaimTypes.Name"/>.
103
+ /// </summary>
104
+ /// <param name="nameType">Type to set on the username claim.</param>
64
105
protected void AddNameType ( string nameType )
65
106
{
66
107
this . nameType = nameType ;
67
108
}
68
109
110
+ /// <summary>
111
+ /// Sets type of the role claim. Default is <see cref="ClaimTypes.Role"/>.
112
+ /// </summary>
113
+ /// <param name="roleType">Type to set on the role claim.</param>
69
114
protected void AddRoleType ( string roleType )
70
115
{
71
116
this . roleType = roleType ;
72
117
}
73
118
119
+ /// <summary>
120
+ /// Sets identifier claim to the built <see cref="ClaimsIdentity"/>.
121
+ /// </summary>
122
+ /// <param name="identifier">Value of the identifier claim - <see cref="ClaimTypes.NameIdentifier"/>.</param>
74
123
protected void AddIdentifier ( string identifier )
75
124
{
76
125
this . AddClaim ( ClaimTypes . NameIdentifier , identifier ) ;
77
126
}
78
127
128
+ /// <summary>
129
+ /// Sets username claims to the built <see cref="ClaimsIdentity"/>.
130
+ /// </summary>
131
+ /// <param name="username">Value of the username claim. Default claim type is <see cref="ClaimTypes.Name"/>.</param>
79
132
protected void AddUsername ( string username )
80
133
{
81
134
this . AddClaim ( this . nameType , username ) ;
82
135
}
83
136
137
+ /// <summary>
138
+ /// Adds claim to the built <see cref="ClaimsIdentity"/>.
139
+ /// </summary>
140
+ /// <param name="claim">The <see cref="Claim"/> to add.</param>
84
141
protected void AddClaim ( Claim claim )
85
142
{
86
143
this . claims . Add ( claim ) ;
87
144
}
88
145
146
+ /// <summary>
147
+ /// Adds claims to the built <see cref="ClaimsIdentity"/>.
148
+ /// </summary>
149
+ /// <param name="claims">Enumerable of <see cref="Claim"/> to add.</param>
89
150
protected void AddClaims ( IEnumerable < Claim > claims )
90
151
{
91
152
claims . ForEach ( c => this . AddClaim ( c ) ) ;
92
153
}
93
154
155
+ /// <summary>
156
+ /// Adds authentication type to the built <see cref="ClaimsIdentity"/>.
157
+ /// </summary>
158
+ /// <param name="authenticationType">Authentication type to add. Default is "Passport".</param>
94
159
protected void AddAuthenticationType ( string authenticationType )
95
160
{
96
161
this . authenticationType = authenticationType ;
97
162
}
98
163
164
+ /// <summary>
165
+ /// Adds role to the built <see cref="ClaimsIdentity"/>.
166
+ /// </summary>
167
+ /// <param name="role">Value of the role claim. Default claim type is <see cref="ClaimTypes.Role"/>.</param>
99
168
protected void AddRole ( string role )
100
169
{
101
170
this . AddClaim ( this . roleType , role ) ;
102
171
}
103
172
173
+ /// <summary>
174
+ /// Adds roles to the built <see cref="ClaimsIdentity"/>.
175
+ /// </summary>
176
+ /// <param name="roles">Enumerable of roles to add.</param>
104
177
protected void AddRoles ( IEnumerable < string > roles )
105
178
{
106
179
roles . ForEach ( r => this . AddRole ( r ) ) ;
107
180
}
108
181
182
+ /// <summary>
183
+ /// Adds claim to the built <see cref="ClaimsIdentity"/>.
184
+ /// </summary>
185
+ /// <param name="type">Type of the claim to add.</param>
186
+ /// <param name="value">Value of the claim to add.</param>
109
187
private void AddClaim ( string type , string value )
110
188
{
111
189
this . claims . Add ( new Claim ( type , value ) ) ;
0 commit comments