Exemple #1
0
        /// <summary>
        ///     Resets this instance state and allows for a new authentication process to start.
        /// </summary>
        /// <returns><c>true</c> if a new guest session identification string retrieved; otherwise, <c>false</c>.</returns>
        public virtual async Task <bool> Reset()
        {
            // Lock this instance
            await LockObject.WaitAsync().ConfigureAwait(false);

            try
            {
                ResetStates();

                return(await GetGuestSession().ConfigureAwait(false));
            }
            finally
            {
                // Unlock this instance
                LockObject.Release();
            }
        }
Exemple #2
0
        /// <summary>
        ///     Downloads the captcha image associated with the latest login attempt.
        /// </summary>
        /// <returns>An array of bytes representing an image in PNG file format</returns>
        public virtual async Task <byte[]> DownloadCaptchaImage()
        {
            if (CachedCaptchaImage != null)
            {
                return(CachedCaptchaImage);
            }

            if (string.IsNullOrWhiteSpace(CaptchaGID))
            {
                return(null);
            }

            // Lock this instance
            await LockObject.WaitAsync().ConfigureAwait(false);

            CachedCaptchaImage = null;

            try
            {
                CachedCaptchaImage = (await OperationRetryHelper.Default.RetryOperationAsync(
                                          () => SteamWebAccess.FetchBinary(
                                              new SteamWebAccessRequest(
                                                  LoginCaptchaUrl,
                                                  SteamWebAccessRequestMethod.Get,
                                                  new QueryStringBuilder
                {
                    { "gid", CaptchaGID }
                }
                                                  ))
                                          ).ConfigureAwait(false)).ToArray();

                return(CachedCaptchaImage);
            }
            finally
            {
                // Unlock this instance
                LockObject.Release();
            }
        }
Exemple #3
0
        /// <summary>
        ///     Tries to authenticate a user with the provided user credentials and returns session data corresponding to a
        ///     successful login; fails if information provided is not enough or service is unavailable.
        /// </summary>
        /// <param name="credentials">The credentials to be used for login process.</param>
        /// <returns>Logged in session to be used with other classes</returns>
        /// <exception cref="ArgumentException">
        ///     Username and/or password is missing. - credentials
        ///     or
        ///     Two factor authentication code is required for login process to continue. - credentials
        ///     or
        ///     Email verification code is required for login process to continue. - credentials
        ///     or
        ///     Captcha is required for login process to continue. - credentials
        /// </exception>
        /// <exception cref="UserLoginException">
        ///     Raises when there is a problem with login process or there is a need for more information. Capture and decide if
        ///     you should repeat the process.
        /// </exception>
        public virtual async Task <WebSession> DoLogin(LoginCredentials credentials)
        {
            if (string.IsNullOrWhiteSpace(credentials.UserName) || string.IsNullOrWhiteSpace(credentials.Password))
            {
                throw new ArgumentException("Username and/or password is missing.", nameof(credentials));
            }

            if (RequiresTwoFactorAuthenticationCode &&
                string.IsNullOrWhiteSpace(credentials.TwoFactorAuthenticationCode))
            {
                throw new ArgumentException("Two factor authentication code is required for login process to continue.",
                                            nameof(credentials));
            }

            if (RequiresEmailVerification && string.IsNullOrWhiteSpace(credentials.EmailVerificationCode))
            {
                throw new ArgumentException("Email verification code is required for login process to continue.",
                                            nameof(credentials));
            }

            if (RequiresCaptchaCode && string.IsNullOrWhiteSpace(credentials.CaptchaCode))
            {
                throw new ArgumentException("Captcha is required for login process to continue.", nameof(credentials));
            }

            // Lock this instance
            await LockObject.WaitAsync().ConfigureAwait(false);

            try
            {
                // Retrieve guest cookies for login process if missing
                if (string.IsNullOrEmpty(SteamWebAccess?.Session?.SessionId))
                {
                    await GetGuestSession().ConfigureAwait(false);
                }

                var loginRequest = await ConstructLoginRequest(credentials).ConfigureAwait(false);

                var loginResponse = loginRequest != null
                    ? await OperationRetryHelper.Default
                                    .RetryOperationAsync(() => SteamWebAccess.FetchString(loginRequest)).ConfigureAwait(false)
                    : null;

                if (loginResponse == null)
                {
                    throw new UserLoginException(UserLoginErrorCode.GeneralFailure, this);
                }

                if (!await ProcessLoginResponse(loginResponse).ConfigureAwait(false))
                {
                    throw new UserLoginException(UserLoginErrorCode.BadCredentials, this);
                }

                var sessionData = SteamWebAccess.Session;
                ResetStates();
                return(sessionData);
            }
            finally
            {
                // Unlock this instance
                LockObject.Release();
            }
        }