-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |