/// <summary> /// Checks if sessionId and token cookies are still valid. /// Sets cookie flag if they are invalid. /// </summary> /// <returns>true if cookies are valid; otherwise false</returns> bool CheckCookies() { // We still haven't re-authenticated if (cookiesAreInvalid) { return(false); } try { if (!SteamWeb.VerifyCookies()) { // Cookies are no longer valid Log.Warn("Cookies are invalid. Need to re-authenticate."); cookiesAreInvalid = true; SteamUser.RequestWebAPIUserNonce(); return(false); } } catch { // Even if exception is caught, we should still continue. Log.Warn("Cookie check failed. http://steamcommunity.com is possibly down."); } return(true); }
/// <summary> /// Send a request to the homepage of steam, check if there is something with "profiles/OurSteamID64/friends" /// If there is such a content inside the string, then we are still authenticated return true, so we know we are loggedon /// If there is not such a content inside the string, then we are not authenticated and try to authenticate to the web again /// To authenticate we have to request a new nonce, which will be needed to authenticate /// </summary> /// <returns></returns> public async Task <bool> RefreshSessionIfNeeded() { string response = await m_WebHelper.GetStringFromRequest("http://steamcommunity.com/my/").ConfigureAwait(false); bool isNotLoggedOn = response.Contains("Sign In"); if (isNotLoggedOn) { SteamUser.WebAPIUserNonceCallback userNonceCallback; try { userNonceCallback = await m_steamUser.RequestWebAPIUserNonce(); } catch (Exception e) { m_logger.Error(e.Message); return(false); } if (string.IsNullOrEmpty(userNonceCallback?.Nonce)) { m_logger.Warning("Usernonce is empty"); } m_logger.Warning("Reauthenticating..."); return(AuthenticateUser(m_steamClient, userNonceCallback?.Nonce)); } else { return(true); } }
internal async Task RefreshSessionIfNeeded() { if (!webBot.Initialized) { return; } if (await webBot.IsLoggedIn().ConfigureAwait(false)) { return; } SteamUser.WebAPIUserNonceCallback callback = null; for (byte i = 0; i < 3 && (callback == null || callback.Result != EResult.OK); i++) { callback = await steamUser.RequestWebAPIUserNonce(); } if (callback == null) { Log("Cant get webAPI user nonce: " + callback.Result, LogType.Error); } webBot.Init(callback.Nonce).Forget(); }
private async void OnLoginKeyReceived(SteamUser.LoginKeyCallback loginKeyCallback) { try { CookieManager.LoginKeyUniqueId = loginKeyCallback.UniqueID; CookieManager.CookiesExpired += async(sender, args) => { try { var webApiUserNonceCallback = await _steamUser.RequestWebAPIUserNonce(); if (webApiUserNonceCallback.Result != EResult.OK) { _logger.Warn( $"#{SequenceNumber} Invalid WebAPIUserNonceCallback result: {webApiUserNonceCallback.Result}"); } CookieManager.WebApiUserNonce = webApiUserNonceCallback.Nonce; await CookieManager.Refresh(); } catch (TaskCanceledException) { _logger.Warn($"#{SequenceNumber} Request new WebAPIUserNonce timeout."); } catch (Exception e) { _logger.Fatal($"#{SequenceNumber} Request new WebAPIUserNonce unhandled exception.", e); } }; await CookieManager.Refresh(); } catch (Exception e) { _logger.Fatal($"#{SequenceNumber} Fatal unhandled exception (OnLoginKeyReceived) : {e.Message}"); Restart(); } }
/// <summary> /// Checks if sessionId and token cookies are still valid. /// Sets cookie flag if they are invalid. /// </summary> /// <returns>true if cookies are valid; otherwise false</returns> bool CheckCookies() { // We still haven't re-authenticated if (CookiesAreInvalid) { return(false); } // Construct cookie container CookieContainer cookies = new CookieContainer(); cookies.Add(new Cookie("sessionid", sessionId, String.Empty, "steamcommunity.com")); cookies.Add(new Cookie("steamLogin", token, String.Empty, "steamcommunity.com")); try { if (!SteamWeb.VerifyCookies(cookies)) { // Cookies are no longer valid log.Warn("Cookies are invalid. Need to re-authenticate."); CookiesAreInvalid = true; SteamUser.RequestWebAPIUserNonce(); return(false); } else { return(true); } } catch { // Even if exception is caught, we should still continue. log.Warn("Cookie check failed. http://steamcommunity.com is possibly down."); return(true); } }
public static bool CheckCookies() { if (cookiesAreInvalid) { return(false); } try { if (!steamWeb.VerifyCookies()) { Console.WriteLine("[" + Program.BOTNAME + "] - Cookies are invalid. Need to re-authenticate."); cookiesAreInvalid = true; steamUser.RequestWebAPIUserNonce(); return(false); } } catch { Console.WriteLine("[" + Program.BOTNAME + "] - Cookie check failed. http://steamcommunity.com is possibly down."); } IsWebLoggedIn = true; return(true); }
//////////////////////////////////////////////////// // STEAM WEB INTERFACE //////////////////////////////////////////////////// // Copied this shit straight from ArchisSteamFarm, credit to him public override async void LoginWebInterface(ulong steamID) { if (!IsAuthenticated) { SteamUser.WebAPIUserNonceCallback callback; try { callback = await _steamUser.RequestWebAPIUserNonce(); } catch (Exception ex) { _log.Error(ex, "Unable to request Web API Nonce. Titan won't be able to execute Web API actions."); return; } if (string.IsNullOrWhiteSpace(callback?.Nonce)) { _log.Error("Received empty Web API Nonce. Titan won't be able to execute Web API actions."); return; } var sessionID = Convert.ToBase64String(Encoding.UTF8.GetBytes(steamID.ToString())); var sessionKey = CryptoHelper.GenerateRandomBlock(32); byte[] cryptedSessionKey; using (var rsa = new RSACrypto(KeyDictionary.GetPublicKey(_steamClient.Universe))) { cryptedSessionKey = rsa.Encrypt(sessionKey); } var loginKey = new byte[callback.Nonce.Length]; Array.Copy(Encoding.ASCII.GetBytes(callback.Nonce), loginKey, callback.Nonce.Length); // AES encrypt the login key with our session key var cryptedLoginKey = CryptoHelper.SymmetricEncrypt(loginKey, sessionKey); if (!Titan.Instance.WebHandle.AuthentificateUser( steamID, cryptedLoginKey, cryptedSessionKey, out var result )) { _log.Error("Failed to authentificate with Web API Nonce. " + "Titan won't be able to execute Web API actions."); return; } var token = result["token"].Value; var secureToken = result["tokensecure"].Value; if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(secureToken)) { _log.Error("Failed to authentificate with Web API Nonce. " + "Titan won't be able to execute Web API actions."); return; } Cookies.Add("sessionid", sessionID); Cookies.Add("steamLogin", token); Cookies.Add("steamLoginSecure", secureToken); if (!Titan.Instance.Options.Secure) { _log.Debug("Authorized with Steam Web API. Session ID: {id}", sessionID); } _log.Information("Successfully authorized with Steam Web API."); IsAuthenticated = true; } }