Esempio n. 1
0
 public Authenticator(AddAuthenticatorResponse authenticatorData, MobileSession session, string deviceId)
 {
     AuthenticatorData = authenticatorData;
     Session           = session;
     DeviceId          = deviceId;
     SteamWeb          = new SteamMobileWebAccess(session ?? new MobileSession());
 }
Esempio n. 2
0
        public static async Task RefreshSession()
        {
            var json          = "SESSION JSON CONTENT";
            var mobileSession = JsonConvert.DeserializeObject <MobileSession>(json);

            if (mobileSession?.HasEnoughInfo() == true)
            {
                var webAccess = new SteamMobileWebAccess(mobileSession);

                if (await webAccess.VerifySession())
                {
                    // Session is valid
                }
                else
                {
                    // Session expired, lets try refreshing it
                    if (await mobileSession.RefreshSession(webAccess))
                    {
                        // Session refreshed and valid again
                    }
                    else
                    {
                        // Failed to refresh the session, must log in again
                    }
                }
            }
        }
Esempio n. 3
0
        public static async Task LinkAuthenticator()
        {
            var json                = "LOGGED IN SESSION JSON CONTENT";
            var mobileSession       = JsonConvert.DeserializeObject <MobileSession>(json);
            var webAccess           = new SteamMobileWebAccess(mobileSession);
            var authenticatorLinker = new AuthenticatorLinker(webAccess);

            var authenticator = await authenticatorLinker.RequestToAddAuthenticator();

            // We just got a valid authenticator data
            // We should serialize and save this instance before doing anything else
            authenticator.SerializeToFile("MyAuthenticator.maFile2");

            // To accept and finalize this authenticator we need to have hold of the
            // text message sent to the account's associated phone number
            var smsCode = "SMS CODE HERE";

            try
            {
                await authenticatorLinker.FinalizeAddAuthenticator(authenticator, smsCode);

                // Authenticator finalized
            }
            catch (AuthenticatorLinkerException e)
            {
                if (e.ErrorCode == AuthenticatorLinkerErrorCode.BadSMSCode)
                {
                    // Bad SMS code
                    throw;
                }
            }
        }
Esempio n. 4
0
        public static async Task AddPhoneToAccount()
        {
            var json                = "LOGGED IN SESSION JSON CONTENT";
            var mobileSession       = JsonConvert.DeserializeObject <MobileSession>(json);
            var webAccess           = new SteamMobileWebAccess(mobileSession);
            var authenticatorLinker = new AuthenticatorLinker(webAccess);

            if (await authenticatorLinker.DoesAccountHasPhoneNumber())
            {
                // The account already has a phone number
                return;
            }

            var phoneNumber = "+98000000000";

            if (!await authenticatorLinker.RequestToAddPhoneNumber(phoneNumber))
            {
                // Request to add the phone number to account failed
                return;
            }

            // To accept and finalize this authenticator we need to have hold of the
            // text message sent to the account's associated phone number
            var smsCode = "SMS CODE HERE";

            if (!await authenticatorLinker.VerifyPhoneNumberBySMS(smsCode))
            {
                // Failed to finalize and verify added phone number
                // Probably bad SMS code; should try again
                return;
            }
        }
