This commit is contained in:
Archi
2023-10-30 22:33:06 +01:00
parent 5a5a6e2074
commit 2f5fdf3185
2 changed files with 19 additions and 41 deletions

View File

@@ -1560,24 +1560,35 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
return false;
}
CAuthentication_AccessToken_GenerateForApp_Response? response = await ArchiHandler.GenerateAccessTokens(RefreshToken!).ConfigureAwait(false);
AccessTokenGenerateResult response;
if (string.IsNullOrEmpty(response?.access_token)) {
try {
response = await SteamClient.Authentication.GenerateAccessTokenForAppAsync(SteamID, RefreshToken!, true).ConfigureAwait(false);
} catch (Exception e) {
// The request has failed, in almost all cases this means our refresh token is no longer valid, relog needed
BotDatabase.RefreshToken = RefreshToken = null;
ArchiLogger.LogGenericWarningException(e);
ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(ArchiHandler.GenerateAccessTokens)));
BotDatabase.RefreshToken = RefreshToken = null;
await Connect(true).ConfigureAwait(false);
return false;
}
// TODO: Handle update of refresh token with next SK2 release
// ReSharper disable once RedundantSuppressNullableWarningExpression - required for .NET Framework
UpdateTokens(response!.access_token, RefreshToken!);
if (string.IsNullOrEmpty(response.AccessToken)) {
// The request has failed, in almost all cases this means our refresh token is no longer valid, relog needed
BotDatabase.RefreshToken = RefreshToken = null;
if (await ArchiWebHandler.Init(SteamID, SteamClient.Universe, response.access_token!, SteamParentalActive ? BotConfig.SteamParentalCode : null).ConfigureAwait(false)) {
ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(SteamClient.Authentication.GenerateAccessTokenForAppAsync)));
await Connect(true).ConfigureAwait(false);
return false;
}
UpdateTokens(response.AccessToken, !string.IsNullOrEmpty(response.RefreshToken) ? response.RefreshToken : RefreshToken!);
if (await ArchiWebHandler.Init(SteamID, SteamClient.Universe, response.AccessToken, SteamParentalActive ? BotConfig.SteamParentalCode : null).ConfigureAwait(false)) {
InitRefreshTokensTimer(AccessTokenValidUntil ?? now.AddHours(18));
return true;

View File

@@ -39,7 +39,6 @@ public sealed class ArchiHandler : ClientMsgHandler {
internal const byte MaxGamesPlayedConcurrently = 32; // This is limit introduced by Steam Network
private readonly ArchiLogger ArchiLogger;
private readonly SteamUnifiedMessages.UnifiedService<IAuthentication> UnifiedAuthenticationService;
private readonly SteamUnifiedMessages.UnifiedService<IChatRoom> UnifiedChatRoomService;
private readonly SteamUnifiedMessages.UnifiedService<IClanChatRooms> UnifiedClanChatRoomsService;
private readonly SteamUnifiedMessages.UnifiedService<ICredentials> UnifiedCredentialsService;
@@ -54,7 +53,6 @@ public sealed class ArchiHandler : ClientMsgHandler {
ArgumentNullException.ThrowIfNull(steamUnifiedMessages);
ArchiLogger = archiLogger ?? throw new ArgumentNullException(nameof(archiLogger));
UnifiedAuthenticationService = steamUnifiedMessages.CreateService<IAuthentication>();
UnifiedChatRoomService = steamUnifiedMessages.CreateService<IChatRoom>();
UnifiedClanChatRoomsService = steamUnifiedMessages.CreateService<IClanChatRooms>();
UnifiedCredentialsService = steamUnifiedMessages.CreateService<ICredentials>();
@@ -355,37 +353,6 @@ public sealed class ArchiHandler : ClientMsgHandler {
Client.Send(request);
}
internal async Task<CAuthentication_AccessToken_GenerateForApp_Response?> GenerateAccessTokens(string refreshToken) {
if (string.IsNullOrEmpty(refreshToken)) {
throw new ArgumentNullException(nameof(refreshToken));
}
if (Client == null) {
throw new InvalidOperationException(nameof(Client));
}
if (!Client.IsConnected || (Client.SteamID == null)) {
return null;
}
CAuthentication_AccessToken_GenerateForApp_Request request = new() {
refresh_token = refreshToken,
steamid = Client.SteamID
};
SteamUnifiedMessages.ServiceMethodResponse response;
try {
response = await UnifiedAuthenticationService.SendMessage(x => x.GenerateAccessTokenForApp(request)).ToLongRunningTask().ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
return null;
}
return response.Result == EResult.OK ? response.GetDeserializedResponse<CAuthentication_AccessToken_GenerateForApp_Response>() : null;
}
internal async Task<ulong> GetClanChatGroupID(ulong steamID) {
if ((steamID == 0) || !new SteamID(steamID).IsClanAccount) {
throw new ArgumentOutOfRangeException(nameof(steamID));