diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/ArchiSteamFarm.CustomPlugins.SignInWithSteam.csproj b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/ArchiSteamFarm.CustomPlugins.SignInWithSteam.csproj new file mode 100644 index 000000000..ada1e2f38 --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/ArchiSteamFarm.CustomPlugins.SignInWithSteam.csproj @@ -0,0 +1,24 @@ + + + Library + + + + + + + + + + + + + + + + + + + + + diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/AssemblyInfo.cs b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/AssemblyInfo.cs new file mode 100644 index 000000000..6ad4b4efb --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/AssemblyInfo.cs @@ -0,0 +1,24 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2023 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; + +[assembly: CLSCompliant(false)] diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamRequest.cs b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamRequest.cs new file mode 100644 index 000000000..050822578 --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamRequest.cs @@ -0,0 +1,30 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2023 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Newtonsoft.Json; + +namespace ArchiSteamFarm.CustomPlugins.SignInWithSteam.Data; + +public sealed class SignInWithSteamRequest { + [JsonProperty(Required = Required.Always)] + public Uri RedirectURL { get; private set; } = null!; +} diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamResponse.cs b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamResponse.cs new file mode 100644 index 000000000..b24ccccab --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/Data/SignInWithSteamResponse.cs @@ -0,0 +1,32 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2023 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Newtonsoft.Json; + +namespace ArchiSteamFarm.CustomPlugins.SignInWithSteam.Data; + +public sealed class SignInWithSteamResponse { + [JsonProperty(Required = Required.Always)] + public Uri ReturnURL { get; private set; } + + internal SignInWithSteamResponse(Uri returnURL) => ReturnURL = returnURL ?? throw new ArgumentNullException(nameof(returnURL)); +} diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamController.cs b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamController.cs new file mode 100644 index 000000000..113cf4bfe --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamController.cs @@ -0,0 +1,119 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2023 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Globalization; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using AngleSharp.Dom; +using ArchiSteamFarm.Core; +using ArchiSteamFarm.CustomPlugins.SignInWithSteam.Data; +using ArchiSteamFarm.IPC.Controllers.Api; +using ArchiSteamFarm.IPC.Responses; +using ArchiSteamFarm.Localization; +using ArchiSteamFarm.Steam; +using ArchiSteamFarm.Steam.Integration; +using ArchiSteamFarm.Web; +using ArchiSteamFarm.Web.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace ArchiSteamFarm.CustomPlugins.SignInWithSteam; + +[Route("/Api/Bot/{botName:required}/SignInWithSteam")] +public sealed class SignInWithSteamController : ArchiController { + [HttpPost] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.ServiceUnavailable)] + public async Task> Post(string botName, [FromBody] SignInWithSteamRequest request) { + ArgumentException.ThrowIfNullOrEmpty(botName); + ArgumentNullException.ThrowIfNull(request); + + Bot? bot = Bot.GetBot(botName); + + if (bot == null) { + return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botName))); + } + + if (!bot.IsConnectedAndLoggedOn) { + return StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, Strings.BotNotConnected)); + } + + // We've got a redirection, initiate OpenID procedure by following it + using HtmlDocumentResponse? challengeResponse = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request.RedirectURL).ConfigureAwait(false); + + if (challengeResponse?.Content == null) { + return StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); + } + + IAttr? paramsNode = challengeResponse.Content.SelectSingleNode("//input[@name='openidparams']/@value"); + + if (paramsNode == null) { + ASF.ArchiLogger.LogNullError(paramsNode); + + return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(paramsNode)))); + } + + string paramsValue = paramsNode.Value; + + if (string.IsNullOrEmpty(paramsValue)) { + ASF.ArchiLogger.LogNullError(paramsValue); + + return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(paramsValue)))); + } + + IAttr? nonceNode = challengeResponse.Content.SelectSingleNode("//input[@name='nonce']/@value"); + + if (nonceNode == null) { + ASF.ArchiLogger.LogNullError(nonceNode); + + return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(nonceNode)))); + } + + string nonceValue = nonceNode.Value; + + if (string.IsNullOrEmpty(nonceValue)) { + ASF.ArchiLogger.LogNullError(nonceValue); + + return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(nonceValue)))); + } + + Uri loginRequest = new(ArchiWebHandler.SteamCommunityURL, "/openid/login"); + + using StringContent actionContent = new("steam_openid_login"); + using StringContent modeContent = new("checkid_setup"); + using StringContent paramsContent = new(paramsValue); + using StringContent nonceContent = new(nonceValue); + + using MultipartFormDataContent data = new(); + + data.Add(actionContent, "action"); + data.Add(modeContent, "openid.mode"); + data.Add(paramsContent, "openidparams"); + data.Add(nonceContent, "nonce"); + + // Accept OpenID request presented and follow redirection back to the data we initially expected + BasicResponse? loginResponse = await bot.ArchiWebHandler.WebBrowser.UrlPost(loginRequest, data: data, requestOptions: WebBrowser.ERequestOptions.ReturnRedirections).ConfigureAwait(false); + + return loginResponse != null ? Ok(new GenericResponse(new SignInWithSteamResponse(loginResponse.FinalUri))) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); + } +} diff --git a/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamPlugin.cs b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamPlugin.cs new file mode 100644 index 000000000..502087dce --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.SignInWithSteam/SignInWithSteamPlugin.cs @@ -0,0 +1,43 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2023 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Composition; +using System.Threading.Tasks; +using ArchiSteamFarm.Core; +using ArchiSteamFarm.Plugins.Interfaces; +using JetBrains.Annotations; + +namespace ArchiSteamFarm.CustomPlugins.SignInWithSteam; + +[Export(typeof(IPlugin))] +[UsedImplicitly] +internal sealed class SignInWithSteamPlugin : IPlugin { + public string Name => nameof(SignInWithSteamPlugin); + + public Version Version => typeof(SignInWithSteamPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version)); + + public Task OnLoaded() { + ASF.ArchiLogger.LogGenericInfo("Loaded!"); + + return Task.CompletedTask; + } +} diff --git a/ArchiSteamFarm.sln b/ArchiSteamFarm.sln index 83aca52e9..da08c73f0 100644 --- a/ArchiSteamFarm.sln +++ b/ArchiSteamFarm.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchiSteamFarm.OfficialPlug EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchiSteamFarm.OfficialPlugins.ItemsMatcher", "ArchiSteamFarm.OfficialPlugins.ItemsMatcher\ArchiSteamFarm.OfficialPlugins.ItemsMatcher.csproj", "{A8BA3425-2EE4-4333-9905-8867EDFE327F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchiSteamFarm.CustomPlugins.SignInWithSteam", "ArchiSteamFarm.CustomPlugins.SignInWithSteam\ArchiSteamFarm.CustomPlugins.SignInWithSteam.csproj", "{27E35B1D-AA85-4D54-978B-520BF88DA0B7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -61,5 +63,11 @@ Global {A8BA3425-2EE4-4333-9905-8867EDFE327F}.DebugFast|Any CPU.Build.0 = Debug|Any CPU {A8BA3425-2EE4-4333-9905-8867EDFE327F}.Release|Any CPU.ActiveCfg = Release|Any CPU {A8BA3425-2EE4-4333-9905-8867EDFE327F}.Release|Any CPU.Build.0 = Release|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.DebugFast|Any CPU.ActiveCfg = Debug|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.DebugFast|Any CPU.Build.0 = Debug|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {27E35B1D-AA85-4D54-978B-520BF88DA0B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal