diff --git a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs index f6d5a4f9d..2a5447d22 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs @@ -373,60 +373,14 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { /// [HttpPost("{botNames:required}/TwoFactorAuthentication/Confirmations/Accept")] [ProducesResponseType(typeof(GenericResponse>), 200)] - public async Task>>> TwoFactorAuthenticationConfirmationsAcceptPost(string botNames) { - if (string.IsNullOrEmpty(botNames)) { - ASF.ArchiLogger.LogNullError(nameof(botNames)); - - return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); - } - - HashSet bots = Bot.GetBots(botNames); - - if ((bots == null) || (bots.Count == 0)) { - return BadRequest(new GenericResponse>(false, string.Format(Strings.BotNotFound, botNames))); - } - - IList<(bool Success, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.HandleTwoFactorAuthenticationConfirmations(true))).ConfigureAwait(false); - - Dictionary result = new Dictionary(bots.Count); - - foreach (Bot bot in bots) { - (bool success, string message) = results[result.Count]; - result[bot.BotName] = new GenericResponse(success, message); - } - - return Ok(new GenericResponse>(result)); - } + public async Task>>> TwoFactorAuthenticationConfirmationsAcceptPost(string botNames) => await TwoFactorAuthenticationConfirmationsPost(botNames, true).ConfigureAwait(false); /// /// Denies 2FA confirmations of given bots, requires ASF 2FA module to be active on them. /// [HttpPost("{botNames:required}/TwoFactorAuthentication/Confirmations/Deny")] [ProducesResponseType(typeof(GenericResponse>), 200)] - public async Task>>> TwoFactorAuthenticationConfirmationsDenyPost(string botNames) { - if (string.IsNullOrEmpty(botNames)) { - ASF.ArchiLogger.LogNullError(nameof(botNames)); - - return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); - } - - HashSet bots = Bot.GetBots(botNames); - - if ((bots == null) || (bots.Count == 0)) { - return BadRequest(new GenericResponse>(false, string.Format(Strings.BotNotFound, botNames))); - } - - IList<(bool Success, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.HandleTwoFactorAuthenticationConfirmations(false))).ConfigureAwait(false); - - Dictionary result = new Dictionary(bots.Count); - - foreach (Bot bot in bots) { - (bool success, string message) = results[result.Count]; - result[bot.BotName] = new GenericResponse(success, message); - } - - return Ok(new GenericResponse>(result)); - } + public async Task>>> TwoFactorAuthenticationConfirmationsDenyPost(string botNames) => await TwoFactorAuthenticationConfirmationsPost(botNames, false).ConfigureAwait(false); /// /// Fetches 2FA tokens of given bots, requires ASF 2FA module to be active on them. @@ -457,5 +411,30 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return Ok(new GenericResponse>>(result)); } + + private async Task>>> TwoFactorAuthenticationConfirmationsPost(string botNames, bool accept) { + if (string.IsNullOrEmpty(botNames)) { + ASF.ArchiLogger.LogNullError(nameof(botNames)); + + return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); + } + + HashSet bots = Bot.GetBots(botNames); + + if ((bots == null) || (bots.Count == 0)) { + return BadRequest(new GenericResponse>(false, string.Format(Strings.BotNotFound, botNames))); + } + + IList<(bool Success, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.HandleTwoFactorAuthenticationConfirmations(accept))).ConfigureAwait(false); + + Dictionary result = new Dictionary(bots.Count); + + foreach (Bot bot in bots) { + (bool success, string message) = results[result.Count]; + result[bot.BotName] = new GenericResponse(success, message); + } + + return Ok(new GenericResponse>(result)); + } } }