Skip to content

Commit

Permalink
Kill API keys entirely
Browse files Browse the repository at this point in the history
Economy bans we can handle ourselves server-side
  • Loading branch information
JustArchi committed Dec 3, 2023
1 parent ddd34d4 commit 27f965d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 131 deletions.
19 changes: 0 additions & 19 deletions ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.IPC.Responses;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.OfficialPlugins.ItemsMatcher.Data;
Expand Down Expand Up @@ -870,24 +869,6 @@ internal void TriggerMatchActivelyEarlier() {
return false;
}

// Bot must have valid API key (e.g. not being restricted account)
bool? hasValidApiKey = await Bot.ArchiWebHandler.HasValidApiKey().ConfigureAwait(false);

if (hasValidApiKey != true) {
Bot.ArchiLogger.LogGenericTrace(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, $"{nameof(Bot.ArchiWebHandler.HasValidApiKey)}: {hasValidApiKey?.ToString() ?? "null"}"));

return hasValidApiKey.HasValue ? false : null;
}

// Bot can't be trade banned
(bool _, bool? Result) economyBan = await Bot.ArchiWebHandler.CachedEconomyBan.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);

if (economyBan.Result != false) {
Bot.ArchiLogger.LogGenericTrace(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, $"{nameof(economyBan)}: {economyBan.Result?.ToString() ?? "null"}"));

return economyBan.Result.HasValue ? false : null;
}

return true;
}

Expand Down
3 changes: 0 additions & 3 deletions ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3213,9 +3213,6 @@ private async void OnLoggedOn(SteamUser.LoggedOnCallback callback) {
return;
}

// Pre-fetch API key for future usage if possible
Utilities.InBackground(ArchiWebHandler.HasValidApiKey);

