chore(deps): update dependency microsoft.identitymodel.jsonwebtokens to 8.4.0
[ArchiSteamFarm.git] / ArchiSteamFarm / Steam / Integration / BotCredentialsProvider.cs
bloba11c3f4bd9729a31712e4d6649d87c7532d0409c
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2025 Łukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
24 using System;
25 using System.ComponentModel;
26 using System.Threading;
27 using System.Threading.Tasks;
28 using ArchiSteamFarm.Core;
29 using ArchiSteamFarm.Localization;
30 using ArchiSteamFarm.Storage;
31 using SteamKit2;
32 using SteamKit2.Authentication;
34 namespace ArchiSteamFarm.Steam.Integration;
36 internal sealed class BotCredentialsProvider : IAuthenticator {
37 private const byte MaxLoginFailures = 5;
39 private readonly Bot Bot;
40 private readonly CancellationTokenSource CancellationTokenSource;
42 private byte LoginFailures;
44 internal BotCredentialsProvider(Bot bot, CancellationTokenSource cancellationTokenSource) {
45 ArgumentNullException.ThrowIfNull(bot);
46 ArgumentNullException.ThrowIfNull(cancellationTokenSource);
48 Bot = bot;
49 CancellationTokenSource = cancellationTokenSource;
52 public async Task<bool> AcceptDeviceConfirmationAsync() {
53 if (Program.Service || (ASF.GlobalConfig?.Headless ?? GlobalConfig.DefaultHeadless)) {
54 // In headless/service mode, we always fallback to the code instead, as user can't confirm future popup from the next login procedure, and we never wait for current one
55 return false;
58 if (Bot.HasMobileAuthenticator || Bot.HasLoginCodeReady) {
59 // We don't want device confirmation under any circumstance, we can provide the code on our own
60 return false;
63 // Ask the user what they want
64 string input = await ProvideInput(ASF.EUserInputType.DeviceConfirmation, false).ConfigureAwait(false);
66 return input.Equals("Y", StringComparison.OrdinalIgnoreCase);
69 public async Task<string> GetDeviceCodeAsync(bool previousCodeWasIncorrect) => await ProvideInput(ASF.EUserInputType.TwoFactorAuthentication, previousCodeWasIncorrect).ConfigureAwait(false);
71 public async Task<string> GetEmailCodeAsync(string email, bool previousCodeWasIncorrect) => await ProvideInput(ASF.EUserInputType.SteamGuard, previousCodeWasIncorrect).ConfigureAwait(false);
73 private async Task<string> ProvideInput(ASF.EUserInputType inputType, bool previousCodeWasIncorrect) {
74 if (!Enum.IsDefined(inputType)) {
75 throw new InvalidEnumArgumentException(nameof(inputType), (int) inputType, typeof(ASF.EUserInputType));
78 if (previousCodeWasIncorrect && (++LoginFailures >= MaxLoginFailures)) {
79 EResult reason = inputType == ASF.EUserInputType.TwoFactorAuthentication ? EResult.TwoFactorCodeMismatch : EResult.InvalidLoginAuthCode;
81 Bot.ArchiLogger.LogGenericWarning(Strings.FormatBotUnableToLogin(reason, reason));
83 await CancellationTokenSource.CancelAsync().ConfigureAwait(false);
85 return "";
88 string? result = await Bot.RequestInput(inputType, previousCodeWasIncorrect).ConfigureAwait(false);
90 if (string.IsNullOrEmpty(result)) {
91 await CancellationTokenSource.CancelAsync().ConfigureAwait(false);
93 return "";
96 return result;