Files
Nebula-Auth/NebulaAuth/Model/MaClient.cs
T
Давид Чернопятов 06748d6f83 1.5.3 progress
- SessionHandler refactored. Now it used as global authorization API
- Added semaphore to SessionHandler
- WaitLogin dialog flow moved to SessionHandler
- MaClient and PortableMaClient authorization logic now separeted and works correctly
- Fixed App startup error was not localized and closed immediatly
- Fixed code progress bar was freezing due to low Dispatcher priority
- Fixed legacy serializer typo error in AccessToken field on write operation
2024-10-22 19:25:27 +03:00

172 lines
5.6 KiB
C#

using AchiesUtilities.Web.Models;
using AchiesUtilities.Web.Proxy;
using NebulaAuth.Model.Entities;
using SteamLib.Api.Mobile;
using SteamLib.Authentication;
using SteamLib.Core.Interfaces;
using SteamLib.Exceptions;
using SteamLib.ProtoCore.Services;
using SteamLib.SteamMobile.Confirmations;
using SteamLib.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace NebulaAuth.Model;
public static class MaClient
{
private static HttpClientHandler ClientHandler { get; }
private static HttpClient Client { get; }
private static DynamicProxy Proxy { get; }
public static ProxyData? DefaultProxy { get; set; }
public static HttpClientHandlerPair Chp => new(Client, ClientHandler);
static MaClient()
{
Proxy = new DynamicProxy();
var pair = ClientBuilder.BuildMobileClient(Proxy, null);
Client = pair.Client;
ClientHandler = pair.Handler;
}
public static void SetAccount(Mafile? account)
{
ClientHandler.CookieContainer.ClearAllCookies();
if (account != null)
{
if (account.SessionData != null)
{
ClientHandler.CookieContainer.SetSteamMobileCookiesWithMobileToken(account.SessionData);
}
else
{
ClientHandler.CookieContainer.ClearSteamCookies();
ClientHandler.CookieContainer.AddMinimalMobileCookies();
AdmissionHelper.TransferCommunityCookies(ClientHandler.CookieContainer);
}
Proxy.SetData(account.Proxy?.Data);
}
}
public static Task<IEnumerable<Confirmation>> GetConfirmations(Mafile mafile)
{
ValidateMafile(mafile);
SetProxy(mafile);
return SteamMobileConfirmationsApi.GetConfirmations(Client, mafile, mafile.SessionData!.SteamId);
}
public static Task LoginAgain(Mafile mafile, string password, bool savePassword, ICaptchaResolver? resolver)
{
SetProxy(mafile);
return SessionHandler.LoginAgain(Chp, mafile, password, savePassword);
}
public static Task RefreshSession(Mafile mafile)
{
ValidateMafile(mafile, true);
SetProxy(mafile);
return SessionHandler.RefreshMobileToken(Chp, mafile);
}
public static Task<bool> SendConfirmation(Mafile mafile, Confirmation confirmation, bool confirm)
{
ValidateMafile(mafile);
SetProxy(mafile);
return SteamMobileConfirmationsApi.SendConfirmation(Client, confirmation, mafile.SessionData!.SteamId, mafile, confirm);
}
public static Task<bool> SendMultipleConfirmation(Mafile mafile, IEnumerable<Confirmation> confirmations, bool confirm)
{
var enumerable = confirmations.ToList();
if (enumerable.Count == 0)
{
return Task.FromResult(result: false);
}
ValidateMafile(mafile);
SetProxy(mafile);
return SteamMobileConfirmationsApi.SendMultipleConfirmations(Client, enumerable, mafile.SessionData!.SteamId, mafile, confirm);
}
public static Task<RemoveAuthenticator_Response> RemoveAuthenticator(Mafile mafile)
{
ValidateMafile(mafile);
SetProxy(mafile);
if (mafile.RevocationCode == null)
{
throw new InvalidOperationException("This mafile does not have R-Code");
}
var token = mafile.SessionData!.GetMobileToken()!;
return SteamMobileApi.RemoveAuthenticator(Client, token.Value.Token, mafile.RevocationCode);
}
private static void SetProxy(Mafile mafile)
{
Proxy.SetData(mafile.Proxy?.Data ?? DefaultProxy);
}
private static void ValidateMafile(Mafile mafile, bool ignoreAccessToken = false)
{
if (mafile.SessionData == null) throw new SessionInvalidException();
if (mafile.SessionData.RefreshToken.IsExpired)
throw new SessionPermanentlyExpiredException();
if (ignoreAccessToken == false)
{
var access = mafile.SessionData.GetMobileToken();
if (access == null || access.Value.IsExpired)
throw new SessionPermanentlyExpiredException();
}
}
public static async Task<LoginConfirmationResult> ConfirmLoginRequest(Mafile mafile)
{
ValidateMafile(mafile);
SetProxy(mafile);
var token = mafile.SessionData!.GetMobileToken()!.Value;
var sessions = await SteamMobileAuthenticatorApi.GetAuthSessionsForAccount(Client, token.Token);
if (sessions.ClientIds.Count == 0)
{
return new LoginConfirmationResult
{
Error = LoginConfirmationError.NoRequests
};
}
if (sessions.ClientIds.Count > 1)
{
return new LoginConfirmationResult
{
Error = LoginConfirmationError.MoreThanOneRequest
};
}
var clientId = sessions.ClientIds.Single();
var clientInfo = await SteamMobileAuthenticatorApi.GetAuthSessionInfo(Client, token.Token, clientId);
var updateReq = new UpdateAuthSessionWithMobileConfirmation_Request
{
ClientId = clientId,
Confirm = true,
Persistence = 1,
Steamid = mafile.SessionData.SteamId.Steam64.ToUlong(),
Version = 1
};
await SteamMobileAuthenticatorApi.UpdateAuthSessionStatus(Client, token.Token, mafile.SharedSecret, updateReq);
return new LoginConfirmationResult
{
Country = clientInfo.Country,
IP = clientInfo.IP,
Success = true
};
}
}