feat(backend): start authentication controllers

This commit is contained in:
sam 2024-06-12 03:47:20 +02:00
parent 493a6e4d29
commit 25540f2de2
15 changed files with 777 additions and 17 deletions

View file

@ -3,6 +3,7 @@ using Foxnouns.Backend.Database;
using Foxnouns.Backend.Database.Models;
using Foxnouns.Backend.Utils;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using NodaTime;
namespace Foxnouns.Backend.Services;
@ -30,6 +31,23 @@ public class AuthService(ILogger logger, DatabaseContext db, ISnowflakeGenerator
return user;
}
public async Task<User> AuthenticateUserAsync(string email, string password)
{
var user = await db.Users.FirstOrDefaultAsync(u => u.AuthMethods.Any(a => a.AuthType == AuthType.Email && a.RemoteId == email));
if (user == null) throw new ApiError.NotFound("No user with that email address found, or password is incorrect");
var pwResult = await Task.Run(() => _passwordHasher.VerifyHashedPassword(user, user.Password!, password));
if (pwResult == PasswordVerificationResult.Failed)
throw new ApiError.NotFound("No user with that email address found, or password is incorrect");
if (pwResult == PasswordVerificationResult.SuccessRehashNeeded)
{
user.Password = await Task.Run(() => _passwordHasher.HashPassword(user, password));
await db.SaveChangesAsync();
}
return user;
}
public (string, Token) GenerateToken(User user, Application application, string[] scopes, Instant expires)
{
if (!OauthUtils.ValidateScopes(application, scopes))

View file

@ -0,0 +1,51 @@
using Foxnouns.Backend.Database;
using Foxnouns.Backend.Database.Models;
using Foxnouns.Backend.Utils;
using Microsoft.EntityFrameworkCore;
using NodaTime;
namespace Foxnouns.Backend.Services;
public class KeyCacheService(DatabaseContext db, IClock clock, ILogger logger)
{
public Task SetKeyAsync(string key, string value, Duration expireAfter) =>
db.SetKeyAsync(key, value, clock.GetCurrentInstant() + expireAfter);
public async Task SetKeyAsync(string key, string value, Instant expires)
{
db.TemporaryKeys.Add(new TemporaryKey
{
Expires = expires,
Key = key,
Value = value,
});
await db.SaveChangesAsync();
}
public async Task<string?> GetKeyAsync(string key, bool delete = false)
{
var value = await db.TemporaryKeys.FirstOrDefaultAsync(k => k.Key == key);
if (value == null) return null;
if (delete)
{
await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync();
await db.SaveChangesAsync();
}
return value.Value;
}
public async Task DeleteExpiredKeysAsync()
{
var count = await db.TemporaryKeys.Where(k => k.Expires < clock.GetCurrentInstant()).ExecuteDeleteAsync();
if (count != 0) logger.Information("Removed {Count} expired keys from the database", count);
}
public async Task<string> GenerateAuthStateAsync()
{
var state = OauthUtils.RandomToken();
await SetKeyAsync($"oauth_state:{state}", "", Duration.FromMinutes(10));
return state;
}
}

View file

@ -7,7 +7,7 @@ namespace Foxnouns.Backend.Services;
public class UserRendererService(DatabaseContext db, MemberRendererService memberRendererService)
{
public async Task<object> RenderUserAsync(User user, User? selfUser = null, bool renderMembers = true)
public async Task<UserResponse> RenderUserAsync(User user, User? selfUser = null, bool renderMembers = true)
{
renderMembers = renderMembers && (!user.ListHidden || selfUser?.Id == user.Id);