diff options
Diffstat (limited to 'Services/UserService.cs')
| -rw-r--r-- | Services/UserService.cs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/Services/UserService.cs b/Services/UserService.cs index d2abea3..1a2bd8f 100644 --- a/Services/UserService.cs +++ b/Services/UserService.cs @@ -1,9 +1,14 @@ -namespace HyperBooru.Services; +using Microsoft.AspNetCore.Cryptography.KeyDerivation; +using Microsoft.EntityFrameworkCore; + +namespace HyperBooru.Services; public interface IUserService { public bool ShowNsfw { get; set; } public event EventHandler<bool> ShowNsfwChanged; + + public User User { get; } } public class UserService : IUserService { @@ -15,7 +20,42 @@ public class UserService : IUserService { } } + public User User { + get { + if(user is not null) + return user; + using var db = dbFactory.CreateDbContext(); + int id = int.Parse(httpContextAccessor + .HttpContext!.User.Claims + .First(c => c.Type == "ObjectId") + .Value); + return user = db.Users.Find(id)!; + } + } + public event EventHandler<bool> ShowNsfwChanged; private bool showNsfw = false; + + private User? user; + + private IDbContextFactory<HBContext> dbFactory; + private IHttpContextAccessor httpContextAccessor; + + public UserService( + IDbContextFactory<HBContext> dbFactory, + IHttpContextAccessor httpContextAccessor) { + + this.dbFactory = dbFactory; + this.httpContextAccessor = httpContextAccessor; + } + + public static string HashPassword(string password) => + Convert.ToBase64String( + KeyDerivation.Pbkdf2( + password, + Array.Empty<byte>(), + KeyDerivationPrf.HMACSHA512, + 100_000, + 512 / 8)); } |
