Skip to content

Commit 541ad83

Browse files
authored
Merge branch 'development' into development
2 parents d1adf2f + e1fa4c6 commit 541ad83

File tree

63 files changed

+1727
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1727
-389
lines changed

samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
<ProjectReference Include="..\Autofac.Web\Autofac.Web.csproj" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<Folder Include="Properties\" />
21+
</ItemGroup>
22+
1923
</Project>

samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
1212
</ItemGroup>
1313

14+
15+
<ItemGroup>
16+
<Folder Include="Properties\" />
17+
</ItemGroup>
18+
1419
</Project>

samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Authentication
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Security.Claims;
7+
8+
/// <summary>
9+
/// Base class for creating mocked authenticated <see cref="ClaimsPrincipal"/>.
10+
/// </summary>
11+
public class BaseClaimsPrincipalUserBuilder : BaseUserBuilder
12+
{
13+
private readonly ICollection<ClaimsIdentity> identities;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="BaseClaimsPrincipalUserBuilder"/> class.
17+
/// </summary>
18+
public BaseClaimsPrincipalUserBuilder()
19+
=> this.identities = new List<ClaimsIdentity>();
20+
21+
/// <summary>
22+
/// Returns the principle based on provided identities and claims.
23+
/// </summary>
24+
/// <returns>This <see cref="ClaimsPrincipal"/>.</returns>
25+
public ClaimsPrincipal GetClaimsPrincipal()
26+
{
27+
var claimIdentities = this.identities.Reverse().ToList();
28+
claimIdentities.Add(this.GetAuthenticatedClaimsIdentity());
29+
30+
var claimsPrincipal = new ClaimsPrincipal(claimIdentities);
31+
32+
return claimsPrincipal;
33+
}
34+
35+
/// <summary>
36+
/// Returns the principle based on provided claims only.
37+
/// </summary>
38+
/// <returns>This <see cref="ClaimsPrincipal"/>.</returns>
39+
public ClaimsPrincipal GetClaimsPrincipalBasedOnClaimsOnly()
40+
{
41+
var claimsPrincipal = new ClaimsPrincipal(this.GetAuthenticatedClaimsIdentity());
42+
43+
return claimsPrincipal;
44+
}
45+
46+
/// <summary>
47+
/// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username.
48+
/// </summary>
49+
/// <returns>Authenticated <see cref="ClaimsPrincipal"/>.</returns>
50+
/// <value>Result of type <see cref="ClaimsPrincipal"/>.</value>
51+
public static ClaimsPrincipal DefaultAuthenticated { get; }
52+
= new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity());
53+
54+
protected void AddIdentity(ClaimsIdentity identity)
55+
=> this.identities.Add(identity);
56+
57+
protected void AddIdentities(IEnumerable<ClaimsIdentity> identities)
58+
{
59+
foreach (var identity in identities)
60+
{
61+
this.AddIdentity(identity);
62+
}
63+
}
64+
}
65+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
7474
/// Creates new authenticated claims identity by using the accumulated claims and authentication type.
7575
/// </summary>
7676
/// <returns>Mock of <see cref="ClaimsIdentity"/>.</returns>
77-
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
77+
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
7878
=> CreateAuthenticatedClaimsIdentity(
7979
this.claims,
8080
this.authenticationType,
@@ -97,51 +97,91 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
9797
/// Sets identifier claim to the built <see cref="ClaimsIdentity"/>.
9898
/// </summary>
9999
/// <param name="identifier">Value of the identifier claim - <see cref="ClaimTypes.NameIdentifier"/>.</param>
100-
protected void AddIdentifier(string identifier)
100+
protected void AddIdentifier(string identifier)
101101
=> this.AddClaim(ClaimTypes.NameIdentifier, identifier);
102102

103103
/// <summary>
104104
/// Sets username claims to the built <see cref="ClaimsIdentity"/>.
105105
/// </summary>
106106
/// <param name="username">Value of the username claim. Default claim type is <see cref="ClaimTypes.Name"/>.</param>
107-
protected void AddUsername(string username)
107+
protected void AddUsername(string username)
108108
=> this.AddClaim(this.nameType, username);
109109

110110
/// <summary>
111111
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
112112
/// </summary>
113113
/// <param name="claim">The <see cref="Claim"/> to add.</param>
114-
protected void AddClaim(Claim claim)
114+
protected void AddClaim(Claim claim)
115115
=> this.claims.Add(claim);
116116

117117
/// <summary>
118118
/// Adds claims to the built <see cref="ClaimsIdentity"/>.
119119
/// </summary>
120120
/// <param name="claims">Collection of <see cref="Claim"/> to add.</param>
121-
protected void AddClaims(IEnumerable<Claim> claims)
121+
protected void AddClaims(IEnumerable<Claim> claims)
122122
=> claims.ForEach(this.AddClaim);
123123

124124
/// <summary>
125125
/// Adds authentication type to the built <see cref="ClaimsIdentity"/>.
126126
/// </summary>
127127
/// <param name="authenticationType">Authentication type to add. Default is "Passport".</param>
128-
protected void AddAuthenticationType(string authenticationType)
128+
protected void AddAuthenticationType(string authenticationType)
129129
=> this.authenticationType = authenticationType;
130130

131131
/// <summary>
132132
/// Adds role to the built <see cref="ClaimsIdentity"/>.
133133
/// </summary>
134134
/// <param name="role">Value of the role claim. Default claim type is <see cref="ClaimTypes.Role"/>.</param>
135-
protected void AddRole(string role)
135+
protected void AddRole(string role)
136136
=> this.AddClaim(this.roleType, role);
137137

138138
/// <summary>
139139
/// Adds roles to the built <see cref="ClaimsIdentity"/>.
140140
/// </summary>
141141
/// <param name="roles">Collection of roles to add.</param>
142-
protected void AddRoles(IEnumerable<string> roles)
142+
protected void AddRoles(IEnumerable<string> roles)
143143
=> roles.ForEach(this.AddRole);
144144

145+
/// <summary>
146+
/// Removes provided <see cref="Claim"/> from the list of claims.
147+
/// </summary>
148+
/// <param name="claim">Claim to be removed.</param>
149+
protected void RemoveClaim(Claim claim)
150+
{
151+
if (this.claims.Contains(claim))
152+
{
153+
this.claims.Remove(claim);
154+
}
155+
}
156+
157+
/// <summary>
158+
/// Removes provided claim by its type and value from the list of claims.
159+
/// </summary>
160+
/// <param name="type">Claim's type.</param>
161+
/// <param name="value">Claim's value.</param>
162+
protected void RemoveClaim(string type, string value)
163+
{
164+
var claimsToRemove = this.claims
165+
.Where(x => x.Type.Equals(type) && x.Value.Equals(value))
166+
.ToList();
167+
168+
claimsToRemove.ForEach(claim => this.claims.Remove(claim));
169+
}
170+
171+
/// <summary>
172+
/// Remove claim by providing its role.
173+
/// </summary>
174+
/// <param name="role">Claim's role.</param>
175+
protected void RemoveRole(string role)
176+
=> this.RemoveClaim(this.roleType, role);
177+
178+
/// <summary>
179+
/// Remove claim by providing its username.
180+
/// </summary>
181+
/// <param name="username">Claim's username.</param>
182+
protected void RemoveUsername(string username)
183+
=> this.RemoveClaim(this.nameType, username);
184+
145185
/// <summary>
146186
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
147187
/// </summary>

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsPrincipalBuilder.cs

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)