-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.net 8 migration and refactorings (#201)
* .NET 8 * .NET 8 * Setup of Identity without external dependencies * Update README.md * Update dotnet-core.yml
- Loading branch information
1 parent
4c348d6
commit dcbe058
Showing
59 changed files
with
1,093 additions
and
354 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
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
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>disable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Equinox.Domain\Equinox.Domain.csproj"/> | ||
<ProjectReference Include="..\Equinox.Infra.Data\Equinox.Infra.Data.csproj"/> | ||
<ProjectReference Include="..\Equinox.Domain\Equinox.Domain.csproj" /> | ||
<ProjectReference Include="..\Equinox.Infra.Data\Equinox.Infra.Data.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="11.0.1"/> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
</ItemGroup> | ||
</Project> |
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
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
5 changes: 3 additions & 2 deletions
5
src/Equinox.Infra.CrossCutting.Bus/Equinox.Infra.CrossCutting.Bus.csproj
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
10 changes: 10 additions & 0 deletions
10
src/Equinox.Infra.CrossCutting.Identity/API/AppJwtSettings.cs
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 Equinox.Infra.CrossCutting.Identity.API | ||
{ | ||
public class AppJwtSettings | ||
{ | ||
public string SecretKey { get; set; } | ||
public int Expiration { get; set; } = 1; | ||
public string Issuer { get; set; } = "Equinox.Api"; | ||
public string Audience { get; set; } = "Api"; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/Equinox.Infra.CrossCutting.Identity/API/JwtBuilder.cs
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,117 @@ | ||
using Equinox.Infra.CrossCutting.Identity.Models; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
|
||
namespace Equinox.Infra.CrossCutting.Identity.API | ||
{ | ||
public class JwtBuilder<TIdentityUser, TKey> where TIdentityUser : IdentityUser<TKey> where TKey : IEquatable<TKey> | ||
{ | ||
private UserManager<TIdentityUser> _userManager; | ||
private AppJwtSettings _appJwtSettings; | ||
private TIdentityUser _user; | ||
private ICollection<Claim> _userClaims; | ||
private ICollection<Claim> _jwtClaims; | ||
private ClaimsIdentity _identityClaims; | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithUserManager(UserManager<TIdentityUser> userManager) | ||
{ | ||
_userManager = userManager ?? throw new ArgumentException(nameof(userManager)); | ||
return this; | ||
} | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithJwtSettings(AppJwtSettings appJwtSettings) | ||
{ | ||
_appJwtSettings = appJwtSettings ?? throw new ArgumentException(nameof(appJwtSettings)); | ||
return this; | ||
} | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithEmail(string email) | ||
{ | ||
if (string.IsNullOrEmpty(email)) throw new ArgumentException(nameof(email)); | ||
|
||
_user = _userManager.FindByEmailAsync(email).Result; | ||
_userClaims = new List<Claim>(); | ||
_jwtClaims = new List<Claim>(); | ||
_identityClaims = new ClaimsIdentity(); | ||
|
||
return this; | ||
} | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithJwtClaims() | ||
{ | ||
_jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, _user.Id.ToString())); | ||
_jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Email, _user.Email)); | ||
_jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); | ||
_jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Nbf, ToUnixEpochDate(DateTime.UtcNow).ToString())); | ||
_jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(DateTime.UtcNow).ToString(), ClaimValueTypes.Integer64)); | ||
|
||
_identityClaims.AddClaims(_jwtClaims); | ||
|
||
return this; | ||
} | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithUserClaims() | ||
{ | ||
_userClaims = _userManager.GetClaimsAsync(_user).Result; | ||
_identityClaims.AddClaims(_userClaims); | ||
|
||
return this; | ||
} | ||
|
||
public JwtBuilder<TIdentityUser, TKey> WithUserRoles() | ||
{ | ||
var userRoles = _userManager.GetRolesAsync(_user).Result; | ||
userRoles.ToList().ForEach(r => _identityClaims.AddClaim(new Claim("role", r))); | ||
|
||
return this; | ||
} | ||
|
||
public string BuildToken() | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var key = Encoding.ASCII.GetBytes(_appJwtSettings.SecretKey); | ||
var token = tokenHandler.CreateToken(new SecurityTokenDescriptor | ||
{ | ||
Issuer = _appJwtSettings.Issuer, | ||
Audience = _appJwtSettings.Audience, | ||
Subject = _identityClaims, | ||
Expires = DateTime.UtcNow.AddHours(_appJwtSettings.Expiration), | ||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), | ||
SecurityAlgorithms.HmacSha256Signature) | ||
}); | ||
|
||
return tokenHandler.WriteToken(token); | ||
} | ||
|
||
public UserResponse BuildUserResponse() | ||
{ | ||
var user = new UserResponse | ||
{ | ||
AccessToken = BuildToken(), | ||
ExpiresIn = TimeSpan.FromHours(_appJwtSettings.Expiration).TotalSeconds, | ||
UserToken = new UserToken | ||
{ | ||
Id = _user.Id, | ||
Email = _user.Email, | ||
Claims = _userClaims.Select(c => new UserClaim { Type = c.Type, Value = c.Value }) | ||
} | ||
}; | ||
|
||
return user; | ||
} | ||
|
||
private static long ToUnixEpochDate(DateTime date) | ||
=> (long)Math.Round((date.ToUniversalTime() - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)) | ||
.TotalSeconds); | ||
} | ||
|
||
public class JwtBuilder<TIdentityUser> : JwtBuilder<TIdentityUser, string> where TIdentityUser : IdentityUser<string> { } | ||
|
||
public sealed class JwtBuilder : JwtBuilder<IdentityUser> { } | ||
} |
25 changes: 0 additions & 25 deletions
25
src/Equinox.Infra.CrossCutting.Identity/ApiIdentityConfig.cs
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
src/Equinox.Infra.CrossCutting.Identity/Authorization/CustomAuthorizationValidation.cs
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,17 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Equinox.Infra.CrossCutting.Identity.Authorization | ||
{ | ||
public static class CustomAuthorizationValidation | ||
{ | ||
public static bool UserHasValidClaim(HttpContext context, string claimName, string claimValue) | ||
{ | ||
return context.User.Identity.IsAuthenticated && | ||
context.User.Claims.Any(c => | ||
c.Type == claimName && | ||
c.Value.Split(',').Select(v => v.Trim()).Contains(claimValue)); | ||
} | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Equinox.Infra.CrossCutting.Identity/Authorization/CustomAuthorizeAttribute.cs
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,13 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Equinox.Infra.CrossCutting.Identity.Authorization | ||
{ | ||
public class CustomAuthorizeAttribute : TypeFilterAttribute | ||
{ | ||
public CustomAuthorizeAttribute(string claimName, string claimValue) : base(typeof(RequerimentClaimFilter)) | ||
{ | ||
Arguments = new object[] { new Claim(claimName, claimValue) }; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Equinox.Infra.CrossCutting.Identity/Authorization/RequerimentClaimFilter.cs
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,30 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace Equinox.Infra.CrossCutting.Identity.Authorization | ||
{ | ||
internal class RequerimentClaimFilter : IAuthorizationFilter | ||
{ | ||
private readonly Claim _claim; | ||
|
||
public RequerimentClaimFilter(Claim claim) | ||
{ | ||
_claim = claim; | ||
} | ||
|
||
public void OnAuthorization(AuthorizationFilterContext context) | ||
{ | ||
if (!context.HttpContext.User.Identity.IsAuthenticated) | ||
{ | ||
context.Result = new StatusCodeResult(401); | ||
return; | ||
} | ||
|
||
if (!CustomAuthorizationValidation.UserHasValidClaim(context.HttpContext, _claim.Type, _claim.Value)) | ||
{ | ||
context.Result = new StatusCodeResult(403); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.