summaryrefslogtreecommitdiff
path: root/Services/UserService.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-09-15 10:31:20 +1000
committerJake Mannens <jakem_5@hotmail.com>2026-01-14 20:28:34 +1100
commit5565be07f8d8d473759315fd99747c64e2ce3450 (patch)
treefe0323eebd9981d1f2bad219bff5ac9cd4b674aa /Services/UserService.cs
parent6255f7c74687934e6701ddd98f5e3a84de78c451 (diff)
Completed initial login functionality
Diffstat (limited to 'Services/UserService.cs')
-rw-r--r--Services/UserService.cs42
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));
}