35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AS400API.Auth;
|
|
|
|
public sealed class DemoUserStore
|
|
{
|
|
private readonly Dictionary<string, DemoUser> _users;
|
|
|
|
public DemoUserStore()
|
|
{
|
|
_users = new Dictionary<string, DemoUser>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["admin"] = CreateUser("admin", "Pass@123", new[] { Roles.Admin, Roles.Operator }),
|
|
["operator"] = CreateUser("operator", "Pass@123", new[] { Roles.Operator })
|
|
};
|
|
}
|
|
|
|
public ValueTask<DemoUser?> FindByNameAsync(string username)
|
|
{
|
|
_users.TryGetValue(username, out var user);
|
|
return ValueTask.FromResult(user);
|
|
}
|
|
|
|
public bool ValidateCredentials(DemoUser user, string password) =>
|
|
PasswordHasher.Verify(password, user.PasswordHash, user.PasswordSalt);
|
|
|
|
private static DemoUser CreateUser(string username, string password, IReadOnlyCollection<string> roles)
|
|
{
|
|
var (hash, salt) = PasswordHasher.HashPassword(password);
|
|
return new DemoUser(username, hash, salt, roles);
|
|
}
|
|
}
|