Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Fixes isAuthenticated logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cordeiro committed Jan 10, 2024
1 parent 39dd082 commit eecf99a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,26 @@ public CrunchyrollAuthenticationHandler(IOptions<CrunchyrollAuthenticationOption

private async Task<string> 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<KeyValuePair<string, string>>()
{
new("grant_type", isAuthenticated ? "password" : "client_id"),
new("scope", "offline_access")
};

if(!string.IsNullOrEmpty(username)) keys.Add(new KeyValuePair<string, string>(nameof(username), username));
if (!string.IsNullOrEmpty(password)) keys.Add(new KeyValuePair<string, string>(nameof(password), password));

if (isAuthenticated)
{
keys.Add(new KeyValuePair<string, string>(nameof(username), username));
keys.Add(new KeyValuePair<string, string>(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();
Expand Down

0 comments on commit eecf99a

Please sign in to comment.