(feature): custom jwt acl entry claim support
This commit is contained in:
parent
ab7fc58406
commit
d121f045bd
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<!-- NuGet package metadata -->
|
<!-- NuGet package metadata -->
|
||||||
<PackageId>MaksIT.Core</PackageId>
|
<PackageId>MaksIT.Core</PackageId>
|
||||||
<Version>1.4.5</Version>
|
<Version>1.4.6</Version>
|
||||||
<Authors>Maksym Sadovnychyy</Authors>
|
<Authors>Maksym Sadovnychyy</Authors>
|
||||||
<Company>MAKS-IT</Company>
|
<Company>MAKS-IT</Company>
|
||||||
<Product>MaksIT.Core</Product>
|
<Product>MaksIT.Core</Product>
|
||||||
|
|||||||
12
src/MaksIT.Core/Security/JWT/CustomClaims.cs
Normal file
12
src/MaksIT.Core/Security/JWT/CustomClaims.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using MaksIT.Core.Abstractions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MaksIT.Core.Security.JWT;
|
||||||
|
public class CustomClaims : Enumeration {
|
||||||
|
public static readonly CustomClaims AclEntry = new(1, "acl_entry");
|
||||||
|
private CustomClaims(int id, string name) : base(id, name) { }
|
||||||
|
}
|
||||||
@ -17,6 +17,8 @@ public class JWTTokenClaims {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string>? Roles { get; set; }
|
public List<string>? Roles { get; set; }
|
||||||
|
|
||||||
|
public List<string>? AclEntries { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the date and time when the token was issued.
|
/// Gets or sets the date and time when the token was issued.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -37,4 +37,7 @@ public class JWTTokenGenerateRequest {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string>? Roles { get; set; }
|
public List<string>? Roles { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public List<string>? AclEntries { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -8,8 +8,10 @@ using System.Text;
|
|||||||
|
|
||||||
namespace MaksIT.Core.Security.JWT;
|
namespace MaksIT.Core.Security.JWT;
|
||||||
|
|
||||||
public static class JwtGenerator {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static class JwtGenerator {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to generate a JWT token using the specified request parameters.
|
/// Attempts to generate a JWT token using the specified request parameters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -49,6 +51,9 @@ public static class JwtGenerator {
|
|||||||
if (request.Roles !=null)
|
if (request.Roles !=null)
|
||||||
claims.AddRange(request.Roles.Select(role => new Claim(ClaimTypes.Role, role)));
|
claims.AddRange(request.Roles.Select(role => new Claim(ClaimTypes.Role, role)));
|
||||||
|
|
||||||
|
if (request.AclEntries != null)
|
||||||
|
claims.AddRange(request.AclEntries.Select(acl => new Claim(CustomClaims.AclEntry.Name, acl)));
|
||||||
|
|
||||||
var tokenDescriptor = new JwtSecurityToken(
|
var tokenDescriptor = new JwtSecurityToken(
|
||||||
issuer: request.Issuer,
|
issuer: request.Issuer,
|
||||||
audience: request.Audience,
|
audience: request.Audience,
|
||||||
@ -141,6 +146,7 @@ public static class JwtGenerator {
|
|||||||
|
|
||||||
var username = principal.Identity?.Name;
|
var username = principal.Identity?.Name;
|
||||||
var roles = principal.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList();
|
var roles = principal.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList();
|
||||||
|
var aclEntries = principal.Claims.Where(c => c.Type == CustomClaims.AclEntry.Name).Select(c => c.Value).ToList();
|
||||||
|
|
||||||
var issuedAtClaim = principal.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Iat)?.Value;
|
var issuedAtClaim = principal.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Iat)?.Value;
|
||||||
var expiresAtClaim = principal.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Exp)?.Value;
|
var expiresAtClaim = principal.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Exp)?.Value;
|
||||||
@ -152,6 +158,7 @@ public static class JwtGenerator {
|
|||||||
UserId = userId,
|
UserId = userId,
|
||||||
Username = username,
|
Username = username,
|
||||||
Roles = roles,
|
Roles = roles,
|
||||||
|
AclEntries = aclEntries,
|
||||||
IssuedAt = issuedAt,
|
IssuedAt = issuedAt,
|
||||||
ExpiresAt = expiresAt
|
ExpiresAt = expiresAt
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user