fix hoyolab user creation

This commit is contained in:
DismissedLight
2023-03-28 16:43:37 +08:00
parent b1ace71648
commit ab65a62c11
7 changed files with 61 additions and 64 deletions

View File

@@ -123,7 +123,7 @@ internal sealed class User : ObservableObject
internal static async Task<User?> CreateAsync(Cookie cookie, bool isOversea, CancellationToken token = default)
{
// 这里只负责创建实体用户,稍后在用户服务中保存到数据库
EntityUser entity = EntityUser.Create(cookie);
EntityUser entity = EntityUser.Create(cookie, isOversea);
entity.Aid = cookie.GetValueOrDefault(Cookie.STUID);
entity.Mid = isOversea ? entity.Aid : cookie.GetValueOrDefault(Cookie.MID);

View File

@@ -64,24 +64,11 @@ internal sealed class User : ISelectable
/// 创建一个新的用户
/// </summary>
/// <param name="cookie">cookie</param>
/// <param name="isOversea">是否为国际服</param>
/// <returns>新创建的用户</returns>
public static User Create(Cookie cookie)
public static User Create(Cookie cookie, bool isOversea)
{
_ = cookie.TryGetAsSToken(out Cookie? stoken);
_ = cookie.TryGetAsLToken(out Cookie? ltoken);
_ = cookie.TryGetAsCookieToken(out Cookie? cookieToken);
return new() { SToken = stoken, LToken = ltoken, CookieToken = cookieToken };
}
/// <summary>
/// 创建一个国际服用户
/// </summary>
/// <param name="cookie">cookie</param>
/// <returns>新创建的用户</returns>
public static User CreateOs(Cookie cookie)
{
_ = cookie.TryGetAsLegacySToken(out Cookie? stoken);
_ = cookie.TryGetAsSToken(isOversea, out Cookie? stoken);
_ = cookie.TryGetAsLToken(out Cookie? ltoken);
_ = cookie.TryGetAsCookieToken(out Cookie? cookieToken);

View File

@@ -194,7 +194,7 @@ internal class UserService : IUserService
public async Task<ValueResult<UserOptionResult, string>> ProcessInputCookieAsync(Cookie cookie, bool isOversea)
{
await ThreadHelper.SwitchToBackgroundAsync();
string? mid = cookie.GetValueOrDefault(Cookie.MID);
string? mid = cookie.GetValueOrDefault(isOversea ? Cookie.MID : Cookie.STUID);
if (mid == null)
{
@@ -208,7 +208,7 @@ internal class UserService : IUserService
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
if (cookie.TryGetAsSToken(out Cookie? stoken))
if (cookie.TryGetAsSToken(isOversea, out Cookie? stoken))
{
user.SToken = stoken;
user.LToken = cookie.TryGetAsLToken(out Cookie? ltoken) ? ltoken : user.LToken;

View File

@@ -66,7 +66,7 @@ internal sealed partial class LoginHoyoverseUserPage : Microsoft.UI.Xaml.Control
}
Cookie loginTicketCookie = Cookie.FromCoreWebView2Cookies(cookies);
loginTicketCookie["login_uid"] = uid;
loginTicketCookie[Cookie.LOGIN_UID] = uid;
// 使用 loginTicket 获取 stoken
Response<ListWrapper<NameToken>> multiTokenResponse = await Ioc.Default
@@ -80,12 +80,12 @@ internal sealed partial class LoginHoyoverseUserPage : Microsoft.UI.Xaml.Control
}
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
Cookie hoyoLabCookie = Cookie.Parse($"{Cookie.STUID}={uid};{Cookie.STOKEN}={multiTokenMap[Cookie.STOKEN]}");
Cookie hoyolabCookie = Cookie.FromSToken(uid, multiTokenMap[Cookie.STOKEN]);
// 处理 cookie 并添加用户
(UserOptionResult result, string nickname) = await Ioc.Default
.GetRequiredService<IUserService>()
.ProcessInputCookieAsync(hoyoLabCookie, true)
.ProcessInputCookieAsync(hoyolabCookie, true)
.ConfigureAwait(false);
Ioc.Default.GetRequiredService<INavigationService>().GoBack();

View File

@@ -67,7 +67,7 @@ internal sealed partial class LoginMihoyoUserPage : Microsoft.UI.Xaml.Controls.P
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
Cookie stokenV1 = Cookie.Parse($"{Cookie.STUID}={loginTicketCookie[Cookie.LOGIN_UID]};{Cookie.STOKEN}={multiTokenMap[Cookie.STOKEN]}");
Cookie stokenV1 = Cookie.FromSToken(loginTicketCookie[Cookie.LOGIN_UID], multiTokenMap[Cookie.STOKEN]);
Response<LoginResult> loginResultResponse = await Ioc.Default
.GetRequiredService<PassportClient>()

View File

@@ -201,7 +201,7 @@
Style="{StaticResource SubtitleTextBlockStyle}"
Text="{shcm:ResourceString Name=ViewUserDefaultDescription}"
Visibility="{Binding Users.Count, Converter={StaticResource Int32ToVisibilityRevertConverter}}"/>
<Grid>
<Grid Margin="6,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>

View File

@@ -94,52 +94,20 @@ internal sealed partial class Cookie
return new(cookieMap);
}
public bool TryGetAsSToken([NotNullWhen(true)] out Cookie? cookie)
public static Cookie FromSToken(string stuid, string stoken)
{
bool hasMid = TryGetValue(MID, out string? mid);
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
bool hasSTuid = TryGetValue(STUID, out string? stuid);
if (hasMid && hasSToken && hasSTuid)
SortedDictionary<string, string> cookieMap = new()
{
cookie = new Cookie(new()
{
[MID] = mid!,
[STOKEN] = stoken!,
[STUID] = stuid!,
});
[STUID] = stuid,
[STOKEN] = stoken,
};
return true;
}
cookie = null;
return false;
return new(cookieMap);
}
/// <summary>
/// 提取其中的 stoken 信息
/// Used for hoyolab account.
/// </summary>
/// <param name="cookie">A cookie contains stoken and stuid, without mid.</param>
/// <returns>是否获取成功</returns>
public bool TryGetAsLegacySToken([NotNullWhen(true)] out Cookie? cookie)
public bool TryGetAsSToken(bool isOversea, [NotNullWhen(true)] out Cookie? cookie)
{
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
bool hasSTuid = TryGetValue(STUID, out string? stuid);
if (hasSToken && hasSTuid)
{
cookie = new Cookie(new()
{
[STOKEN] = stoken!,
[STUID] = stuid!,
});
return true;
}
cookie = null;
return false;
return isOversea ? TryGetAsLegacySToken(out cookie) : TryGetAsSToken(out cookie);
}
public bool TryGetAsLToken([NotNullWhen(true)] out Cookie? cookie)
@@ -206,4 +174,46 @@ internal sealed partial class Cookie
{
return string.Join(';', inner.Select(kvp => $"{kvp.Key}={kvp.Value}"));
}
private bool TryGetAsSToken([NotNullWhen(true)] out Cookie? cookie)
{
bool hasMid = TryGetValue(MID, out string? mid);
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
bool hasSTuid = TryGetValue(STUID, out string? stuid);
if (hasMid && hasSToken && hasSTuid)
{
cookie = new Cookie(new()
{
[MID] = mid!,
[STOKEN] = stoken!,
[STUID] = stuid!,
});
return true;
}
cookie = null;
return false;
}
private bool TryGetAsLegacySToken([NotNullWhen(true)] out Cookie? cookie)
{
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
bool hasSTuid = TryGetValue(STUID, out string? stuid);
if (hasSToken && hasSTuid)
{
cookie = new Cookie(new()
{
[STOKEN] = stoken!,
[STUID] = stuid!,
});
return true;
}
cookie = null;
return false;
}
}