remember me
This commit is contained in:
parent
dac3acfecf
commit
8629883f88
10 changed files with 278 additions and 96 deletions
|
@ -3,7 +3,8 @@ using System.Text.Encodings.Web;
|
|||
using Femto.Api.Sessions;
|
||||
using Femto.Common;
|
||||
using Femto.Modules.Auth.Application;
|
||||
using Femto.Modules.Auth.Application.Services;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Models;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
|
@ -20,41 +21,14 @@ internal class SessionAuthenticationHandler(
|
|||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
Logger.LogDebug("{TraceId} Authenticating session", this.Context.TraceIdentifier);
|
||||
|
||||
var sessionId = this.Context.GetSessionId();
|
||||
|
||||
if (sessionId is null)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} SessionId was null ", this.Context.TraceIdentifier);
|
||||
return AuthenticateResult.NoResult();
|
||||
}
|
||||
|
||||
var session = await authService.GetSession(sessionId);
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} Loaded session was null ", this.Context.TraceIdentifier);
|
||||
return await FailAndDeleteSession(sessionId);
|
||||
}
|
||||
var user = await this.TryAuthenticateWithSession();
|
||||
|
||||
if (session.IsExpired)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} Loaded session was expired ", this.Context.TraceIdentifier);
|
||||
return await FailAndDeleteSession(sessionId);
|
||||
}
|
||||
|
||||
var user = await authService.GetUserWithId(session.UserId);
|
||||
if (user is null)
|
||||
user = await this.TryAuthenticateWithRememberMeToken();
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return await FailAndDeleteSession(sessionId);
|
||||
}
|
||||
|
||||
if (session.ExpiresSoon)
|
||||
{
|
||||
session = await authService.CreateWeakSession(session.UserId);
|
||||
this.Context.SetSession(session, user);
|
||||
}
|
||||
return AuthenticateResult.NoResult();
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
|
@ -72,10 +46,77 @@ internal class SessionAuthenticationHandler(
|
|||
return AuthenticateResult.Success(new AuthenticationTicket(principal, this.Scheme.Name));
|
||||
}
|
||||
|
||||
private async Task<AuthenticateResult> FailAndDeleteSession(string sessionId)
|
||||
private async Task<UserInfo?> TryAuthenticateWithSession()
|
||||
{
|
||||
await authService.DeleteSession(sessionId);
|
||||
this.Context.DeleteSession();
|
||||
return AuthenticateResult.Fail("invalid session");
|
||||
var sessionId = this.Context.GetSessionId();
|
||||
|
||||
if (sessionId is null)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} SessionId was null ", this.Context.TraceIdentifier);
|
||||
return null;
|
||||
}
|
||||
|
||||
var session = await authService.GetSession(sessionId);
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} Loaded session was null ", this.Context.TraceIdentifier);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (session.IsExpired)
|
||||
{
|
||||
Logger.LogDebug("{TraceId} Loaded session was expired ", this.Context.TraceIdentifier);
|
||||
await authService.DeleteSession(sessionId);
|
||||
this.Context.DeleteSession();
|
||||
return null;
|
||||
}
|
||||
|
||||
var user = await authService.GetUserWithId(session.UserId);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
await authService.DeleteSession(sessionId);
|
||||
this.Context.DeleteSession();
|
||||
return null;
|
||||
}
|
||||
|
||||
if (session.ExpiresSoon)
|
||||
{
|
||||
session = await authService.CreateWeakSession(session.UserId);
|
||||
this.Context.SetSession(session, user);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<UserInfo?> TryAuthenticateWithRememberMeToken()
|
||||
{
|
||||
/*
|
||||
* load remember me from token
|
||||
* if it is null, return null
|
||||
* if it exists, validate it
|
||||
* if it is valid, create a new weak session, return the user
|
||||
* if it is almost expired, refresh it
|
||||
*/
|
||||
|
||||
var rememberMeToken = this.Context.GetRememberMeToken();
|
||||
|
||||
if (rememberMeToken is null)
|
||||
return null;
|
||||
|
||||
var (user, newRememberMeToken) = await authService.GetUserWithRememberMeToken(rememberMeToken);
|
||||
|
||||
if (user is null)
|
||||
return null;
|
||||
|
||||
var session = await authService.CreateWeakSession(user.Id);
|
||||
|
||||
this.Context.SetSession(session, user);
|
||||
|
||||
if (newRememberMeToken is not null)
|
||||
this.Context.SetRememberMeToken(newRememberMeToken);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Femto.Api.Sessions;
|
||||
using Femto.Common;
|
||||
using Femto.Modules.Auth.Application.Services;
|
||||
using Femto.Modules.Auth.Application;
|
||||
using Femto.Modules.Auth.Contracts;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -18,10 +18,9 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
|||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var result = await authService.GetUserWithCredentials(
|
||||
var result = await authService.AuthenticateUserCredentials(
|
||||
request.Username,
|
||||
request.Password,
|
||||
request.RememberMe,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
|
@ -29,23 +28,35 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
|||
return this.BadRequest();
|
||||
|
||||
var (user, session) = result;
|
||||
|
||||
|
||||
HttpContext.SetSession(session, user);
|
||||
|
||||
if (request.RememberMe)
|
||||
{
|
||||
var newRememberMeToken = await authService.CreateRememberMeToken(user.Id);
|
||||
HttpContext.SetRememberMeToken(newRememberMeToken);
|
||||
}
|
||||
|
||||
return new LoginResponse(user.Id, user.Username, user.Roles.Any(r => r == Role.SuperUser));
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<RegisterResponse>> Register([FromBody] RegisterRequest request)
|
||||
public async Task<ActionResult<RegisterResponse>> Register([FromBody] RegisterRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var (user, session) = await authService.CreateUserWithCredentials(
|
||||
request.Username,
|
||||
request.Password,
|
||||
request.SignupCode,
|
||||
request.RememberMe
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
HttpContext.SetSession(session, user);
|
||||
|
||||
if (request.RememberMe)
|
||||
{
|
||||
var newRememberMeToken = await authService.CreateRememberMeToken(user.Id);
|
||||
HttpContext.SetRememberMeToken(newRememberMeToken);
|
||||
}
|
||||
|
||||
return new RegisterResponse(
|
||||
user.Id,
|
||||
|
@ -65,6 +76,14 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
|||
HttpContext.DeleteSession();
|
||||
}
|
||||
|
||||
var rememberMeToken = HttpContext.GetRememberMeToken();
|
||||
|
||||
if (rememberMeToken is not null)
|
||||
{
|
||||
await authService.DeleteRememberMeToken(rememberMeToken);
|
||||
HttpContext.DeleteRememberMeToken();
|
||||
}
|
||||
|
||||
return Ok(new { });
|
||||
}
|
||||
|
||||
|
|
|
@ -15,18 +15,12 @@ internal static class HttpContextSessionExtensions
|
|||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
};
|
||||
|
||||
public static string? GetSessionId(this HttpContext httpContext)
|
||||
{
|
||||
var sessionId = httpContext.Request.Cookies["sid"];
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
public static string? GetSessionId(this HttpContext httpContext) =>
|
||||
httpContext.Request.Cookies["sid"];
|
||||
|
||||
public static void SetSession(this HttpContext context, Session session, UserInfo user)
|
||||
{
|
||||
var cookieSettings = context.RequestServices.GetRequiredService<
|
||||
IOptions<CookieSettings>
|
||||
>();
|
||||
var cookieSettings = context.RequestServices.GetRequiredService<IOptions<CookieSettings>>();
|
||||
|
||||
context.Response.Cookies.Append(
|
||||
"sid",
|
||||
|
@ -57,7 +51,7 @@ internal static class HttpContextSessionExtensions
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static void DeleteSession(this HttpContext httpContext)
|
||||
{
|
||||
var cookieSettings = httpContext.RequestServices.GetRequiredService<
|
||||
|
@ -91,4 +85,47 @@ internal static class HttpContextSessionExtensions
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static RememberMeToken? GetRememberMeToken(this HttpContext httpContext) =>
|
||||
httpContext.Request.Cookies["rid"] is { } code ? RememberMeToken.FromCode(code) : null;
|
||||
|
||||
public static void SetRememberMeToken(this HttpContext context, NewRememberMeToken token)
|
||||
{
|
||||
var cookieSettings = context.RequestServices.GetRequiredService<IOptions<CookieSettings>>();
|
||||
|
||||
context.Response.Cookies.Append(
|
||||
"rid",
|
||||
token.Code,
|
||||
new CookieOptions
|
||||
{
|
||||
Path = "/",
|
||||
IsEssential = true,
|
||||
Domain = cookieSettings.Value.Domain,
|
||||
HttpOnly = true,
|
||||
Secure = cookieSettings.Value.Secure,
|
||||
SameSite = cookieSettings.Value.SameSite,
|
||||
Expires = token.Expires,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static void DeleteRememberMeToken(this HttpContext context)
|
||||
{
|
||||
var cookieSettings = context.RequestServices.GetRequiredService<IOptions<CookieSettings>>();
|
||||
|
||||
context.Response.Cookies.Delete(
|
||||
"rid",
|
||||
new CookieOptions
|
||||
{
|
||||
Path = "/",
|
||||
HttpOnly = true,
|
||||
Domain = cookieSettings.Value.Domain,
|
||||
IsEssential = true,
|
||||
Secure = cookieSettings.Value.Secure,
|
||||
SameSite = cookieSettings.Value.SameSite,
|
||||
Expires = DateTimeOffset.UtcNow.AddDays(-1),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue