Password encryption, Claim groups
This commit is contained in:
@@ -10,4 +10,8 @@
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.16.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FoodsharingSiegen.Contracts\FoodsharingSiegen.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using FoodsharingSiegen.Contracts.Entity;
|
||||
using FoodsharingSiegen.Contracts.Helper;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace FoodsharingSiegen.Shared.Helper
|
||||
@@ -16,22 +17,23 @@ namespace FoodsharingSiegen.Shared.Helper
|
||||
/// <summary>
|
||||
/// Creates the token using the specified user id (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id</param>
|
||||
/// <returns>The string</returns>
|
||||
public static string CreateToken(Guid userId)
|
||||
public static string CreateToken(User user)
|
||||
{
|
||||
user.Password = "";
|
||||
var serializedUser = JsonSerializer.Serialize(user);
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
|
||||
new Claim("CanDoShit","yes")
|
||||
new Claim(ClaimTypes.UserData, serializedUser)
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddDays(30),
|
||||
Issuer = Issuer,
|
||||
Audience = Audience,
|
||||
SigningCredentials = new SigningCredentials(GetSigningKey(), SecurityAlgorithms.HmacSha256Signature)
|
||||
SigningCredentials = new SigningCredentials(Cryptor.GetSigningKey(), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
@@ -39,92 +41,52 @@ namespace FoodsharingSiegen.Shared.Helper
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method Decrypt
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts the crypted text (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="cryptedText">The crypted text</param>
|
||||
/// <param name="plainText"></param>
|
||||
/// <returns>The string</returns>
|
||||
public static bool TryDecrypt(string cryptedText, out string plainText)
|
||||
{
|
||||
plainText = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
CreateAlgorithm(out var tripleDes);
|
||||
|
||||
var toEncryptArray = Convert.FromBase64String(cryptedText);
|
||||
|
||||
var cTransform = tripleDes.CreateDecryptor();
|
||||
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
tripleDes.Clear();
|
||||
|
||||
plainText = Encoding.UTF8.GetString(resultArray);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method Encrypt
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the plain text (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="plainText">The plain text</param>
|
||||
/// <returns>The string</returns>
|
||||
public static string Encrypt(string plainText)
|
||||
{
|
||||
CreateAlgorithm(out var tripleDes);
|
||||
|
||||
var toEncryptArray = Encoding.UTF8.GetBytes(plainText );
|
||||
|
||||
|
||||
|
||||
var cTransform = tripleDes.CreateEncryptor();
|
||||
|
||||
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
tripleDes.Clear();
|
||||
|
||||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Public Method ValidateToken
|
||||
|
||||
/// <summary>
|
||||
/// Validates the token using the specified token (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="token">The token</param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns>A task containing the bool</returns>
|
||||
public static async Task<bool> ValidateToken(string? token)
|
||||
public static bool ValidateToken(string? token, out User? user)
|
||||
{
|
||||
user = null;
|
||||
|
||||
try
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var result = await tokenHandler.ValidateTokenAsync(token, new TokenValidationParameters
|
||||
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = GetSigningKey(),
|
||||
IssuerSigningKey = Cryptor.GetSigningKey(),
|
||||
|
||||
ValidateAudience = true,
|
||||
ValidAudience = Audience,
|
||||
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = Issuer
|
||||
});
|
||||
}, out var stuff);
|
||||
|
||||
var result = tokenHandler.ValidateTokenAsync(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = Cryptor.GetSigningKey(),
|
||||
|
||||
ValidateAudience = true,
|
||||
ValidAudience = Audience,
|
||||
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = Issuer
|
||||
}).Result;
|
||||
|
||||
if (result.Claims.TryGetValue(ClaimTypes.UserData, out var jsonObj))
|
||||
{
|
||||
user = JsonSerializer.Deserialize<User>(jsonObj.ToString());
|
||||
if (user != null) user.Password = string.Empty;
|
||||
}
|
||||
|
||||
return result.IsValid;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -135,40 +97,6 @@ namespace FoodsharingSiegen.Shared.Helper
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method CreateAlgorithm
|
||||
|
||||
/// <summary>
|
||||
/// Creates the algorithm using the specified triple des (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="tripleDes">The triple des</param>
|
||||
private static void CreateAlgorithm(out TripleDES tripleDes)
|
||||
{
|
||||
var md5 = MD5.Create();
|
||||
var keyArray = md5.ComputeHash(Encoding.UTF8.GetBytes(SigningKey));
|
||||
md5.Clear();
|
||||
|
||||
tripleDes = TripleDES.Create();
|
||||
tripleDes.Key = keyArray;
|
||||
tripleDes.Mode = CipherMode.ECB;
|
||||
tripleDes.Padding = PaddingMode.PKCS7;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method GetSigningKey
|
||||
|
||||
/// <summary>
|
||||
/// Gets the signing key (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <returns>The security key</returns>
|
||||
private static SecurityKey GetSigningKey() => new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SigningKey));
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// The signing key
|
||||
/// </summary>
|
||||
private const string SigningKey = "2uasw2§$%1nd47n9s43&%Zs3529s23&/%AW";
|
||||
|
||||
/// <summary>
|
||||
/// The audience
|
||||
|
||||
Reference in New Issue
Block a user