Init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using maintenance.Authentication.Configuration;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class AdminRoleClaimsTransformation : IClaimsTransformation
|
||||
{
|
||||
private static readonly string[] GroupClaimTypes =
|
||||
{
|
||||
"groups",
|
||||
"group",
|
||||
ClaimTypes.GroupSid,
|
||||
"http://schemas.xmlsoap.org/claims/Group"
|
||||
};
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public AdminRoleClaimsTransformation(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
|
||||
{
|
||||
if (principal.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
}
|
||||
|
||||
if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == AuthRoles.Admin))
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
}
|
||||
|
||||
var configuredGroups = (_configuration[AuthEnvironment.AdminGroups] ?? string.Empty)
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (configuredGroups.Length == 0)
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
}
|
||||
|
||||
var principalGroups = identity.Claims
|
||||
.Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase))
|
||||
.Select(c => c.Value);
|
||||
|
||||
var isAdmin = principalGroups.Any(group =>
|
||||
configuredGroups.Contains(group, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
if (isAdmin)
|
||||
{
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, AuthRoles.Admin));
|
||||
}
|
||||
|
||||
return Task.FromResult(principal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public static class AuthRoles
|
||||
{
|
||||
public const string Admin = "Admin";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class IdentityContext
|
||||
{
|
||||
public required string UserId { get; init; }
|
||||
public required string DisplayName { get; init; }
|
||||
public required IReadOnlyCollection<string> Groups { get; init; }
|
||||
public required IReadOnlyCollection<string> Roles { get; init; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using maintenance.Authentication.Abstractions;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class IdentityContextFactory : IIdentityContextFactory
|
||||
{
|
||||
private static readonly string[] GroupClaimTypes =
|
||||
{
|
||||
"groups",
|
||||
"group",
|
||||
ClaimTypes.GroupSid,
|
||||
"http://schemas.xmlsoap.org/claims/Group"
|
||||
};
|
||||
|
||||
public IdentityContext Create(ClaimsPrincipal principal)
|
||||
{
|
||||
var groups = principal.Claims
|
||||
.Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase))
|
||||
.Select(c => c.Value)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var roles = principal.Claims
|
||||
.Where(c => c.Type == ClaimTypes.Role)
|
||||
.Select(c => c.Value)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var userId = principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? principal.FindFirstValue("sub")
|
||||
?? principal.Identity?.Name
|
||||
?? "unknown";
|
||||
|
||||
var displayName = principal.FindFirstValue(ClaimTypes.Name)
|
||||
?? principal.Identity?.Name
|
||||
?? userId;
|
||||
|
||||
return new IdentityContext
|
||||
{
|
||||
UserId = userId,
|
||||
DisplayName = displayName,
|
||||
Groups = groups,
|
||||
Roles = roles
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user