-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update(JWT): update Authentication System and improve security using …
…encryption algorithms
- Loading branch information
Showing
106 changed files
with
966 additions
and
104 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
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); | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Binary file not shown.
Binary file modified
BIN
-170 Bytes
(99%)
Application/obj/Debug/net7.0/Application.csproj.AssemblyReference.cache
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
Application/obj/Debug/net7.0/Application.csproj.CoreCompileInputs.cache
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 +1 @@ | ||
b653dafe8a57ebd046447627da49392539ef239a3fc0a203c9e2fdc4d9c2c0fc | ||
66508dc63685e4b71585674463a4c4ee8116f753d3186e5c9a58b60a6c5455ad |
Binary file not shown.
Binary file not shown.
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 +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 not shown.
Binary file not shown.
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
Oops, something went wrong.