From 88e83b65114a0b5fc079e1177e635f564225cbc5 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 7 Apr 2015 12:51:36 +0200 Subject: [PATCH] Improve error message upon private key import --- src/js/controller/login/login-new-device.js | 19 +++++++++++++++---- src/js/directive/login-new-device.js | 1 - 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/js/controller/login/login-new-device.js b/src/js/controller/login/login-new-device.js index 4300882..389d3ca 100644 --- a/src/js/controller/login/login-new-device.js +++ b/src/js/controller/login/login-new-device.js @@ -5,8 +5,19 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, aut $scope.incorrect = false; + var PRIV_KEY_PREFIX = '-----BEGIN PGP PRIVATE KEY BLOCK-----'; + var PUB_KEY_PREFIX = '-----BEGIN PGP PUBLIC KEY BLOCK-----'; + var PRIV_ERR_MSG = 'Cannot find private PGP key block!'; + $scope.pasteKey = function(pasted) { - var index = pasted.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----'); + var index = pasted.indexOf(PRIV_KEY_PREFIX); + if (index === -1) { + $scope.errMsg = PRIV_ERR_MSG; + return; + } + + $scope.errMsg = undefined; // reset error msg + $scope.key = { privateKeyArmored: pasted.substring(index, pasted.length).trim() }; @@ -14,7 +25,7 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, aut $scope.confirmPassphrase = function() { if ($scope.form.$invalid || !$scope.key) { - $scope.errMsg = 'Please fill out all required fields!'; + $scope.errMsg = PRIV_ERR_MSG; return; } @@ -36,11 +47,11 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, aut keypair = keys || {}; // extract public key from private key block if missing in key file - if (!$scope.key.publicKeyArmored || $scope.key.publicKeyArmored.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) { + if (!$scope.key.publicKeyArmored || $scope.key.publicKeyArmored.indexOf(PUB_KEY_PREFIX) < 0) { try { $scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored); } catch (e) { - throw new Error('Error reading PGP key!'); + throw new Error('Cannot find public PGP key!'); } } diff --git a/src/js/directive/login-new-device.js b/src/js/directive/login-new-device.js index 5d610ea..4b3b13f 100644 --- a/src/js/directive/login-new-device.js +++ b/src/js/directive/login-new-device.js @@ -18,7 +18,6 @@ ngModule.directive('fileReader', function() { keyParts; if (index === -1) { - scope.displayError(new Error('Error parsing private PGP key block!')); return; }