Skip to content

Commit

Permalink
update(JWT): update Authentication System and improve security using …
Browse files Browse the repository at this point in the history
…encryption algorithms
  • Loading branch information
RubDaShen committed Jun 22, 2024
1 parent 59ffb9b commit f877ab5
Show file tree
Hide file tree
Showing 106 changed files with 966 additions and 104 deletions.
4 changes: 4 additions & 0 deletions Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BCrypt.Net" Version="0.1.0" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Application/IAM/CommandServices/EncryptService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using _2_Domain.IAM.Services.Commands;

namespace Application.IAM.CommandServices;

public class EncryptService : IEncryptService
{
public string Encrypt(string password)
{
return BCrypt.Net.BCrypt.HashPassword(password);
}

public bool Verify(string password, string passwordHashed)
{
return BCrypt.Net.BCrypt.Verify(password, passwordHashed);
}
}
63 changes: 63 additions & 0 deletions Application/IAM/CommandServices/TokenService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Security.Claims;
using System.Text;
using _2_Domain.IAM.Models.Entities;
using _2_Domain.IAM.Services.Commands;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;

namespace Application.IAM.CommandServices;

public class TokenService : ITokenService
{
private string Secret = "3d8ec21ed300ddf5b3bcc16b4bb737388116f609452e009392673bee76353cb4";

public string GenerateToken(User user)
{
var key = Encoding.ASCII.GetBytes(Secret);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Sid, user.Id.ToString()),
new Claim(ClaimTypes.Name, user._UserInformation.Name)
}),
Expires = DateTime.UtcNow.AddHours(4),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var tokenHandler = new JsonWebTokenHandler();

var token = tokenHandler.CreateToken(tokenDescriptor);
return token;
}

