Skip to content

Commit

Permalink
add context and identity context
Browse files Browse the repository at this point in the history
  • Loading branch information
vendyp committed Jan 21, 2024
1 parent 537690d commit a62789a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/MyLib.Abstractions/Contexts/IContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MyLib.Abstractions.Contexts;

public interface IContext
{
Guid RequestId { get; }
string? IpAddress { get; }
string? TraceId { get; }
string? UserAgent { get; }
IIdentityContext Identity { get; }
}
10 changes: 10 additions & 0 deletions src/MyLib.Abstractions/Contexts/IIdentityContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MyLib.Abstractions.Contexts;

public interface IIdentityContext
{
bool IsAuthenticated { get; }
public Guid Id { get; }
public string Username { get; }
List<string> Roles { get; }
Dictionary<string, IEnumerable<string>> Claims { get; }
}
12 changes: 12 additions & 0 deletions src/MyLib.Infrastructure/Contexts/Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MyLib.Abstractions.Contexts;

namespace MyLib.Infrastructure.Contexts;

internal sealed class Context : IContext
{
public Guid RequestId { get; } = Guid.NewGuid();
public string? TraceId { get; set; }
public string? IpAddress { get; set; }
public string? UserAgent { get; set; }
public IIdentityContext Identity { get; } = IdentityContext.Empty;
}
43 changes: 43 additions & 0 deletions src/MyLib.Infrastructure/Contexts/IdentityContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Security.Claims;
using MyLib.Abstractions.Contexts;

namespace MyLib.Infrastructure.Contexts;

internal sealed class IdentityContext : IIdentityContext
{
public bool IsAuthenticated { get; }
public Guid Id { get; set; }
public string Username { get; set; } = string.Empty;
public Dictionary<string, IEnumerable<string>> Claims { get; }
public List<string> Roles { get; }

public IdentityContext()
{
Roles = new List<string>();
Claims = new Dictionary<string, IEnumerable<string>>();
}

public IdentityContext(Guid id) : this()
{
Id = id;
}

public IdentityContext(ClaimsPrincipal principal) : this()
{
if (principal.Identity is null || string.IsNullOrWhiteSpace(principal.Identity.Name))
return;

IsAuthenticated = principal.Identity?.IsAuthenticated is true;

if (principal.Claims.Any(e => e.Type == ClaimTypes.Role))
foreach (var claim in principal.Claims.Where(e => e.Type == ClaimTypes.Role))
Roles.Add(claim.Value);

Id = new Guid(principal.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value!);
Username = principal.Claims.SingleOrDefault(x => x.Type == ClaimTypes.Name)?.Value!;
Claims = principal.Claims.GroupBy(x => x.Type)
.ToDictionary(x => x.Key, x => x.Select(c => c.Value.ToString()));
}

public static IIdentityContext Empty => new IdentityContext();
}
14 changes: 14 additions & 0 deletions tests/MyLib.Tests/Contexts/ContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MyLib.Infrastructure.Contexts;
using Shouldly;

namespace MyLib.Tests.Contexts;

public class ContextTests
{
[Fact]
public void Context_Ctor_ShouldBe_Ok()
{
var context = new Context();
context.RequestId.ShouldNotBe(Guid.Empty);
}
}
49 changes: 49 additions & 0 deletions tests/MyLib.Tests/Contexts/IdentityContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Security.Claims;
using MyLib.Infrastructure.Contexts;
using Shouldly;

namespace MyLib.Tests.Contexts;

public class IdentityContextTests
{
[Fact]
public void IdentityContext_Ctor_ShouldBe_Ok()
{
var identityContext = new IdentityContext();
identityContext.Claims.ShouldNotBeNull();
identityContext.Roles.ShouldNotBeNull();
}

[Fact]
public void IdentityContext_Ctor_First_ShouldBe_Ok()
{
var id = Guid.NewGuid();
var identityContext = new IdentityContext(id);
identityContext.Id.ShouldBe(id);
}

[Fact]
public void IdentityContext_Ctor_Second_ShouldBe_Ok()
{
var id = Guid.NewGuid();
var name = "Test1234";
var list = new List<Claim>();
list.Add(new Claim(ClaimTypes.NameIdentifier, id.ToString()));
list.Add(new Claim(ClaimTypes.Name, name));

var listRole = new List<Claim>();
listRole.Add(new Claim(ClaimTypes.Role, "Role1"));
listRole.Add(new Claim(ClaimTypes.Role, "Role2"));
listRole.Add(new Claim(ClaimTypes.Role, "Role3"));
list.AddRange(listRole);

var claimIdentity = new ClaimsIdentity(list, "test");
var claimPrincipal = new ClaimsPrincipal(claimIdentity);
var identityContext = new IdentityContext(claimPrincipal);

identityContext.IsAuthenticated.ShouldBeTrue();
identityContext.Claims[ClaimTypes.Role].ToList().ShouldBeEquivalentTo(listRole.Select(e => e.Value).ToList());
identityContext.Id.ShouldBe(id);
identityContext.Username.ShouldBe(name);
}
}

0 comments on commit a62789a

Please sign in to comment.