public async Task <IActionResult> Setup([FromBody] RequestSetupGoogle2FaRequest model) { try { if (await _google2FaService.ClientHasEnabledAsync(model.ClientId)) { throw new Google2FaAlreadySetException(model.ClientId, "Cannot set up 2FA because it's already set up"); } if (_confirmationCodesServiceSettings.Google2FaSetupDisabled) { throw new Exception("Google 2FA setup is disabled"); } var manualEntryKey = await _google2FaService.CreateAsync(model.ClientId); return(Ok(new RequestSetupGoogle2FaResponse { ManualEntryKey = manualEntryKey })); } catch (Exception exception) { _log.WriteError(nameof(Setup), new { model.ClientId }, exception); switch (exception) { case Google2FaAlreadySetException _: return(BadRequest()); } throw; } }
public async Task <CommandHandlingResult> Handle(ValidateConfirmationCommand command, IEventPublisher publisher) { if (!await _google2FaService.ClientHasEnabledAsync(command.ClientId)) { publisher.PublishEvent(new ConfirmationValidationFailedEvent { Id = command.Id, ClientId = command.ClientId, Reason = ValidationFailReason.SecondFactorNotSetUp }); return(CommandHandlingResult.Ok()); } if (await _blacklistService.IsClientBlockedAsync(command.ClientId)) { publisher.PublishEvent(new ConfirmationValidationFailedEvent { Id = command.Id, ClientId = command.ClientId, Reason = ValidationFailReason.InvalidConfirmation }); } if (await _google2FaService.CheckCodeAsync(command.ClientId, command.Confirmation)) { await _blacklistService.ClientSucceededAsync(command.ClientId); publisher.PublishEvent(new ConfirmationValidationPassedEvent { Id = command.Id, ClientId = command.ClientId }); } else { await _blacklistService.ClientFailedAsync(command.ClientId); publisher.PublishEvent(new ConfirmationValidationFailedEvent { Id = command.Id, ClientId = command.ClientId, Reason = ValidationFailReason.InvalidConfirmation }); } return(CommandHandlingResult.Ok()); }