From eecf99a3bd97dde77fd97abda82fc69dc9f244ad Mon Sep 17 00:00:00 2001 From: Marcos Cordeiro Date: Tue, 9 Jan 2024 21:09:58 -0300 Subject: [PATCH] Fixes isAuthenticated logic --- .../CrunchyrollAuthenticationHandler.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs b/Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs index 1143562..b963677 100644 --- a/Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs +++ b/Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs @@ -35,23 +35,26 @@ public CrunchyrollAuthenticationHandler(IOptions CreateAccessToken(string username = null, string password = null) { - var isAuthenticated = string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password); + var isAuthenticated = !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password); var keys = new List>() { new("grant_type", isAuthenticated ? "password" : "client_id"), new("scope", "offline_access") }; - - if(!string.IsNullOrEmpty(username)) keys.Add(new KeyValuePair(nameof(username), username)); - if (!string.IsNullOrEmpty(password)) keys.Add(new KeyValuePair(nameof(password), password)); + if (isAuthenticated) + { + keys.Add(new KeyValuePair(nameof(username), username)); + keys.Add(new KeyValuePair(nameof(password), password)); + } + using var formUrlEncodedContent = new FormUrlEncodedContent(keys); using var httpMessage = new HttpRequestMessage(); httpMessage.Method = HttpMethod.Post; httpMessage.Content = formUrlEncodedContent; httpMessage.RequestUri = new Uri("auth/v1/token", UriKind.Relative); - httpMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", isAuthenticated ? AuthenticationOptions.Value.AnonymousBasicAuthHeader : AuthenticationOptions.Value.AuthenticatedBasicAuthHeader); + httpMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", isAuthenticated ? AuthenticationOptions.Value.AuthenticatedBasicAuthHeader : AuthenticationOptions.Value.AnonymousBasicAuthHeader); using var authResponse = await AuthHttpClient.SendAsync(httpMessage); authResponse.EnsureSuccessStatusCode();