From 662b09064aa0e2fcd5dda9495d0d1d98086da655 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ou=C5=A1ek?= <brousek@ics.muni.cz>
Date: Thu, 24 Mar 2022 12:42:10 +0100
Subject: [PATCH] fix: improve error messages

---
 dictionaries/privacyidea.definition.json  |  8 ++++----
 dictionaries/privacyidea.translation.json |  8 ++++----
 www/FormBuilder.php                       | 11 +++--------
 3 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/dictionaries/privacyidea.definition.json b/dictionaries/privacyidea.definition.json
index 2f2b5f6..7ec13d0 100644
--- a/dictionaries/privacyidea.definition.json
+++ b/dictionaries/privacyidea.definition.json
@@ -45,7 +45,7 @@
     "en": "Unable to proceed with Web Authn because the context is insecure!"
   },
   "alert_webauthn_unavailable": {
-    "en": "Could not load WebAuthn library. Please try again or use other token."
+    "en": "Could not load WebAuthn, your device probably does not support it. Please try again or use another method."
   },
   "alert_webAuthnSignRequest_error": {
     "en": "Error while signing WebAuthnSignRequest:"
@@ -54,7 +54,7 @@
     "en": "Unable to proceed with U2F because the context is insecure!"
   },
   "alert_u2f_unavailable": {
-    "en": "Could not load U2F library. Please try again or use other token."
+    "en": "Could not load U2F, your device probably does not support it. Please try again or use another method."
   },
   "alert_U2FSignRequest_error": {
     "en": "Error while signing U2FSignRequest:"
@@ -62,8 +62,8 @@
   "tryAgain": {
     "en": "Try Again"
   },
-  "incorrect_otp": {
-    "en": "You have entered incorrect OTP. Please try again or use another token."
+  "error_message": {
+    "en": "Verification was not successful. Please try again."
   },
   "error": {
     "en": "Error "
diff --git a/dictionaries/privacyidea.translation.json b/dictionaries/privacyidea.translation.json
index 38b5a08..b74df65 100644
--- a/dictionaries/privacyidea.translation.json
+++ b/dictionaries/privacyidea.translation.json
@@ -67,7 +67,7 @@
     "cs": "Není možné použít autentizaci WebAuthn, protože neodpovídá bezpečnostní kontext."
   },
   "alert_webauthn_unavailable": {
-    "cs": "Načtení WebAuthn knihovny se nezdařilo. Zkuste to znovu nebo použijte jiný token."
+    "cs": "Načtení WebAuthn se nezdařilo, nejspíš ho Vaše zařízení nepodporuje. Zkuste to znovu nebo použijte jinou metodu."
   },
   "alert_webAuthnSignRequest_error": {
     "cs": "Chyba při podepisování WebAuthnSignRequest:"
@@ -76,7 +76,7 @@
     "cs": "Není možné použít autentizaci U2F, protože neodpovídá bezpečnostní kontext."
   },
   "alert_u2f_unavailable": {
-    "cs": "Načtení U2F knihovny se nezdařilo. Zkuste to znovu nebo použijte jiný token."
+    "cs": "Načtení U2F se nezdařilo, nejspíš ho Vaše zařízení nepodporuje. Zkuste to znovu nebo použijte jinou metodu."
   },
   "alert_U2FSignRequest_error": {
     "cs": "Chyba při podepisování U2FSignRequest:"
@@ -86,8 +86,8 @@
     "de": "Nochmal versuchen",
     "nl": "Probeer opnieuw"
   },
-  "incorrect_otp": {
-    "cs": "Zadali jste nesprávné jednorázové heslo. Zkuste to znovu nebo použijte jiný token."
+  "error_message": {
+    "cs": "Ověření nebylo úspěšné. Zkuste to znovu nebo použijte jinou metodu."
   },
   "error": {
     "cs": "Chyba "
diff --git a/www/FormBuilder.php b/www/FormBuilder.php
index 11bd286..150702f 100644
--- a/www/FormBuilder.php
+++ b/www/FormBuilder.php
@@ -39,14 +39,9 @@ $tpl->data['errorCode'] = null;
 $tpl->data['errorMessage'] = null;
 
 if (!empty($state['privacyidea:privacyidea']['errorCode']) || !empty($state['privacyidea:privacyidea']['errorMessage'])) {
-    if (!empty($state['privacyidea:privacyidea']['errorCode'])) {
-        $tpl->data['errorCode'] = $state['privacyidea:privacyidea']['errorCode'];
-        $state['privacyidea:privacyidea']['errorCode'] = '';
-        $tpl->data['errorMessage'] = $state['privacyidea:privacyidea']['errorMessage'];
-    } else {
-        $tpl->data['errorCode'] = '';
-        $tpl->data['errorMessage'] = $tpl->t('{privacyidea:privacyidea:incorrect_otp}');
-    }
+    $tpl->data['errorCode'] = ($state['privacyidea:privacyidea']['errorCode'] ?? null) ?: '';
+    $state['privacyidea:privacyidea']['errorCode'] = ''
+    $tpl->data['errorMessage'] = $tpl->t('{privacyidea:privacyidea:error_message}');
     $state['privacyidea:privacyidea']['errorMessage'] = '';
     $stateId = State::saveState($state, 'privacyidea:privacyidea');
 }
-- 
GitLab