diff --git a/Security/Services/AuthService.cs b/Security/Services/AuthService.cs index 38ee706..5bb7262 100644 --- a/Security/Services/AuthService.cs +++ b/Security/Services/AuthService.cs @@ -29,14 +29,14 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I private static string GetFirstAuthCacheKey(string fingerprint) => $"{fingerprint}_auth_token_first"; private static string GetAttemptFailedCountKey(string fingerprint) => $"{fingerprint}_login_failed"; - private Task StoreAuthTokenInCache(AuthToken data, CancellationToken cancellation) => + private Task StoreAuthTokenInCache(AuthToken data, CancellationToken cancellationToken) => cache.SetAsync( GetAuthCacheKey(data.Fingerprint), JsonSerializer.SerializeToUtf8Bytes(data), slidingExpiration: Lifetime, - cancellationToken: cancellation); + cancellationToken: cancellationToken); - private Task StoreFirstAuthTokenInCache(User data, RequestContextInfo requestContext, CancellationToken cancellation) => + private Task StoreFirstAuthTokenInCache(User data, RequestContextInfo requestContext, CancellationToken cancellationToken) => cache.SetAsync( GetFirstAuthCacheKey(requestContext.Fingerprint), JsonSerializer.SerializeToUtf8Bytes(new FirstAuthToken(requestContext) @@ -46,14 +46,14 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I TwoFactorAuthenticator = data.TwoFactorAuthenticator }), slidingExpiration: LifetimeFirstAuth, - cancellationToken: cancellation); + cancellationToken: cancellationToken); private Task RevokeAccessToken(string token) => revokedToken.AddTokenToRevokedAsync(token, accessTokenService.GetExpireDateTime(token)); - private async Task RecordFailedLoginAttempt(string fingerprint, string userId, CancellationToken cancellation) + private async Task RecordFailedLoginAttempt(string fingerprint, string userId, CancellationToken cancellationToken) { - var failedLoginAttemptsCount = await cache.GetAsync(GetAttemptFailedCountKey(fingerprint), cancellation) ?? 1; + var failedLoginAttemptsCount = await cache.GetAsync(GetAttemptFailedCountKey(fingerprint), cancellationToken) ?? 1; var failedLoginCacheExpiration = TimeSpan.FromHours(1); if (failedLoginAttemptsCount > 5) @@ -74,30 +74,30 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I failedLoginAttemptsCount); await cache.SetAsync(GetAttemptFailedCountKey(fingerprint), failedLoginAttemptsCount + 1, - slidingExpiration: failedLoginCacheExpiration, cancellationToken: cancellation); + slidingExpiration: failedLoginCacheExpiration, cancellationToken: cancellationToken); } - private Task ResetFailedLoginAttempts(string fingerprint, CancellationToken cancellation) => - cache.RemoveAsync(GetAttemptFailedCountKey(fingerprint), cancellation); + private Task ResetFailedLoginAttempts(string fingerprint, CancellationToken cancellationToken) => + cache.RemoveAsync(GetAttemptFailedCountKey(fingerprint), cancellationToken); private async Task VerifyUserOrThrowError(RequestContextInfo requestContext, User user, string password, string username, - CancellationToken cancellation = default) + CancellationToken cancellationToken = default) { if ((user.Email.Equals(username, StringComparison.OrdinalIgnoreCase) || user.Username.Equals(username, StringComparison.OrdinalIgnoreCase)) && passwordService.VerifyPassword(password, user.Salt, user.PasswordHash)) { - await ResetFailedLoginAttempts(requestContext.Fingerprint, cancellation); + await ResetFailedLoginAttempts(requestContext.Fingerprint, cancellationToken); return; } - await RecordFailedLoginAttempt(requestContext.Fingerprint, user.Id, cancellation); + await RecordFailedLoginAttempt(requestContext.Fingerprint, user.Id, cancellationToken); throw new SecurityException("Authentication failed. Please check your credentials."); } private async Task GenerateAuthTokensAsync(CookieOptions cookieOptions, HttpContext context, - RequestContextInfo requestContext, string userId, CancellationToken cancellation = default) + RequestContextInfo requestContext, string userId, CancellationToken cancellationToken = default) { var refreshToken = GenerateRefreshToken(); var (token, expireIn) = GenerateAccessToken(userId); @@ -110,7 +110,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I AccessToken = token }; - await StoreAuthTokenInCache(authToken, cancellation); + await StoreAuthTokenInCache(authToken, cancellationToken); cookieOptions.SetCookie(context, CookieNames.AccessToken, authToken.AccessToken, expireIn); cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime)); @@ -121,11 +121,11 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I } public async Task LoginAsync(CookieOptions cookieOptions, HttpContext context, TwoFactorAuthenticator authenticator, string code, - CancellationToken cancellation = default) + CancellationToken cancellationToken = default) { var requestContext = new RequestContextInfo(context, cookieOptions); - var firstTokenAuth = await cache.GetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), cancellationToken: cancellation); + var firstTokenAuth = await cache.GetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), cancellationToken: cancellationToken); if (firstTokenAuth == null || authenticator != firstTokenAuth.TwoFactorAuthenticator) throw new SecurityException("Session expired. Please log in again."); @@ -147,35 +147,35 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I if (!totp.VerifyToken(code)) { - await RecordFailedLoginAttempt(requestContext.Fingerprint, firstTokenAuth.UserId, cancellation); + await RecordFailedLoginAttempt(requestContext.Fingerprint, firstTokenAuth.UserId, cancellationToken); throw new SecurityException("Invalid verification code. Please try again."); } - await ResetFailedLoginAttempts(requestContext.Fingerprint, cancellation); + await ResetFailedLoginAttempts(requestContext.Fingerprint, cancellationToken); } break; default: throw new InvalidOperationException("Unsupported authorization method."); } - await GenerateAuthTokensAsync(cookieOptions, context, requestContext, firstTokenAuth.UserId, cancellation); + await GenerateAuthTokensAsync(cookieOptions, context, requestContext, firstTokenAuth.UserId, cancellationToken); return true; } private async Task LoginAsync(CookieOptions cookieOptions, HttpContext context, User user, - CancellationToken cancellation = default) + CancellationToken cancellationToken = default) { var requestContext = new RequestContextInfo(context, cookieOptions); if (user.TwoFactorAuthenticator == TwoFactorAuthenticator.None) { - await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id, cancellation); + await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id, cancellationToken); return TwoFactorAuthenticator.None; } - await StoreFirstAuthTokenInCache(user, requestContext, cancellation); + await StoreFirstAuthTokenInCache(user, requestContext, cancellationToken); return user.TwoFactorAuthenticator; } @@ -201,20 +201,20 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I User user, string password, string username, - CancellationToken cancellation = default) + CancellationToken cancellationToken = default) { var requestContext = new RequestContextInfo(context, cookieOptions); username = username.Trim(); - await VerifyUserOrThrowError(requestContext, user, password, username, cancellation); + await VerifyUserOrThrowError(requestContext, user, password, username, cancellationToken); - return await LoginAsync(cookieOptions, context, user, cancellation); + return await LoginAsync(cookieOptions, context, user, cancellationToken); } - public async Task RefreshTokenAsync(CookieOptions cookieOptions, HttpContext context, CancellationToken cancellation = default) + public async Task RefreshTokenAsync(CookieOptions cookieOptions, HttpContext context, CancellationToken cancellationToken = default) { const string defaultMessageError = "The session time has expired"; var requestContext = new RequestContextInfo(context, cookieOptions); - var authToken = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellation) ?? + var authToken = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellationToken) ?? throw new SecurityException(defaultMessageError); if (authToken.RefreshToken != requestContext.RefreshToken || @@ -222,7 +222,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I authToken.Ip != requestContext.Ip) { await RevokeAccessToken(authToken.AccessToken); - await cache.RemoveAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellation); + await cache.RemoveAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellationToken); cookieOptions.DropCookie(context, CookieNames.AccessToken); cookieOptions.DropCookie(context, CookieNames.RefreshToken); @@ -274,24 +274,24 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I authToken.AccessToken = token; authToken.RefreshToken = newRefreshToken; - await StoreAuthTokenInCache(authToken, cancellation); + await StoreAuthTokenInCache(authToken, cancellationToken); cookieOptions.SetCookie(context, CookieNames.AccessToken, authToken.AccessToken, expireIn); cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime)); } - public async Task LogoutAsync(CookieOptions cookieOptions, HttpContext context, CancellationToken cancellation = default) + public async Task LogoutAsync(CookieOptions cookieOptions, HttpContext context, CancellationToken cancellationToken = default) { var requestContext = new RequestContextInfo(context, cookieOptions); cookieOptions.DropCookie(context, CookieNames.AccessToken); cookieOptions.DropCookie(context, CookieNames.RefreshToken); - var authTokenStruct = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellation); + var authTokenStruct = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellationToken); if (authTokenStruct == null) return; await RevokeAccessToken(authTokenStruct.AccessToken); - await cache.RemoveAsync(requestContext.Fingerprint, cancellation); + await cache.RemoveAsync(requestContext.Fingerprint, cancellationToken); } } \ No newline at end of file diff --git a/Security/Services/OAuthService.cs b/Security/Services/OAuthService.cs index 6a2389b..ea5a984 100644 --- a/Security/Services/OAuthService.cs +++ b/Security/Services/OAuthService.cs @@ -58,7 +58,7 @@ public class OAuthService(ILogger logger, Dictionary ExchangeCodeForTokensAsync(string requestUri, string redirectUrl, string code, - string clientId, string secret, CancellationToken cancellation) + string clientId, string secret, CancellationToken cancellationToken) { var tokenRequest = new HttpRequestMessage(HttpMethod.Post, requestUri) { @@ -75,8 +75,8 @@ public class OAuthService(ILogger logger, Dictionary logger, Dictionary GetUserProfileAsync(string requestUri, string authHeader, string accessToken, OAuthProvider provider, - CancellationToken cancellation) + CancellationToken cancellationToken) { var request = new HttpRequestMessage(HttpMethod.Get, requestUri); @@ -97,8 +97,8 @@ public class OAuthService(ILogger logger, Dictionary logger, Dictionary + private Task StoreOAuthUserInCache(string key, OAuthUserExtension data, CancellationToken cancellationToken) => cache.SetAsync( key, JsonSerializer.SerializeToUtf8Bytes(data), absoluteExpirationRelativeToNow: TimeSpan.FromMinutes(15), - cancellationToken: cancellation); + cancellationToken: cancellationToken); public Uri GetProviderRedirect(CookieOptions cookieOptions, HttpContext context, string redirectUri, @@ -209,7 +209,7 @@ public class OAuthService(ILogger logger, Dictionary (x.Key, new Uri(redirectUri.TrimEnd('/') + "/?provider=" + (int)x.Key)))]; public async Task LoginOAuth(CookieOptions cookieOptions, HttpContext context, - string redirectUrl, string code, string state, CancellationToken cancellation = default) + string redirectUrl, string code, string state, CancellationToken cancellationToken = default) { var result = new LoginOAuth() { @@ -226,7 +226,7 @@ public class OAuthService(ILogger logger, Dictionary logger, Dictionary logger, Dictionary logger, Dictionary logger, Dictionary logger, Dictionary logger, Dictionary - GetOAuthUser(CookieOptions cookieOptions, HttpContext context, string token, CancellationToken cancellation = default) + GetOAuthUser(CookieOptions cookieOptions, HttpContext context, string token, CancellationToken cancellationToken = default) { var requestInfo = new RequestContextInfo(context, cookieOptions); - var result = await cache.GetAsync(token, cancellation); + var result = await cache.GetAsync(token, cancellationToken); var tokenFailedKey = $"{requestInfo.Fingerprint}_oauth_token_failed"; if (result == null) { var failedTokenAttemptsCount = await cache.GetAsync( tokenFailedKey, - cancellation) ?? 1; + cancellationToken) ?? 1; var failedTokenCacheExpiration = TimeSpan.FromHours(1); @@ -364,7 +364,7 @@ public class OAuthService(ILogger logger, Dictionary logger, Dictionary