2024-03-16 23:06:13 +00:00
|
|
|
// ----------------------------------------------------------------------------------------------
|
2023-01-29 21:51:25 +00:00
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
2024-03-16 23:06:13 +00:00
|
|
|
// ----------------------------------------------------------------------------------------------
|
2024-03-26 00:15:17 +00:00
|
|
|
// |
|
2024-01-08 10:33:28 +00:00
|
|
|
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
|
2023-01-29 21:51:25 +00:00
|
|
|
// Contact: JustArchi@JustArchi.net
|
2024-03-26 00:15:17 +00:00
|
|
|
// |
|
2023-01-29 21:51:25 +00:00
|
|
|
// 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
|
2024-03-26 00:15:17 +00:00
|
|
|
// |
|
2023-01-29 21:51:25 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2024-03-26 00:15:17 +00:00
|
|
|
// |
|
2023-01-29 21:51:25 +00:00
|
|
|
// 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.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]
|
2023-11-16 20:49:15 +00:00
|
|
|
[ProducesResponseType<GenericResponse<SignInWithSteamResponse>>((int) HttpStatusCode.OK)]
|
|
|
|
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.BadRequest)]
|
|
|
|
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.ServiceUnavailable)]
|
2023-01-29 21:51:25 +00:00
|
|
|
public async Task<ActionResult<GenericResponse>> Post(string botName, [FromBody] SignInWithSteamRequest request) {
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(botName);
|
2023-01-29 21:51:25 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
|
|
|
|
|
|
Bot? bot = Bot.GetBot(botName);
|
|
|
|
|
|
|
|
if (bot == null) {
|
2024-08-05 00:45:53 +00:00
|
|
|
return BadRequest(new GenericResponse(false, Strings.FormatBotNotFound(botName)));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-08-05 00:45:53 +00:00
|
|
|
return StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, Strings.FormatErrorRequestFailedTooManyTimes(WebBrowser.MaxTries)));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IAttr? paramsNode = challengeResponse.Content.SelectSingleNode<IAttr>("//input[@name='openidparams']/@value");
|
|
|
|
|
|
|
|
if (paramsNode == null) {
|
|
|
|
ASF.ArchiLogger.LogNullError(paramsNode);
|
|
|
|
|
2024-08-05 00:45:53 +00:00
|
|
|
return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, Strings.FormatErrorObjectIsNull(nameof(paramsNode))));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string paramsValue = paramsNode.Value;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(paramsValue)) {
|
|
|
|
ASF.ArchiLogger.LogNullError(paramsValue);
|
|
|
|
|
2024-08-05 00:45:53 +00:00
|
|
|
return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, Strings.FormatErrorObjectIsNull(nameof(paramsValue))));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IAttr? nonceNode = challengeResponse.Content.SelectSingleNode<IAttr>("//input[@name='nonce']/@value");
|
|
|
|
|
|
|
|
if (nonceNode == null) {
|
|
|
|
ASF.ArchiLogger.LogNullError(nonceNode);
|
|
|
|
|
2024-08-05 00:45:53 +00:00
|
|
|
return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, Strings.FormatErrorObjectIsNull(nameof(nonceNode))));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string nonceValue = nonceNode.Value;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(nonceValue)) {
|
|
|
|
ASF.ArchiLogger.LogNullError(nonceValue);
|
|
|
|
|
2024-08-05 00:45:53 +00:00
|
|
|
return StatusCode((int) HttpStatusCode.InternalServerError, new GenericResponse(false, Strings.FormatErrorObjectIsNull(nameof(nonceValue))));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-08-05 00:45:53 +00:00
|
|
|
return loginResponse != null ? Ok(new GenericResponse<SignInWithSteamResponse>(new SignInWithSteamResponse(loginResponse.FinalUri))) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, Strings.FormatErrorRequestFailedTooManyTimes(WebBrowser.MaxTries)));
|
2023-01-29 21:51:25 +00:00
|
|
|
}
|
|
|
|
}
|