Skip to content

Commit

Permalink
#283 "Without.." implementations for ModelState, Model, User, Distrib…
Browse files Browse the repository at this point in the history
…uted Cache (#365)

* #283 Initial implementation for without ModelState builder.

* #283 Renaming of all related classes and interfaces to "With and without" model state.

* #283 Adding xml comments and missing ComponentBuilderModelStateWithoutExtensions method for removing model state by key, without using the builder.

* #283  Adding tests for without model state implementation.

* #283 Rearranging the usings.

* #283 Changing the interfaces and builders name to be consistent with the functionallity for With And WithoutUser.

* #283 Adding Interfaces for without claims identity builder.

* #283 WIthoutTempData class naming refactoring, adding the class shells without the implementation.

* #283 Adding WithoutTempDataBuilder implementation.

* #283 Adding ComponentBuilderTempDataWithoutExtensions implementation.
Things left to be done:
- Add xml documentation
- Write tests

* #283 Adding xml documentation for all public methods and classes.

* #283 Adding tests for without temp data builder

* #283 Adding initial structure of the with and without distributed cache.
- Tidying up the structure of the memory cache and distributed cache folders in order to be more clear what and from where is used.

* #283 Adding xml documentation for the interfaces and public/protected methods.

* #283 Renaming several interfaces in order to match their corresponsing implementations.

Added tests for without distributed cache logic.
Added extension methods for the distributed cache component builder.

* #283 Adding comments for the component builder distributed cache.

* #283 Removing without claims identity builder in order for the PR to go to the main branch.

* #283 Removing wrongly commited launchSettings.json files.

* #283 Adding small refactoring of the authentication builders in order to continue with the WithoutUser implementation.

* #283 Adding couple of tests for WithoutUser

* #283 Added missed inferface method that was removed by mistake.

* #283 Fixing broken build.

* #283 Sets comments where such are missing.
- Fixing ignored test, added one more.

* Updated README

* Update README

* Updated README

* #365, #283 Resolving PR comments
  • Loading branch information
vaptsarov authored Apr 21, 2021
1 parent 80e4d48 commit 859ee57
Show file tree
Hide file tree
Showing 63 changed files with 1,727 additions and 389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<ProjectReference Include="..\Autofac.Web\Autofac.Web.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
</ItemGroup>


<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace MyTested.AspNetCore.Mvc.Builders.Authentication
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

/// <summary>
/// Base class for creating mocked authenticated <see cref="ClaimsPrincipal"/>.
/// </summary>
public class BaseClaimsPrincipalUserBuilder : BaseUserBuilder
{
private readonly ICollection<ClaimsIdentity> identities;

/// <summary>
/// Initializes a new instance of the <see cref="BaseClaimsPrincipalUserBuilder"/> class.
/// </summary>
public BaseClaimsPrincipalUserBuilder()
=> this.identities = new List<ClaimsIdentity>();

/// <summary>
/// Returns the principle based on provided identities and claims.
/// </summary>
/// <returns>This <see cref="ClaimsPrincipal"/>.</returns>
public ClaimsPrincipal GetClaimsPrincipal()
{
var claimIdentities = this.identities.Reverse().ToList();
claimIdentities.Add(this.GetAuthenticatedClaimsIdentity());

var claimsPrincipal = new ClaimsPrincipal(claimIdentities);

return claimsPrincipal;
}

/// <summary>
/// Returns the principle based on provided claims only.
/// </summary>
/// <returns>This <see cref="ClaimsPrincipal"/>.</returns>
public ClaimsPrincipal GetClaimsPrincipalBasedOnClaimsOnly()
{
var claimsPrincipal = new ClaimsPrincipal(this.GetAuthenticatedClaimsIdentity());

return claimsPrincipal;
}

/// <summary>
/// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username.
/// </summary>
/// <returns>Authenticated <see cref="ClaimsPrincipal"/>.</returns>
/// <value>Result of type <see cref="ClaimsPrincipal"/>.</value>
public static ClaimsPrincipal DefaultAuthenticated { get; }
= new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity());

protected void AddIdentity(ClaimsIdentity identity)
=> this.identities.Add(identity);

protected void AddIdentities(IEnumerable<ClaimsIdentity> identities)
{
foreach (var identity in identities)
{
this.AddIdentity(identity);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
/// Creates new authenticated claims identity by using the accumulated claims and authentication type.
/// </summary>
/// <returns>Mock of <see cref="ClaimsIdentity"/>.</returns>
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
=> CreateAuthenticatedClaimsIdentity(
this.claims,
this.authenticationType,
Expand All @@ -97,51 +97,91 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
/// Sets identifier claim to the built <see cref="ClaimsIdentity"/>.
/// </summary>
/// <param name="identifier">Value of the identifier claim - <see cref="ClaimTypes.NameIdentifier"/>.</param>
protected void AddIdentifier(string identifier)
protected void AddIdentifier(string identifier)
=> this.AddClaim(ClaimTypes.NameIdentifier, identifier);

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

/// <summary>
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
/// </summary>
/// <param name="claim">The <see cref="Claim"/> to add.</param>
protected void AddClaim(Claim claim)
protected void AddClaim(Claim claim)
=> this.claims.Add(claim);

/// <summary>
/// Adds claims to the built <see cref="ClaimsIdentity"/>.
/// </summary>
/// <param name="claims">Collection of <see cref="Claim"/> to add.</param>
protected void AddClaims(IEnumerable<Claim> claims)
protected void AddClaims(IEnumerable<Claim> claims)
=> claims.ForEach(this.AddClaim);

/// <summary>
/// Adds authentication type to the built <see cref="ClaimsIdentity"/>.
/// </summary>
/// <param name="authenticationType">Authentication type to add. Default is "Passport".</param>
protected void AddAuthenticationType(string authenticationType)
protected void AddAuthenticationType(string authenticationType)
=> this.authenticationType = authenticationType;

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

/// <summary>
/// Adds roles to the built <see cref="ClaimsIdentity"/>.
/// </summary>
/// <param name="roles">Collection of roles to add.</param>
protected void AddRoles(IEnumerable<string> roles)
protected void AddRoles(IEnumerable<string> roles)
=> roles.ForEach(this.AddRole);

/// <summary>
/// Removes provided <see cref="Claim"/> from the list of claims.
/// </summary>
/// <param name="claim">Claim to be removed.</param>
protected void RemoveClaim(Claim claim)
{
if (this.claims.Contains(claim))
{
this.claims.Remove(claim);
}
}

/// <summary>
/// Removes provided claim by its type and value from the list of claims.
/// </summary>
/// <param name="type">Claim's type.</param>
/// <param name="value">Claim's value.</param>
protected void RemoveClaim(string type, string value)
{
var claimsToRemove = this.claims
.Where(x => x.Type.Equals(type) && x.Value.Equals(value))
.ToList();

claimsToRemove.ForEach(claim => this.claims.Remove(claim));
}

/// <summary>
/// Remove claim by providing its role.
/// </summary>
/// <param name="role">Claim's role.</param>
protected void RemoveRole(string role)
=> this.RemoveClaim(this.roleType, role);

/// <summary>
/// Remove claim by providing its username.
/// </summary>
/// <param name="username">Claim's username.</param>
protected void RemoveUsername(string username)
=> this.RemoveClaim(this.nameType, username);

/// <summary>
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
/// </summary>
Expand Down

This file was deleted.

Loading

0 comments on commit 859ee57

Please sign in to comment.