public async Task<int?> ValidateToken(string token)
{
if (string.IsNullOrEmpty(token))
{
return null;
}

var tokenHandler = new JsonWebTokenHandler();
var key = Encoding.ASCII.GetBytes(Secret);
try
{
var tokenValidationResult = await tokenHandler.ValidateTokenAsync(token, new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
ClockSkew = TimeSpan.Zero
});

var jwtToken = (JsonWebToken)tokenValidationResult.SecurityToken;
var userId = int.Parse(jwtToken.Claims.First(claim => claim.Type == ClaimTypes.Sid).Value);
return userId;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
161 changes: 161 additions & 0 deletions Application/bin/Debug/net7.0/Application.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
"Application.dll": {}
}
},
"AutoMapper/9.0.0": {
"dependencies": {
"Microsoft.CSharp": "4.5.0",
"System.Reflection.Emit": "4.3.0"
},
"runtime": {
"lib/netstandard2.0/AutoMapper.dll": {
"assemblyVersion": "9.0.0.0",
"fileVersion": "9.0.0.0"
}
}
},
"Microsoft.CSharp/4.5.0": {},
"Microsoft.EntityFrameworkCore/7.0.19": {
"dependencies": {
"Microsoft.EntityFrameworkCore.Abstractions": "7.0.19",
Expand Down Expand Up @@ -202,6 +215,8 @@
}
}
},
"Microsoft.NETCore.Platforms/1.1.0": {},
"Microsoft.NETCore.Targets/1.1.0": {},
"MySqlConnector/2.2.5": {
"runtime": {
"lib/net7.0/MySqlConnector.dll": {
Expand Down Expand Up @@ -234,8 +249,70 @@
}
}
},
"System.IO/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0",
"System.Runtime": "4.3.0",
"System.Text.Encoding": "4.3.0",
"System.Threading.Tasks": "4.3.0"
}
},
"System.Reflection/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0",
"System.IO": "4.3.0",
"System.Reflection.Primitives": "4.3.0",
"System.Runtime": "4.3.0"
}
},
"System.Reflection.Emit/4.3.0": {
"dependencies": {
"System.IO": "4.3.0",
"System.Reflection": "4.3.0",
"System.Reflection.Emit.ILGeneration": "4.3.0",
"System.Reflection.Primitives": "4.3.0",
"System.Runtime": "4.3.0"
}
},
"System.Reflection.Emit.ILGeneration/4.3.0": {
"dependencies": {
"System.Reflection": "4.3.0",
"System.Reflection.Primitives": "4.3.0",
"System.Runtime": "4.3.0"
}
},
"System.Reflection.Primitives/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0",
"System.Runtime": "4.3.0"
}
},
"System.Runtime/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0"
}
},
"System.Text.Encoding/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0",
"System.Runtime": "4.3.0"
}
},
"System.Threading.Tasks/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.NETCore.Targets": "1.1.0",
"System.Runtime": "4.3.0"
}
},
"Domain/1.0.0": {
"dependencies": {
"AutoMapper": "9.0.0",
"Microsoft.Extensions.Configuration.Binder": "7.0.4",
"Shared": "1.0.0",
"System.IdentityModel.Tokens.Jwt": "7.6.0"
Expand Down Expand Up @@ -268,6 +345,20 @@
"serviceable": false,
"sha512": ""
},
"AutoMapper/9.0.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-xCqvoxT4HIrNY/xlXG9W+BA/awdrhWvMTKTK/igkGSRbhOhpl3Q8O8Gxlhzjc9JsYqE7sS6AxgyuUUvZ6R5/Bw==",
"path": "automapper/9.0.0",
"hashPath": "automapper.9.0.0.nupkg.sha512"
},
"Microsoft.CSharp/4.5.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-kaj6Wb4qoMuH3HySFJhxwQfe8R/sJsNJnANrvv8WdFPMoNbKY5htfNscv+LHCu5ipz+49m2e+WQXpLXr9XYemQ==",
"path": "microsoft.csharp/4.5.0",
"hashPath": "microsoft.csharp.4.5.0.nupkg.sha512"
},
"Microsoft.EntityFrameworkCore/7.0.19": {
"type": "package",
"serviceable": true,
Expand Down Expand Up @@ -394,6 +485,20 @@
"path": "microsoft.identitymodel.tokens/7.6.0",
"hashPath": "microsoft.identitymodel.tokens.7.6.0.nupkg.sha512"
},
"Microsoft.NETCore.Platforms/1.1.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==",
"path": "microsoft.netcore.platforms/1.1.0",
"hashPath": "microsoft.netcore.platforms.1.1.0.nupkg.sha512"
},
"Microsoft.NETCore.Targets/1.1.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-aOZA3BWfz9RXjpzt0sRJJMjAscAUm3Hoa4UWAfceV9UTYxgwZ1lZt5nO2myFf+/jetYQo4uTP7zS8sJY67BBxg==",
"path": "microsoft.netcore.targets/1.1.0",
"hashPath": "microsoft.netcore.targets.1.1.0.nupkg.sha512"
},
"MySqlConnector/2.2.5": {
"type": "package",
"serviceable": true,
Expand All @@ -415,6 +520,62 @@
"path": "system.identitymodel.tokens.jwt/7.6.0",
"hashPath": "system.identitymodel.tokens.jwt.7.6.0.nupkg.sha512"
},
"System.IO/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-3qjaHvxQPDpSOYICjUoTsmoq5u6QJAFRUITgeT/4gqkF1bajbSmb1kwSxEA8AHlofqgcKJcM8udgieRNhaJ5Cg==",
"path": "system.io/4.3.0",
"hashPath": "system.io.4.3.0.nupkg.sha512"
},
"System.Reflection/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-KMiAFoW7MfJGa9nDFNcfu+FpEdiHpWgTcS2HdMpDvt9saK3y/G4GwprPyzqjFH9NTaGPQeWNHU+iDlDILj96aQ==",
"path": "system.reflection/4.3.0",
"hashPath": "system.reflection.4.3.0.nupkg.sha512"
},
"System.Reflection.Emit/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-228FG0jLcIwTVJyz8CLFKueVqQK36ANazUManGaJHkO0icjiIypKW7YLWLIWahyIkdh5M7mV2dJepllLyA1SKg==",
"path": "system.reflection.emit/4.3.0",
"hashPath": "system.reflection.emit.4.3.0.nupkg.sha512"
},
"System.Reflection.Emit.ILGeneration/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-59tBslAk9733NXLrUJrwNZEzbMAcu8k344OYo+wfSVygcgZ9lgBdGIzH/nrg3LYhXceynyvTc8t5/GD4Ri0/ng==",
"path": "system.reflection.emit.ilgeneration/4.3.0",
"hashPath": "system.reflection.emit.ilgeneration.4.3.0.nupkg.sha512"
},
"System.Reflection.Primitives/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-5RXItQz5As4xN2/YUDxdpsEkMhvw3e6aNveFXUn4Hl/udNTCNhnKp8lT9fnc3MhvGKh1baak5CovpuQUXHAlIA==",
"path": "system.reflection.primitives/4.3.0",
"hashPath": "system.reflection.primitives.4.3.0.nupkg.sha512"
},
"System.Runtime/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-JufQi0vPQ0xGnAczR13AUFglDyVYt4Kqnz1AZaiKZ5+GICq0/1MH/mO/eAJHt/mHW1zjKBJd7kV26SrxddAhiw==",
"path": "system.runtime/4.3.0",
"hashPath": "system.runtime.4.3.0.nupkg.sha512"
},
"System.Text.Encoding/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-BiIg+KWaSDOITze6jGQynxg64naAPtqGHBwDrLaCtixsa5bKiR8dpPOHA7ge3C0JJQizJE+sfkz1wV+BAKAYZw==",
"path": "system.text.encoding/4.3.0",
"hashPath": "system.text.encoding.4.3.0.nupkg.sha512"
},
"System.Threading.Tasks/4.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-LbSxKEdOUhVe8BezB/9uOGGppt+nZf6e1VFyw6v3DN6lqitm0OSn2uXMOdtP0M3W4iMcqcivm2J6UgqiwwnXiA==",
"path": "system.threading.tasks/4.3.0",
"hashPath": "system.threading.tasks.4.3.0.nupkg.sha512"
},
"Domain/1.0.0": {
"type": "project",
"serviceable": false,
Expand Down
Binary file modified Application/bin/Debug/net7.0/Application.dll
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Application.pdb
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Domain.dll
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Domain.pdb
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Infrastructure.dll
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Infrastructure.pdb
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Shared.dll
Binary file not shown.
Binary file modified Application/bin/Debug/net7.0/Shared.pdb
Binary file not shown.
6 changes: 6 additions & 0 deletions Application/obj/Application.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"dependencies": {
"BCrypt.Net": {
"target": "Package",
"version": "[0.1.0, )"
}
},
"imports": [
"net461",
"net462",
Expand Down
Binary file modified Application/obj/Debug/net7.0/Application.assets.cache
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b653dafe8a57ebd046447627da49392539ef239a3fc0a203c9e2fdc4d9c2c0fc
66508dc63685e4b71585674463a4c4ee8116f753d3186e5c9a58b60a6c5455ad
Binary file modified Application/obj/Debug/net7.0/Application.dll
Binary file not shown.
Binary file modified Application/obj/Debug/net7.0/Application.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion Application/obj/Debug/net7.0/Application.sourcelink.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documents":{"C:\\Users\\USUARIO\\RiderProjects\\Propertunity\\*":"https://raw.githubusercontent.com/SmarTech-Propertunity/upc-pre-202401--si730-WS52-SmarTech-BackEndPlatform/efb0a74a79e224341b6535ff3f419fe85c893dbf/*"}}
{"documents":{"C:\\Users\\USUARIO\\RiderProjects\\Propertunity\\*":"https://raw.githubusercontent.com/SmarTech-Propertunity/upc-pre-202401--si730-WS52-SmarTech-BackEndPlatform/59ffb9b2fd7e87ff4a95d25ea4baf124737fe78d/*"}}
Binary file modified Application/obj/Debug/net7.0/ref/Application.dll
Binary file not shown.
Binary file modified Application/obj/Debug/net7.0/refint/Application.dll
Binary file not shown.
47 changes: 46 additions & 1 deletion Application/obj/project.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
}
}
},
"BCrypt.Net/0.1.0": {
"type": "package",
"compile": {
"lib/net35/BCrypt.Net.dll": {
"related": ".XML"
}
},
"runtime": {
"lib/net35/BCrypt.Net.dll": {
"related": ".XML"
}
}
},
"Microsoft.CSharp/4.5.0": {
"type": "package",
"compile": {
Expand Down Expand Up @@ -582,6 +595,19 @@
"lib/netstandard2.0/AutoMapper.xml"
]
},
"BCrypt.Net/0.1.0": {
"sha512": "sST2w361Dxt9GGMfpOTiK50wXGV64Ybb1hiX3xjEWnhVYZNF43NuySGwADJa7X1R+bA53NsFR+tuDxcYiJeIOA==",
"type": "package",
"path": "bcrypt.net/0.1.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"bcrypt.net.0.1.0.nupkg.sha512",
"bcrypt.net.nuspec",
"lib/net35/BCrypt.Net.XML",
"lib/net35/BCrypt.Net.dll"
]
},
"Microsoft.CSharp/4.5.0": {
"sha512": "kaj6Wb4qoMuH3HySFJhxwQfe8R/sJsNJnANrvv8WdFPMoNbKY5htfNscv+LHCu5ipz+49m2e+WQXpLXr9XYemQ==",
"type": "package",
Expand Down Expand Up @@ -1773,6 +1799,7 @@
},
"projectFileDependencyGroups": {
"net7.0": [
"BCrypt.Net >= 0.1.0",
"Domain >= 1.0.0",
"Infrastructure >= 1.0.0",
"Shared >= 1.0.0"
Expand Down Expand Up @@ -1832,6 +1859,12 @@
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"dependencies": {
"BCrypt.Net": {
"target": "Package",
"version": "[0.1.0, )"
}
},
"imports": [
"net461",
"net462",
Expand Down Expand Up @@ -1865,5 +1898,17 @@
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.100\\RuntimeIdentifierGraph.json"
}
}
}
},
"logs": [
{
"code": "NU1701",
"level": "Warning",
"warningLevel": 1,
"message": "Package 'BCrypt.Net 0.1.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.",
"libraryId": "BCrypt.Net",
"targetGraphs": [
"net7.0"
]
}
]
}
Loading

0 comments on commit f877ab5

Please sign in to comment.