oliverbooth.dev/OliverBooth/Services/SessionService.cs

169 lines
5.2 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
using System.Net;
2024-02-24 15:04:03 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
using ISession = OliverBooth.Data.Web.ISession;
namespace OliverBooth.Services;
internal sealed class SessionService : ISessionService
{
private readonly ILogger<SessionService> _logger;
private readonly IUserService _userService;
private readonly IDbContextFactory<WebContext> _webContextFactory;
/// <summary>
/// Initializes a new instance of the <see cref="SessionService" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="userService">The user service.</param>
/// <param name="blogContextFactory">The <see cref="BlogContext" /> factory.</param>
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public SessionService(ILogger<SessionService> logger,
IUserService userService,
IDbContextFactory<BlogContext> blogContextFactory,
IDbContextFactory<WebContext> webContextFactory)
{
_logger = logger;
_userService = userService;
_webContextFactory = webContextFactory;
}
/// <inheritdoc />
public ISession CreateSession(HttpRequest request, IUser user)
{
2024-02-25 14:11:33 +00:00
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (user is null)
{
throw new ArgumentNullException(nameof(user));
}
using WebContext context = _webContextFactory.CreateDbContext();
var now = DateTimeOffset.UtcNow;
var session = new Session
{
UserId = user.Id,
IpAddress = request.HttpContext.Connection.RemoteIpAddress!,
Created = now,
Updated = now,
LastAccessed = now,
Expires = now + TimeSpan.FromDays(1),
RequiresTotp = !string.IsNullOrWhiteSpace(user.Totp)
};
EntityEntry<Session> entry = context.Sessions.Add(session);
context.SaveChanges();
return entry.Entity;
}
/// <inheritdoc />
public void DeleteSession(ISession session)
{
using WebContext context = _webContextFactory.CreateDbContext();
context.Sessions.Remove((Session)session);
context.SaveChanges();
}
/// <inheritdoc />
public void SaveSessionCookie(HttpResponse response, ISession session)
{
2024-02-25 14:11:33 +00:00
if (response is null)
{
throw new ArgumentNullException(nameof(response));
}
if (session is null)
{
throw new ArgumentNullException(nameof(session));
}
Span<byte> buffer = stackalloc byte[16];
2024-02-25 14:11:33 +00:00
if (!session.Id.TryWriteBytes(buffer))
{
return;
}
IPAddress? remoteIpAddress = response.HttpContext.Connection.RemoteIpAddress;
_logger.LogDebug("Writing cookie 'sid' to HTTP response for {RemoteAddr}", remoteIpAddress);
response.Cookies.Append("sid", Convert.ToBase64String(buffer));
}
/// <inheritdoc />
public bool TryGetSession(Guid sessionId, [NotNullWhen(true)] out ISession? session)
{
using WebContext context = _webContextFactory.CreateDbContext();
session = context.Sessions.FirstOrDefault(s => s.Id == sessionId);
return session is not null;
}
/// <inheritdoc />
2024-02-24 15:04:03 +00:00
public bool TryGetSession(HttpRequest request, [NotNullWhen(true)] out ISession? session)
{
2024-02-25 14:11:33 +00:00
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
session = null;
IPAddress? remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
2024-02-25 14:11:33 +00:00
if (remoteIpAddress is null)
{
return false;
}
if (!request.Cookies.TryGetValue("sid", out string? sessionIdCookie))
2024-02-25 14:11:33 +00:00
{
return false;
2024-02-25 14:11:33 +00:00
}
Span<byte> bytes = stackalloc byte[16];
if (!Convert.TryFromBase64Chars(sessionIdCookie, bytes, out int bytesWritten) || bytesWritten < 16)
2024-02-25 14:11:33 +00:00
{
return false;
2024-02-25 14:11:33 +00:00
}
var sessionId = new Guid(bytes);
2024-02-24 15:04:03 +00:00
return TryGetSession(sessionId, out session);
}
/// <inheritdoc />
public bool ValidateSession(HttpRequest request, ISession session)
{
2024-02-25 14:11:33 +00:00
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
2024-02-25 14:11:33 +00:00
if (session is null)
{
throw new ArgumentNullException(nameof(session));
}
2024-02-24 15:04:03 +00:00
2024-02-25 14:11:33 +00:00
IPAddress? remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
if (remoteIpAddress is null)
{
return false;
}
Span<byte> remoteAddressBytes = stackalloc byte[16];
Span<byte> sessionAddressBytes = stackalloc byte[16];
if (!remoteIpAddress.TryWriteBytes(remoteAddressBytes, out _) ||
!session.IpAddress.TryWriteBytes(sessionAddressBytes, out _))
return false;
2024-02-24 15:04:03 +00:00
if (!remoteAddressBytes.SequenceEqual(sessionAddressBytes))
return false;
2024-02-24 15:04:03 +00:00
if (_userService.TryGetUser(session.UserId, out _))
return false;
return true;
}
}