Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Authentication/MicrosoftAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
var resultDict = new RetrieveDeviceCode
{
ClientId = ClientId,
UserCode = responseDict.UserCode,

Check warning on line 58 in Authentication/MicrosoftAuthentication.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
DeviceCode = responseDict.DeviceCode,
VerificationUri = responseDict.VerificationUri,
Message = responseDict.Message
Expand Down Expand Up @@ -95,7 +95,7 @@
if (tokenRes.IsSuccessStatusCode)
{
var tokenData = tokenJson.ToJsonEntry<GetTokenResponse>();
if (!string.IsNullOrEmpty(tokenData.AccessToken))

Check warning on line 98 in Authentication/MicrosoftAuthentication.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
return new GetTokenResponse
{
ExpiresIn = tokenData.ExpiresIn,
Expand All @@ -104,6 +104,31 @@
ClientId = deviceCodeInfo.ClientId
};
}
else
{
// 先判断是否正在等待中,直接使用 Contains,避免额外解析开销
if (!tokenJson.Contains("authorization_pending"))
{
try
{
var errorData = tokenJson.ToJsonEntry<GetTokenErrorResponse>();
switch (errorData.Error)
{
case "authorization_declined":
throw new Exception("用户拒绝了授权");
case "bad_verification_code":
throw new Exception("提交的 device_code 无效");
case "expired_token":
throw new TimeoutException("登录已超时,请重试"); // 避免轮询时正好过期导致抓不到异常
// 实际上还有其他的错误类型,但按理说不会出现,比如 slow_down
}
}
catch (Exception e)
{
throw new Exception("轮询获取 Token 错误:" + e.Message);
}
}
}

await Task.Delay(pollingInterval);
}
Expand Down
15 changes: 15 additions & 0 deletions Models/Authentication/GetTokenErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace StarLight_Core.Models.Authentication;

/// <summary>
/// 令牌错误信息
/// </summary>
public class GetTokenErrorResponse
{
/// <summary>
/// 验证令牌
/// </summary>
[JsonPropertyName("error")]
public string Error { get; set; }
}
44 changes: 44 additions & 0 deletions Skin/Fetchers/YggdrasilSkinFetcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using StarLight_Core.Models.Authentication;
using StarLight_Core.Models.Skin;
using StarLight_Core.Utilities;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Text;

namespace StarLight_Core.Skin.Fetchers;

/// <summary>
/// 外置皮肤获取器
/// </summary>
public class YggdrasilSkinFetcher
{
/// <summary>
/// 获取外置皮肤
/// </summary>
/// <param name="account">外置账户</param>
/// <returns>皮肤图片字节信息</returns>
public static async Task<byte[]> GetYggdrasilSkinAsync(YggdrasilAccount account)
{
return await GetYggdrasilSkinAsync(account.ServerUrl, account.Uuid);
}

/// <summary>
/// 获取外置皮肤
/// </summary>
/// <param name="serverUrl">服务器 Url</param>
/// <param name="uuid">外置账户 Uuid</param>
/// <returns></returns>
public static async Task<byte[]> GetYggdrasilSkinAsync(string serverUrl, string uuid)
{
var baseUrl = serverUrl.TrimEnd("/");

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 33 in Skin/Fetchers/YggdrasilSkinFetcher.cs

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'string' to 'char'
var skinJson = await HttpUtil.GetStringAsync($"{baseUrl}/sessionserver/session/minecraft/profile/{uuid}");
var skinUrl =
Encoding.UTF8.GetString(
Convert.FromBase64String(
skinJson.ToJsonEntry<ProfileJsonEntity>().Properties.First().Value))
.ToJsonEntry<SkinJsonEntity>()
.Textures.Skin.Url;
using var httpClient = new HttpClient();
return await httpClient.GetByteArrayAsync(skinUrl);
}
}
Loading