if ((GamesRedeemerInBackgroundTimer == null) && BotDatabase.HasGamesToRedeemInBackground) {
Utilities.InBackground(() => RedeemGamesInBackground());
}
Expand Down
115 changes: 6 additions & 109 deletions ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public sealed class ArchiWebHandler : IDisposable {
private const ushort MaxItemsInSingleInventoryRequest = 5000;
private const byte MinimumSessionValidityInSeconds = 10;
private const string SteamAppsService = "ISteamApps";
private const string SteamUserService = "ISteamUser";
private const string TwoFactorService = "ITwoFactorService";

[PublicAPI]
Expand All @@ -73,12 +72,6 @@ public sealed class ArchiWebHandler : IDisposable {
[PublicAPI]
public ArchiCacheable<string> CachedAccessToken { get; }

[PublicAPI]
public ArchiCacheable<string> CachedApiKey { get; }

[PublicAPI]
public ArchiCacheable<bool?> CachedEconomyBan { get; }

[PublicAPI]
public WebBrowser WebBrowser { get; }

Expand All @@ -97,16 +90,12 @@ internal ArchiWebHandler(Bot bot) {
Bot = bot;

CachedAccessToken = new ArchiCacheable<string>(ResolveAccessToken, TimeSpan.FromHours(6));
CachedApiKey = new ArchiCacheable<string>(ResolveApiKey, TimeSpan.FromHours(6));
CachedEconomyBan = new ArchiCacheable<bool?>(ResolveEconomyBan, TimeSpan.FromHours(6));

WebBrowser = new WebBrowser(bot.ArchiLogger, ASF.GlobalConfig?.WebProxy);
}

public void Dispose() {
CachedAccessToken.Dispose();
CachedApiKey.Dispose();
CachedEconomyBan.Dispose();
SessionSemaphore.Dispose();
WebBrowser.Dispose();
}
Expand Down Expand Up @@ -467,14 +456,14 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a

[PublicAPI]
public async Task<HashSet<TradeOffer>?> GetTradeOffers(bool? activeOnly = null, bool? receivedOffers = null, bool? sentOffers = null, bool? withDescriptions = null) {
(_, string? steamApiKey) = await CachedApiKey.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);
(_, string? accessToken) = await CachedAccessToken.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);

if (string.IsNullOrEmpty(steamApiKey)) {
if (string.IsNullOrEmpty(accessToken)) {
return null;
}

Dictionary<string, object?> arguments = new(StringComparer.Ordinal) {
{ "key", steamApiKey }
{ "access_token", accessToken }
};

if (activeOnly.HasValue) {
Expand Down Expand Up @@ -663,13 +652,6 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
return result;
}

[PublicAPI]
public async Task<bool?> HasValidApiKey() {
(_, string? steamApiKey) = await CachedApiKey.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);

return !string.IsNullOrEmpty(steamApiKey);
}

[PublicAPI]
public async Task<bool> JoinGroup(ulong groupID) {
if ((groupID == 0) || !new SteamID(groupID).IsClanAccount) {
Expand Down Expand Up @@ -1821,14 +1803,14 @@ internal async Task<byte> GetCardCountForGame(uint appID) {
throw new ArgumentOutOfRangeException(nameof(steamID));
}

(_, string? steamApiKey) = await CachedApiKey.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);
(_, string? accessToken) = await CachedAccessToken.GetValue(ECacheFallback.SuccessPreviously).ConfigureAwait(false);

if (string.IsNullOrEmpty(steamApiKey)) {
if (string.IsNullOrEmpty(accessToken)) {
return null;
}

Dictionary<string, object?> arguments = new(!string.IsNullOrEmpty(tradeToken) ? 3 : 2, StringComparer.Ordinal) {
{ "key", steamApiKey },
{ "access_token", accessToken },
{ "steamid_target", steamID }
};

Expand Down Expand Up @@ -2283,8 +2265,6 @@ internal void OnDisconnected() {
Initialized = false;

Utilities.InBackground(() => CachedAccessToken.Reset());
Utilities.InBackground(() => CachedApiKey.Reset());
Utilities.InBackground(() => CachedEconomyBan.Reset());
}

internal void OnVanityURLChanged(string? vanityURL = null) => VanityURL = !string.IsNullOrEmpty(vanityURL) ? vanityURL : null;
Expand Down Expand Up @@ -2720,89 +2700,6 @@ private async Task<bool> RefreshSession() {
}
}

private async Task<(bool Success, bool? Result)> ResolveEconomyBan(CancellationToken cancellationToken = default) {
(_, string? steamApiKey) = await CachedApiKey.GetValue(ECacheFallback.SuccessPreviously, cancellationToken).ConfigureAwait(false);

if (string.IsNullOrEmpty(steamApiKey)) {
return (false, null);
}

if (!Initialized) {
byte connectionTimeout = ASF.GlobalConfig?.ConnectionTimeout ?? GlobalConfig.DefaultConnectionTimeout;

for (byte i = 0; (i < connectionTimeout) && !Initialized && Bot.IsConnectedAndLoggedOn; i++) {
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
}

if (!Initialized) {
Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed);

return (false, null);
}
}

Dictionary<string, object?> arguments = new(2, StringComparer.Ordinal) {
{ "key", steamApiKey },
{ "steamids", Bot.SteamID }
};

KeyValue? response = null;

for (byte i = 0; (i < WebBrowser.MaxTries) && (response == null); i++) {
if ((i > 0) && (WebLimiterDelay > 0)) {
await Task.Delay(WebLimiterDelay, cancellationToken).ConfigureAwait(false);
}

using WebAPI.AsyncInterface service = Bot.SteamConfiguration.GetAsyncWebAPIInterface(SteamUserService);

service.Timeout = WebBrowser.Timeout;

try {
response = await WebLimitRequest(
WebAPI.DefaultBaseAddress,

// ReSharper disable once AccessToDisposedClosure
async () => await service.CallAsync(HttpMethod.Get, "GetPlayerBans", args: arguments).ConfigureAwait(false), cancellationToken
).ConfigureAwait(false);
} catch (TaskCanceledException e) {
Bot.ArchiLogger.LogGenericDebuggingException(e);
} catch (Exception e) {
Bot.ArchiLogger.LogGenericWarningException(e);
}
}

if (response == null) {
Bot.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries));

return (false, null);
}

List<KeyValue> players = response["players"].Children;

if (players.Count != 1) {
Bot.ArchiLogger.LogNullError(players);

return (false, null);
}

string? economyBanText = players[0]["EconomyBan"].AsString();

switch (economyBanText) {
case null:
Bot.ArchiLogger.LogNullError(economyBanText);

return (false, null);
case "none":
return (true, false);
case "banned":
return (true, true);
default:
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(economyBanText), economyBanText));

return (false, null);
}
}

private async Task<bool> UnlockParentalAccount(string parentalCode) {
ArgumentException.ThrowIfNullOrEmpty(parentalCode);

Expand Down

0 comments on commit 27f965d

Please sign in to comment.