1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/IdentityServer/Security/PasswordHasher.cs

63 lines
1.9 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Teknik.Configuration;
using Microsoft.Extensions.Identity.Core;
using Microsoft.Extensions.Options;
using Teknik.Utilities.Cryptography;
using Teknik.Utilities;
using System.Text;
using Teknik.IdentityServer.Models;
namespace Teknik.IdentityServer.Security
{
public class TeknikPasswordHasher : PasswordHasher<ApplicationUser>
{
private readonly Config _config;
public TeknikPasswordHasher(Config config)
{
_config = config;
}
public override PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword)
{
if (hashedPassword == null)
{
throw new ArgumentNullException(nameof(hashedPassword));
}
if (providedPassword == null)
{
throw new ArgumentNullException(nameof(providedPassword));
}
// Test legacy password hashes
#region Legacy Checks
2019-01-29 02:58:02 +01:00
byte[] hashBytes = SHA384.Hash(user.UserName.ToLower(), providedPassword);
string hash = hashBytes.ToHex();
if (hashedPassword == hash)
{
return PasswordVerificationResult.SuccessRehashNeeded;
}
hash = Encoding.ASCII.GetString(hashBytes);
if (hashedPassword == hash)
{
return PasswordVerificationResult.SuccessRehashNeeded;
}
hash = SHA256.Hash(providedPassword, _config.Salt1, _config.Salt2);
if (hashedPassword == hash)
{
return PasswordVerificationResult.SuccessRehashNeeded;
}
#endregion
// Test Latest
return base.VerifyHashedPassword(user, hashedPassword, providedPassword);
}
}
}