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(); })); }
private async Task <bool> GetConfitmations() { bool success = false; int counter = 0; bool isNeedRepeat; do { isNeedRepeat = false; if (++counter > 3) { App.Logger.Trace($"Authenticator.GetConfitmations Counter: [{counter}]"); break; } try { Confirmation[] confirmations = await App.SteamGuardHelper.CurrentSteamGuard.FetchConfirmationsAsync(); App.Logger.Info($"Authenticator.GetConfitmations Fetched: {confirmations.Length}"); foreach (var confirmation in confirmations) { ConfirmationItem item = new ConfirmationItem(confirmation) { Number = ConfirmationsSource.Count + 1 }; if (!ConfirmationsSource.Contains(item)) { ConfirmationsSource.Add(item); ConfirmationEvent.Invoke(this, new AuthenticatorConfirmationEventArgs(ConfirmationActionResult.Added, item)); } App.Logger.Trace($"Authenticator.GetConfitmations Added Confirmation: {item}"); } success = true; } catch (SteamGuardAccount.WGTokenInvalidException ex) { App.Logger.Warn($"Authenticator.GetConfitmations WGTokenInvalidException: {ex.Message}"); isNeedRepeat = await RefreshSession(); } catch (SteamGuardAccount.WGTokenExpiredException ex) { App.Logger.Warn($"Authenticator.GetConfitmations WGTokenExpiredException: {ex.Message}"); isNeedRepeat = await RefreshSession(); } catch (WebException ex) { App.Logger.Error($"Authenticator.GetConfitmations WebException: {ex.Message}"); break; } catch (Exception ex) { App.Logger.Error($"Authenticator.GetConfitmations Exception: {ex.Message}"); break; } } while (isNeedRepeat); return(success); }
public void CleanConfirmations() { App.Logger.Trace($"Authenticator.CleanConfirmations: [{ConfirmationsSource.Count}]"); try { ConfirmationsSource.Clear(); } catch (Exception ex) { App.Logger.Warn($"Authenticator.CleanConfirmations Error: [{ex.Message}]"); } }