Skip to content

Commit

Permalink
.net 8 migration and refactorings (#201)
Browse files Browse the repository at this point in the history
* .NET 8

* .NET 8

* Setup of Identity without external dependencies

* Update README.md

* Update dotnet-core.yml
  • Loading branch information
EduardoPires authored Jul 31, 2024
1 parent 4c348d6 commit dcbe058
Show file tree
Hide file tree
Showing 59 changed files with 1,093 additions and 354 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ The Equinox Project is a open-source project written in .NET Core
The goal of this project is implement the most common used technologies and share with the technical community the best way to develop great applications with .NET

[![Build status](https://ci.appveyor.com/api/projects/status/rl2ja69994rt3ei6?svg=true)](https://ci.appveyor.com/project/EduardoPires/equinoxproject)
![.NET Core](https://github.com/EduardoPires/EquinoxProject/workflows/.NET%20Core/badge.svg)
[![License](https://img.shields.io/github/license/eduardopires/equinoxproject.svg)](LICENSE)
[![Issues open](https://img.shields.io/github/issues/eduardopires/equinoxproject.svg)](https://huboard.com/EduardoPires/EquinoxProject/)

Expand All @@ -29,18 +28,17 @@ To know more about how to setup your enviroment visit the [Microsoft .NET Downlo

## Technologies implemented:

- ASP.NET 6.0
- ASP.NET 8.0
- ASP.NET MVC Core
- ASP.NET WebApi Core with JWT Bearer Authentication
- ASP.NET Identity Core
- Entity Framework Core 6.0
- Entity Framework Core 8.0
- .NET Core Native DI
- AutoMapper
- FluentValidator
- MediatR
- Swagger UI with JWT support
- .NET DevPack
- .NET DevPack.Identity

## Architecture:

Expand All @@ -56,6 +54,12 @@ To know more about how to setup your enviroment visit the [Microsoft .NET Downlo

## News

**v1.9 - 06/31/2024**
- Migrated for .NET 8.0
- Full refactoring of Web and Api configuration
- Now all ASP.NET Identity configurations are inside the project, without external dependencies
- All dependencies is up to date

**v1.8 - 03/22/2022**
- Migrated for .NET 6.0
- All dependencies is up to date
Expand Down
9 changes: 5 additions & 4 deletions src/Equinox.Application/Equinox.Application.csproj
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>
3 changes: 2 additions & 1 deletion src/Equinox.Domain.Core/Equinox.Domain.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NetDevPack" Version="7.0.1" />
Expand Down
5 changes: 3 additions & 2 deletions src/Equinox.Domain/Equinox.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Equinox.Domain.Core\Equinox.Domain.Core.csproj" />
<ProjectReference Include="..\Equinox.Infra.CrossCutting.Bus\Equinox.Infra.CrossCutting.Bus.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="10.4.0" />
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mediatr" Version="10.0.1" />
<PackageReference Include="mediatr" Version="12.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Equinox.Domain.Core\Equinox.Domain.Core.csproj" />
Expand Down
10 changes: 10 additions & 0 deletions src/Equinox.Infra.CrossCutting.Identity/API/AppJwtSettings.cs
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 src/Equinox.Infra.CrossCutting.Identity/API/JwtBuilder.cs
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 src/Equinox.Infra.CrossCutting.Identity/ApiIdentityConfig.cs

This file was deleted.

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));
}

}
}
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) };
}
}
}
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);
}
}
}
}
Loading

0 comments on commit dcbe058

Please sign in to comment.