using System.Security.Claims;
using Domain.Entities;
namespace Features.Auth.Services;
///
/// Identifies the kind of token being generated or validated.
///
public enum TokenType
{
/// A short-lived token used to authorize API requests.
AccessToken,
/// A long-lived token used to obtain new access tokens.
RefreshToken,
/// A short-lived token used to confirm a user's email/account.
ConfirmationToken,
}
/// Represents the result of successfully validating a token.
public record ValidatedToken(Guid UserId, string Username, ClaimsPrincipal Principal);
/// Represents the result of refreshing a user's session.
public record RefreshTokenResult(UserAccount UserAccount, string RefreshToken, string AccessToken);
///
/// Defines the expiration windows, in hours, for each type of token issued by .
///
public static class TokenServiceExpirationHours
{
public const double AccessTokenHours = 1;
/// 21 days.
public const double RefreshTokenHours = 504;
/// 30 minutes.
public const double ConfirmationTokenHours = 0.5;
}
///
/// Defines operations for generating and validating JWTs used for access, refresh, and account confirmation.
///
public interface ITokenService
{
string GenerateAccessToken(UserAccount user);
string GenerateRefreshToken(UserAccount user);
string GenerateConfirmationToken(UserAccount user);
/// Generates a token of the type specified by .
/// Must be .
string GenerateToken(UserAccount user)
where T : struct, Enum;
Task ValidateAccessTokenAsync(string token);
Task ValidateRefreshTokenAsync(string token);
Task ValidateConfirmationTokenAsync(string token);
/// Validates a refresh token and issues a new access/refresh token pair for the associated user.
Task RefreshTokenAsync(string refreshTokenString);
}