refactor: rename cancellation to cancellationToken

This commit is contained in:
2025-02-01 21:18:56 +03:00
parent 2453b2bd51
commit a67b72b7fb
2 changed files with 56 additions and 56 deletions

View File

@ -58,7 +58,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
};
private static async Task<OAuthTokenResponse?> 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<OAuthService> logger, Dictionary<OAuthProvider
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MireaSchedule/1.0 (Winsomnia)");
var response = await httpClient.SendAsync(tokenRequest, cancellation);
var data = await response.Content.ReadAsStringAsync(cancellation);
var response = await httpClient.SendAsync(tokenRequest, cancellationToken);
var data = await response.Content.ReadAsStringAsync(cancellationToken);
if (!response.IsSuccessStatusCode)
throw new HttpRequestException(data);
@ -85,7 +85,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
}
private static async Task<OAuthUser?> 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<OAuthService> logger, Dictionary<OAuthProvider
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MireaSchedule/1.0 (Winsomnia)");
var response = await httpClient.SendAsync(request, cancellation);
var data = await response.Content.ReadAsStringAsync(cancellation);
var response = await httpClient.SendAsync(request, cancellationToken);
var data = await response.Content.ReadAsStringAsync(cancellationToken);
if (!response.IsSuccessStatusCode)
throw new HttpRequestException(data);
@ -167,12 +167,12 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
}
}
private Task StoreOAuthUserInCache(string key, OAuthUserExtension data, CancellationToken cancellation) =>
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<OAuthService> logger, Dictionary<OAuthProvider
[.. providers.Select(x => (x.Key, new Uri(redirectUri.TrimEnd('/') + "/?provider=" + (int)x.Key)))];
public async Task<LoginOAuth> 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<OAuthService> logger, Dictionary<OAuthProvider
{
Message = result.ErrorMessage,
Provider = null
}, cancellation);
}, cancellationToken);
return result;
}
@ -251,7 +251,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
result.ErrorMessage = "Invalid authorization request. Please try again later.";
cacheData.Message = result.ErrorMessage;
await StoreOAuthUserInCache(result.Token, cacheData, cancellation);
await StoreOAuthUserInCache(result.Token, cacheData, cancellationToken);
return result;
}
@ -271,7 +271,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
checksum
);
await StoreOAuthUserInCache(result.Token, cacheData, cancellation);
await StoreOAuthUserInCache(result.Token, cacheData, cancellationToken);
return result;
}
@ -280,7 +280,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
try
{
accessToken = await ExchangeCodeForTokensAsync(currentProviderStruct.TokenUrl, redirectUrl, code, providerInfo.ClientId,
providerInfo.Secret, cancellation);
providerInfo.Secret, cancellationToken);
}
catch (Exception ex)
{
@ -288,7 +288,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
payload.Provider,
checksum);
await StoreOAuthUserInCache(result.Token, cacheData, cancellation);
await StoreOAuthUserInCache(result.Token, cacheData, cancellationToken);
return result;
}
@ -300,14 +300,14 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
try
{
user = await GetUserProfileAsync(currentProviderStruct.UserInfoUrl, currentProviderStruct.AuthHeader, accessToken.AccessToken,
payload.Provider, cancellation);
payload.Provider, cancellationToken);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to retrieve user information from provider {Provider}",
payload.Provider);
await StoreOAuthUserInCache(result.Token, cacheData, cancellation);
await StoreOAuthUserInCache(result.Token, cacheData, cancellationToken);
return result;
}
@ -323,24 +323,24 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
IsSuccess = true,
User = user,
Provider = payload.Provider
}, cancellation);
}, cancellationToken);
return result;
}
public async Task<(OAuthUser? User, string? Message, bool IsSuccess, OAuthProvider? Provider)>
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<OAuthUserExtension>(token, cancellation);
var result = await cache.GetAsync<OAuthUserExtension>(token, cancellationToken);
var tokenFailedKey = $"{requestInfo.Fingerprint}_oauth_token_failed";
if (result == null)
{
var failedTokenAttemptsCount = await cache.GetAsync<int?>(
tokenFailedKey,
cancellation) ?? 1;
cancellationToken) ?? 1;
var failedTokenCacheExpiration = TimeSpan.FromHours(1);
@ -364,7 +364,7 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
await cache.SetAsync(tokenFailedKey,
failedTokenAttemptsCount + 1,
slidingExpiration: failedTokenCacheExpiration,
cancellationToken: cancellation);
cancellationToken: cancellationToken);
return (null, "Invalid or expired token.", false, null);
}
@ -406,18 +406,18 @@ public class OAuthService(ILogger<OAuthService> logger, Dictionary<OAuthProvider
result.Ip,
requestInfo.Ip);
await cache.RemoveAsync(token, cancellation);
await cache.RemoveAsync(token, cancellationToken);
return (null, "Invalid or expired token.", false, null);
}
await cache.RemoveAsync(tokenFailedKey, cancellation);
await cache.RemoveAsync(tokenFailedKey, cancellationToken);
result.Ip = requestInfo.Ip;
result.UserAgent = requestInfo.UserAgent;
result.Fingerprint = requestInfo.Fingerprint;
await StoreOAuthUserInCache(token, result, cancellation);
await StoreOAuthUserInCache(token, result, cancellationToken);
return (result.User, result.Message, result.IsSuccess, result.Provider);
}