示例#1
0
        public Task <bool> UpdateConfirmationsAsync()
        {
            return(Task.Run(async() =>
            {
                try
                {
                    _updateSemaphore.Wait();
                    _isUpdateInProcess = true;
                    App.Logger.Trace($"Authenticator.UpdateConfirmations");
                    State = AuthenticatorState.ConfimationUpdating;
                    int beforeCount = ConfirmationsSource.Count;
                    if (await GetConfitmations())
                    {
                        App.Logger.Info($"Authenticator.UpdateConfirmationsAsync Success. Confirmations: ({ConfirmationsSource.Count}) Added: [{ConfirmationsSource.Count - beforeCount}]");
                        State = AuthenticatorState.ConfimationUpdated;
                        _lastUpdate = DateTime.Now;
                        ConfirmationsEvent.Invoke(this, new AuthenticatorConfirmationsEventArgs(ConfirmationActionResult.Added, ConfirmationsSource.Count - beforeCount));

                        return true;
                    }
                    else
                    {
                        App.Logger.Error($"Authenticator.UpdateConfirmationsAsync Error");
                        State = AuthenticatorState.ConfimationError;

                        return false;
                    }
                }
                finally
                {
                    _isUpdateInProcess = false;
                    _updateSemaphore.Release();
                }
            }));
        }
示例#2
0
        public Task StartConfirmationAsync(int timeout, bool autoConfirm, ConfirmationAction action, CancellationToken cancellationToken)
        {
            App.Logger.Info($"Authenticator.StartConfirmation: confirm [{autoConfirm}] action [{action}] timeout [{timeout}]");
            if (!_isAutoStarted &&
                _autocancellationTokenSource != null &&
                !_autocancellationTokenSource.IsCancellationRequested)
            {
                _autocancellationTokenSource.Cancel();
            }
            _isAutoStarted = false;

            return(Task.Run(async() => {
                _autoupdateSemaphore.Wait();
                _isAutoUpdate = true;
                _isAutoConfirm = autoConfirm;
                _timeout = timeout;
                int errorCounter = 0;
                var error = false;
                while (!cancellationToken.IsCancellationRequested && !disposedValue)
                {
                    try
                    {
                        App.Logger.Trace($"Authenticator.StartConfirmation Error Counter: {errorCounter}");
                        var success = await UpdateConfirmationsAsync();
                        if (!success && (++errorCounter > MaxErrors))
                        {
                            State = AuthenticatorState.Error;
                            error = true;
                            break;
                        }
                        else
                        {
                            errorCounter = 0;
                        }

                        if (_isAutoConfirm)
                        {
                            IEnumerable <ConfirmationItem> confirmations = ConfirmationsSource.Where(c => c.Status == ConfirmationStatus.Waiting).ToList();
                            await ProcessConfirmationsAsync(confirmations, action, cancellationToken);
                        }
                        State = AuthenticatorState.Wait;
                        await Task.Delay(TimeSpan.FromSeconds(_timeout), cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        App.Logger.Info($"Authenticator.StartConfirmation: Canceled");
                        break;
                    }
                    catch (Exception ex)
                    {
                        App.Logger.Info($"Authenticator.StartConfirmation: Error {ex.Message}");
                        break;
                    }
                }
                _isAutoUpdate = false;
                State = error ? AuthenticatorState.Error : AuthenticatorState.Ready;
                _autoupdateSemaphore.Release();
            }));
        }
示例#3
0
        public Task <bool> ProcessConfirmationsAsync(IEnumerable <ConfirmationItem> confirmations, ConfirmationAction action, CancellationToken cancellationToken)
        {
            return(Task.Run(async() => {
                _confirmationSemaphore.Wait();
                _isConirmationInProcess = true;
                bool success = true;
                try
                {
                    App.Logger.Info($"Authenticator.ProcessConfirmationsAsync Action: {action}");
                    State = AuthenticatorState.ConfirmationProcessing;
                    if (confirmations == null || confirmations.Count() == 0)
                    {
                        App.Logger.Trace($"Authenticator.ProcessConfirmationsAsync [Empty]");
                        return success;
                    }

                    App.Logger.Info($"Authenticator.ProcessConfirmationsAsync Confirmations for process: {confirmations.Count()}");
                    int counter = 0;
                    foreach (ConfirmationItem confirmation in confirmations)
                    {
                        try
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            await ProcessConfirmationAsync(confirmation, action);
                            counter++;
                        }
                        catch (OperationCanceledException)
                        {
                            App.Logger.Info($"Authenticator.ProcessConfirmationsAsync Canceled");
                            break;
                        }
                    }

                    ConfirmationActionResult confirmationActionResult = action == ConfirmationAction.Accept ? ConfirmationActionResult.Accept : ConfirmationActionResult.Decline;
                    ConfirmationsEvent.Invoke(this, new AuthenticatorConfirmationsEventArgs(confirmationActionResult, counter));

                    return success;
                }
                finally
                {
                    State = AuthenticatorState.ConfirmationProcessed;
                    _isConirmationInProcess = false;
                    _confirmationSemaphore.Release();
                }
            }));
        }
示例#4
0
        public Authenticator(string path, string password)
        {
            if (String.IsNullOrEmpty(password.Trim()))
            {
                throw new ArgumentNullException(nameof(password));
            }
            if (String.IsNullOrEmpty(path.Trim()))
            {
                throw new ArgumentNullException(nameof(path));
            }

            _updateSemaphore       = new SemaphoreSlim(1, 1);
            _confirmationSemaphore = new SemaphoreSlim(1, 1);
            _autoupdateSemaphore   = new SemaphoreSlim(1, 1);

            //Collection Synhronization
            BindingOperations.EnableCollectionSynchronization(_confirmations, _collectionLock);

            _password = password;
            App.SteamGuardHelper.Initialize(path);
            State = AuthenticatorState.Ready;
        }
示例#5
0
        private async Task <bool> RefreshSession()
        {
            App.Logger.Trace($"Authenticato.RefreshSessionAsync");
            State = AuthenticatorState.SessionRefreshing;
            bool sessionResult = await App.SteamGuardHelper.CurrentSteamGuard.RefreshSessionAsync();

            App.Logger.Info($"Authenticator.RefreshSessionAsync Result: {sessionResult}");

            if (sessionResult == false)
            {
                State = AuthenticatorState.Relogin;
                bool reloginResult = await App.AuthWrapper.ReloginAsync(App.SteamGuardHelper.CurrentSteamGuard, Password);

                State = reloginResult ? AuthenticatorState.ReloginSuccess : AuthenticatorState.ReloginError;
            }
            else
            {
                State = AuthenticatorState.SessionRefreshed;
            }

            return(State == AuthenticatorState.SessionRefreshed || State == AuthenticatorState.ReloginSuccess);
        }
示例#6
0
 public AuthenticatorStateChangedEventArgs(AuthenticatorState state)
 {
     State = state;
 }