Esempio n. 5
0
        /// <summary>
        ///     Refreshes the Steam session. Necessary to perform confirmations if your session has expired or changed.
        /// </summary>
        /// <returns>true if the operation completed successfully; otherwise false</returns>
        public async Task <bool> RefreshSession(SteamMobileWebAccess mobileWebAccess)
        {
            try
            {
                var serverResponse = await OperationRetryHelper.Default.RetryOperationAsync(
                    () => new SteamWebAPI(mobileWebAccess)
                    .RequestObject <SteamWebAPIResponse <GetWGTokenResponse> >(
                        "IMobileAuthService",
                        SteamWebAccessRequestMethod.Post,
                        "GetWGToken",
                        "v0001",
                        new
                {
                    access_token = OAuthToken
                }
                        )
                    ).ConfigureAwait(false);

                if (string.IsNullOrEmpty(serverResponse?.Response?.Token) &&
                    string.IsNullOrEmpty(serverResponse?.Response?.TokenSecure))
                {
                    return(false);
                }

                SteamLogin = !string.IsNullOrWhiteSpace(serverResponse.Response?.Token)
                    ? SteamId + "%7C%7C" + serverResponse.Response.Token
                    : null;
                SteamLoginSecure = !string.IsNullOrWhiteSpace(serverResponse.Response?.TokenSecure)
                    ? SteamId + "%7C%7C" + serverResponse.Response.TokenSecure
                    : null;

                return(true);
            }
            catch (WebException e)
            {
                var response = e.Response as HttpWebResponse;

                //Redirecting -- likely to a steammobile:// URI
                if (response?.StatusCode == HttpStatusCode.Found)
                {
                    var location = response.Headers.Get("Location");

                    if (!string.IsNullOrEmpty(location))
                    {
                        // Our OAuth token has expired. This is given both when we must refresh our session, or the entire OAuth Token cannot be refreshed anymore.
                        // Thus, we should only throw this exception when we're attempting to refresh our session.
                        if (location == "steammobile://lostauth")
                        {
                            throw new TokenExpiredException(e);
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 6
0
        protected override async Task <bool> GetGuestSession()
        {
            // Get a new SessionId
            SteamWebAccess = SteamMobileWebAccess.GetGuest();
            (await OperationRetryHelper.Default
             .RetryOperationAsync(() => SteamWebAccess.FetchBinary(new SteamWebAccessRequest(LoginInitializeUrl)))
             .ConfigureAwait(false)).Dispose();

            return(!string.IsNullOrWhiteSpace(SteamWebAccess?.Session?.SessionId));
        }
Esempio n. 7
0
        /// <summary>
        ///     Refreshes the Steam session. Necessary to perform confirmations if your session has expired or changed.
        /// </summary>
        /// <returns>true if the operation completed successfully; otherwise false</returns>
        public async Task <bool> RefreshSession(SteamMobileWebAccess mobileWebAccess)
        {
            try
            {
                var serverResponse = await OperationRetryHelper.Default.RetryOperationAsync(
                    () => new SteamWebAPI(mobileWebAccess)
                    .RequestObject <SteamWebAPIResponse <GetWGTokenResponse> >(
                        "IMobileAuthService",
                        SteamWebAccessRequestMethod.Post,
                        "GetWGToken",
                        "v0001",
                        new
                {
                    access_token = OAuthToken
                }
                        )
                    ).ConfigureAwait(false);

                if (serverResponse == null)
                {
                    throw new TokenInvalidException();
                }

                if (string.IsNullOrEmpty(serverResponse?.Response?.Token) &&
                    string.IsNullOrEmpty(serverResponse?.Response?.TokenSecure))
                {
                    return(false);
                }

                SteamLogin = !string.IsNullOrWhiteSpace(serverResponse.Response?.Token)
                    ? SteamId + "%7C%7C" + serverResponse.Response.Token
                    : null;
                SteamLoginSecure = !string.IsNullOrWhiteSpace(serverResponse.Response?.TokenSecure)
                    ? SteamId + "%7C%7C" + serverResponse.Response.TokenSecure
                    : null;

                return(true);
            }
            catch (Exception e)
            {
                if (IsTokenExpired(e))
                {
                    throw new TokenInvalidException(e);
                }
            }

            return(false);
        }
Esempio n. 8
0
        /// <inheritdoc />
        protected override async Task <bool> ProcessLoginResponse(string response)
        {
            if (await base.ProcessLoginResponse(response).ConfigureAwait(false))
            {
                var loginResponse = JsonConvert.DeserializeObject <MobileLoginResponse>(response);

                if (!(loginResponse.OAuthToken?.OAuthToken?.Length > 0))
                {
                    throw new UserLoginException(UserLoginErrorCode.GeneralFailure, this);
                }

                SteamWebAccess = new SteamMobileWebAccess(new MobileSession(loginResponse.OAuthToken,
                                                                            SteamWebAccess?.Session?.SessionId));

                return(true);
            }

            return(false);
        }
Esempio n. 9
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AuthenticatorLinker" /> class.
 /// </summary>
 /// <param name="webAccess">A logged-in SteamMobileWebAccess instance.</param>
 public AuthenticatorLinker(SteamMobileWebAccess webAccess)
 {
     SteamWeb = webAccess ?? throw new ArgumentNullException(nameof(webAccess));
 }