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 { /// 1 hour. 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 { /// /// Generates a signed access token for the given user, expiring after /// hours. /// string GenerateAccessToken(UserAccount user); /// /// Generates a signed refresh token for the given user, expiring after /// hours. /// string GenerateRefreshToken(UserAccount user); /// /// Generates a signed confirmation token for the given user, expiring after /// hours. /// string GenerateConfirmationToken(UserAccount user); /// Generates a token of the type specified by . /// Must be . string GenerateToken(UserAccount user) where T : struct, Enum; /// Validates an access token's signature and expiration and extracts the caller's identity. /// /// Thrown when the token is missing required claims, has a malformed user ID, or otherwise fails validation. /// Task ValidateAccessTokenAsync(string token); /// Validates a refresh token's signature and expiration and extracts the caller's identity. /// /// Thrown when the token is missing required claims, has a malformed user ID, or otherwise fails validation. /// Task ValidateRefreshTokenAsync(string token); /// Validates a confirmation token's signature and expiration and extracts the caller's identity. /// /// Thrown when the token is missing required claims, has a malformed user ID, or otherwise fails validation. /// Task ValidateConfirmationTokenAsync(string token); /// Validates a refresh token and issues a new access/refresh token pair for the associated user. /// /// Thrown when the refresh token is invalid or expired, or when the user account it refers to no /// longer exists. /// Task RefreshTokenAsync(string refreshTokenString); }