oliverbooth.dev/OliverBooth.Common/Services/ISessionService.cs

107 lines
4.7 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OliverBooth.Common.Data.Web.Users;
using ISession = OliverBooth.Common.Data.Web.Users.ISession;
namespace OliverBooth.Common.Services;
public interface ISessionService
{
/// <summary>
/// Creates a new session for the specified user.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="user">The user.</param>
/// <returns>The newly-created session.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="request" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="user" /> is <see langword="null" />.</para>
/// </exception>
ISession CreateSession(HttpRequest request, IUser user);
/// <summary>
/// Deletes the specified session.
/// </summary>
/// <param name="session">The session to delete.</param>
/// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null" />.</exception>
void DeleteSession(ISession session);
/// <summary>
/// Deletes the client's session cookie.
/// </summary>
/// <param name="response">The response to edit.</param>
/// <exception cref="ArgumentNullException"><paramref name="response" /> is <see langword="null" />.</exception>
IActionResult DeleteSessionCookie(HttpResponse response);
/// <summary>
/// Saves a session cookie to the client's browser.
/// </summary>
/// <param name="response">The response to edit.</param>
/// <param name="session">The session to write.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="request" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="session" /> is <see langword="null" />.</para>
/// </exception>
void SaveSessionCookie(HttpResponse response, ISession session);
/// <summary>
/// Attempts to find the user associated with the client's current request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="response">The response to edit.</param>
/// <param name="user">
/// When this method returns, contains the user with the specified request, if the user is found; otherwise,
/// <see langword="null" />.
/// </param>
/// <returns>
/// <see langword="true" /> if a user is found; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="request" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="response" /> is <see langword="null" />.</para>
/// </exception>
bool TryGetCurrentUser(HttpRequest request, HttpResponse response, [NotNullWhen(true)] out IUser? user);
/// <summary>
/// Attempts to find a session with the specified ID.
/// </summary>
/// <param name="sessionId">The session ID.</param>
/// <param name="session">
/// When this method returns, contains the session with the specified ID, if the session is found; otherwise,
/// <see langword="null" />.
/// </param>
/// <returns>
/// <see langword="true" /> if a session with the specified ID is found; otherwise, <see langword="false" />.
/// </returns>
bool TryGetSession(Guid sessionId, [NotNullWhen(true)] out ISession? session);
/// <summary>
/// Attempts to find the session associated with the HTTP request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="session">
/// When this method returns, contains the session with the specified request, if the user is found; otherwise,
/// <see langword="null" />.
/// </param>
/// <returns><see langword="true" /> if the session was found; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="request" /> is <see langword="null" />.</exception>
bool TryGetSession(HttpRequest request, [NotNullWhen(true)] out ISession? session);
/// <summary>
/// Validates the session with the incoming HTTP request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="session">The session.</param>
/// <returns><see langword="true" /> if the session is valid; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="request" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="session" /> is <see langword="null" />.</para>
/// </exception>
bool ValidateSession(HttpRequest request, ISession session);
}