1
0
mirror of https://github.com/moparisthebest/mail synced 2024-08-13 16:43:47 -04:00
mail/src/js/controller/login-new-device.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-10-22 08:48:38 -04:00
define(function(require) {
2013-10-21 07:10:42 -04:00
'use strict';
2013-10-22 08:48:38 -04:00
var angular = require('angular'),
appController = require('js/app-controller');
var LoginExistingCtrl = function($scope, $location) {
2013-11-04 08:20:14 -05:00
var emailDao = appController._emailDao;
2013-10-22 11:32:30 -04:00
$scope.incorrect = false;
2013-10-21 07:10:42 -04:00
$scope.confirmPassphrase = function() {
2013-11-04 08:20:14 -05:00
if (!$scope.passphrase) {
2013-10-22 11:32:30 -04:00
$scope.incorrect = true;
2013-10-22 08:48:38 -04:00
return;
}
2013-10-22 11:32:30 -04:00
$scope.incorrect = false;
2013-11-04 08:20:14 -05:00
unlockCrypto();
};
2013-10-22 08:48:38 -04:00
2013-11-04 08:20:14 -05:00
function unlockCrypto() {
var userId = emailDao._account.emailAddress;
emailDao._keychain.getUserKeyPair(userId, function(err, keypair) {
2013-10-22 08:48:38 -04:00
if (err) {
console.error(err);
return;
}
2013-11-04 08:20:14 -05:00
keypair.privateKey = {
_id: keypair.publicKey._id,
userId: userId,
encryptedKey: $scope.key.privateKeyArmored
};
emailDao.unlock(keypair, $scope.passphrase, function(err) {
2013-10-22 08:48:38 -04:00
if (err) {
2013-11-04 08:20:14 -05:00
$scope.incorrect = true;
$scope.$apply();
2013-10-22 08:48:38 -04:00
console.error(err);
return;
}
2013-11-04 08:20:14 -05:00
emailDao._keychain.putUserKeyPair(keypair, onUnlock);
2013-10-22 08:48:38 -04:00
});
2013-11-04 08:20:14 -05:00
});
}
function onUnlock(err) {
if (err) {
console.error(err);
return;
2013-10-22 08:48:38 -04:00
}
2013-10-21 07:10:42 -04:00
2013-10-22 08:48:38 -04:00
$location.path('/desktop');
$scope.$apply();
}
2013-10-21 07:10:42 -04:00
};
2013-10-22 08:48:38 -04:00
var ngModule = angular.module('login-new-device', []);
ngModule.directive('fileReader', function() {
return function(scope, elm) {
elm.bind('change', function(e) {
var files = e.target.files,
reader = new FileReader();
if (files.length === 0) {
return;
}
2013-10-22 11:32:30 -04:00
reader.onload = function(e) {
var rawKeys = e.target.result,
index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----');
if (index === -1) {
console.error('Erroneous key file format!');
return;
}
2013-10-22 11:32:30 -04:00
scope.key = {
publicKeyArmored: rawKeys.substring(0, index),
privateKeyArmored: rawKeys.substring(index, rawKeys.length)
2013-10-22 08:48:38 -04:00
};
2013-10-22 11:32:30 -04:00
scope.$apply();
};
2013-10-22 08:48:38 -04:00
reader.readAsText(files[0]);
});
};
});
2013-10-21 07:10:42 -04:00
return LoginExistingCtrl;
});