Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Bilibili provider #1044

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open

Conversation

Loongle
Copy link
Contributor

@Loongle Loongle commented Mar 21, 2025

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<PackageValidationBaselineVersion>9.2.0</PackageValidationBaselineVersion>
<!-- TODO Remove once published to NuGet.org -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>

builder.AddBilibili(options =>
{
ConfigureDefaults(builder, options);
options.ClientSecret = "ee9ee51ee0ceabdeeeb9459168eeeef7";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the need to specify this rather than use the defaults related to all the hashing?

Comment on lines +85 to +86
using var hmacsha256 = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(key));
var hash = hmacsha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using var hmacsha256 = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(key));
var hash = hmacsha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data));
var keyBytes = Encoding.UTF8.GetBytes(key);
var dataBytes = Encoding.UTF8.GetBytes(data);
var hash = HMACSHA256.HashData(keyBytes, dataBytes);

using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Claims;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using System.Security.Claims;
using System.Security.Claims;
using System.Security.Cryptography

private static string ComputeMd5(string input)
{
var inputBytes = Encoding.ASCII.GetBytes(input);
var hashBytes = System.Security.Cryptography.MD5.HashData(inputBytes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var hashBytes = System.Security.Cryptography.MD5.HashData(inputBytes);
var hashBytes = MD5.HashData(inputBytes);

{
var inputBytes = Encoding.ASCII.GetBytes(input);
var hashBytes = System.Security.Cryptography.MD5.HashData(inputBytes);
return Convert.ToHexStringLower(hashBytes).Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase).ToLower(CultureInfo.InvariantCulture);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToLower() is redundant as you're already using ToHexStringLower(), and IIRC, that won't return any dashes either, so the Replace() is also redundant.

Comment on lines +108 to +110
var signature = ComputeHmacSHA256(appSecret, signatureString);

return signature;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var signature = ComputeHmacSHA256(appSecret, signatureString);
return signature;
return ComputeHmacSHA256(appSecret, signatureString);

.Select(h => $"{h.Key}:{string.Join(",", h.Value)}")
.ToList();

var signatureString = string.Join("\n", headers);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just call this signature - it's evident it's a string.

Comment on lines +118 to +127
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Add("Access-Token", tokens.AccessToken);
request.Headers.Add("x-bili-accesskeyid", Options.ClientId);
request.Headers.Add("x-bili-content-md5", ComputeMd5(string.Empty));
request.Headers.Add("x-bili-signature-method", "HMAC-SHA256");
request.Headers.Add("x-bili-signature-nonce", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("x-bili-signature-version", "2.0");
request.Headers.Add("x-bili-timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("Host", "member.bilibili.com");
request.Headers.Add("Connection", "keep-alive");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Add("Access-Token", tokens.AccessToken);
request.Headers.Add("x-bili-accesskeyid", Options.ClientId);
request.Headers.Add("x-bili-content-md5", ComputeMd5(string.Empty));
request.Headers.Add("x-bili-signature-method", "HMAC-SHA256");
request.Headers.Add("x-bili-signature-nonce", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("x-bili-signature-version", "2.0");
request.Headers.Add("x-bili-timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("Host", "member.bilibili.com");
request.Headers.Add("Connection", "keep-alive");
var utcNow = TimeProvider.GetUtcNow();
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Add("Access-Token", tokens.AccessToken);
request.Headers.Add("x-bili-accesskeyid", Options.ClientId);
request.Headers.Add("x-bili-content-md5", ComputeMd5(string.Empty));
request.Headers.Add("x-bili-signature-method", "HMAC-SHA256");
request.Headers.Add("x-bili-signature-nonce", utcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("x-bili-signature-version", "2.0");
request.Headers.Add("x-bili-timestamp", utcNow.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture));
request.Headers.Add("Host", "member.bilibili.com");
request.Headers.Add("Connection", "keep-alive");

# for free to join this conversation on GitHub. Already have an account? # to comment
Development

Successfully merging this pull request may close these issues.

2 participants