FIX: Show better error messages when decrypting posts

FIX: Show better error messages when decrypting posts

Follow-up to e361d75f88370bb2afdf7e33e2757027dca0e022.

diff --git a/assets/javascripts/discourse/controllers/activate-encrypt.js.es6 b/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
index 05d2db9..bb31174 100644
--- a/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
+++ b/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
@@ -19,6 +19,9 @@ export default Ember.Controller.extend(ModalFunctionality, {
     models.forEach(model => {
       model.state.decrypting = false;
       model.state.decrypted = true;
+      model.state.error = I18n.t(
+        "encrypt.preferences.status_enabled_but_inactive"
+      );
       model.scheduleRerender();
     });
     this.set("models", null);
diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 56d3754..ba07058 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -195,63 +195,67 @@ export default {
             (!state.encrypted || state.encrypted !== ciphertext)
           ) {
             state.encrypted = ciphertext;
-            state.decrypting = true;
 
-            getIdentity().then(identity => {
-              if (!identity) {
-                // Absence of private key means user did not activate encryption.
-                showModal("activate-encrypt", { model: this });
-                return;
-              }
+            if (!window.isSecureContext) {
+              state.decrypting = false;
+              state.decrypted = true;
+              state.error = I18n.t("encrypt.preferences.insecure_context");
+            } else {
+              state.decrypting = true;
+              getIdentity().then(identity => {
+                if (!identity) {
+                  // Absence of private key means user did not activate encryption.
+                  showModal("activate-encrypt", { model: this });
+                  return;
+                }
 
-              getTopicKey(topicId)
-                .then(key => decrypt(key, ciphertext))
-                .then(plaintext => {
-                  if (plaintext.signature) {
-                    getUserIdentities([plaintext.signed_by_name])
-                      .then(identities =>
-                        verify(
-                          identities[plaintext.signed_by_name].signPublic,
-                          plaintext,
-                          ciphertext
+                getTopicKey(topicId)
+                  .then(key => decrypt(key, ciphertext))
+                  .then(plaintext => {
+                    if (plaintext.signature) {
+                      getUserIdentities([plaintext.signed_by_name])
+                        .then(identities =>
+                          verify(
+                            identities[plaintext.signed_by_name].signPublic,
+                            plaintext,
+                            ciphertext
+                          )
                         )
-                      )
-                      .then(result => {
-                        verified[attrs.id] = checkMetadata(attrs, plaintext);
-                        if (!result) {
-                          verified[attrs.id].push({
-                            attr: "signature",
-                            actual: false,
-                            expected: true
-                          });
-                        }
-                      })
-                      .catch(() => {
-                        verified[attrs.id] = [
-                          {
-                            attr: "signature",
-                            actual: false,
-                            expected: true
+                        .then(result => {
+                          verified[attrs.id] = checkMetadata(attrs, plaintext);
+                          if (!result) {
+                            verified[attrs.id].push({
+                              attr: "signature",
+                              actual: false,
+                              expected: true
+                            });
                           }
-                        ];
-                      })
-                      .finally(() => this.scheduleRerender());
-                  }
+                        })
+                        .catch(() => {
+                          verified[attrs.id] = [
+                            {
+                              attr: "signature",
+                              actual: false,
+                              expected: true
+                            }
+                          ];
+                        })
+                        .finally(() => this.scheduleRerender());
+                    }
 
-                  return cookAsync(plaintext.raw);
-                })
-                .then(cooked => {
-                  state.decrypting = false;
-                  state.decrypted = cooked.string;
-                  this.scheduleRerender();
-                })
-                // Absence of topic key underlies a bigger error.
-                .catch(() => {
-                  state.decrypting = false;
-                  state.decrypted = true;
-                  this.scheduleRerender();
-                });
-            });
+                    return cookAsync(plaintext.raw);
+                  })
+                  .then(cooked => (state.decrypted = cooked.string))
+                  .catch(() => {
+                    state.decrypted = true;
+                    state.error = I18n.t("encrypt.decryption_failed");
+                  })
+                  .finally(() => {
+                    state.decrypting = false;
+                    this.scheduleRerender();
+                  });
+              });
+            }
           }
 
           if (state.decrypted && state.decrypted !== true) {
@@ -272,7 +276,7 @@ export default {
               "<div class='alert alert-error'>" +
               iconHTML("times") +
               " " +
-              I18n.t("encrypt.decryption_failed") +
+              state.error +
               "</div>" +
               attrs.cooked;
           }
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index b14faf0..531cc96 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -33,7 +33,7 @@ en:
         user_has_no_key: "Unfortunately {{username}} did not enable encrypted messages."
 
       preferences:
-        insecure_context: "Encryption cannot be enabled in an insecure context."
+        insecure_context: "Encryption cannot be used in an insecure context (not HTTPS)."
 
         status_enabled: "You have enabled encryption and activated it on this device."
         status_enabled_other: "This user has enabled encryption and can receive encrypted messages."

GitHub sha: 3f4f488b

1 Like