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

Commit

Permalink
Update crunchyroll authentication
Browse files Browse the repository at this point in the history
The old method of anonymous login was removed
  • Loading branch information
Marcos Cordeiro committed Jan 10, 2024
1 parent 4f1ff57 commit 42490a4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Wasari.App.Abstractions/AuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public class AuthenticationOptions
public string? Password { get; set; }

public bool HasCredentials => !string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password);

public string AnonymousBasicAuthHeader { get; set; } = "Y3Jfd2ViOg==";

public string AuthenticatedBasicAuthHeader { get; set; } = "b2VkYXJteHN0bGgxanZhd2ltbnE6OWxFaHZIWkpEMzJqdVY1ZFc5Vk9TNTdkb3BkSnBnbzE=";
}
2 changes: 0 additions & 2 deletions Wasari.Crunchyroll/AppExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static void AddCrunchyrollServices(this IServiceCollection serviceCollect
serviceCollection.AddHttpClient<CrunchyrollAuthenticationHandler>(c =>
{
c.BaseAddress = crunchyBaseAddres;
c.DefaultRequestHeaders.Add("Authorization",
"Basic a3ZvcGlzdXZ6Yy0teG96Y21kMXk6R21JSTExenVPVnRnTjdlSWZrSlpibzVuLTRHTlZ0cU8=");
});
serviceCollection.AddHttpClient<CrunchyrollApiService>(c => c.BaseAddress = crunchyBaseAddres)
.AddPolicyHandler(GetRetryPolicy())
Expand Down
38 changes: 19 additions & 19 deletions Wasari.Crunchyroll/CrunchyrollAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ public CrunchyrollAuthenticationHandler(IOptions<CrunchyrollAuthenticationOption

private HttpClient AuthHttpClient { get; }

private async Task<string> CreateAccessToken()
private async Task<string> CreateAccessToken(string username = null, string password = null)
{
using var formUrlEncodedContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "client_id") });
using var authResponse = await AuthHttpClient.PostAsync("auth/v1/token", formUrlEncodedContent);
authResponse.EnsureSuccessStatusCode();

await using var responseStream = await authResponse.Content.ReadAsStreamAsync();
var jsonDocument = await JsonDocument.ParseAsync(responseStream);
return jsonDocument.RootElement.GetProperty("access_token").GetString();
}

private async Task<string> CreateAccessToken(string username, string password)
{
using var formUrlEncodedContent = new FormUrlEncodedContent(new[]
var keys = new List<KeyValuePair<string, string>>()
{
new("grant_type", "password")
};

if(!string.IsNullOrEmpty(username)) keys.Add(new KeyValuePair<string, string>(nameof(username), username));
if (!string.IsNullOrEmpty(password))
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("scope", "offline_access")
});
keys.Add(new KeyValuePair<string, string>(nameof(password), password));
keys.Add(new KeyValuePair<string, string>("scope", "offline_access"));
}

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", string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) ? AuthenticationOptions.Value.AnonymousBasicAuthHeader : AuthenticationOptions.Value.AuthenticatedBasicAuthHeader);

using var authResponse = await AuthHttpClient.PostAsync("auth/v1/token", formUrlEncodedContent);
using var authResponse = await AuthHttpClient.SendAsync(httpMessage);
authResponse.EnsureSuccessStatusCode();

await using var responseStream = await authResponse.Content.ReadAsStreamAsync();
Expand Down

0 comments on commit 42490a4

Please sign in to comment.