1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

WIP: start refactoring login controllers

This commit is contained in:
Tankred Hase 2014-12-16 16:14:16 +01:00
parent bfe827e084
commit ea74f3443c
6 changed files with 149 additions and 197 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, appConfig) { var CreateAccountCtrl = function($scope, $location, $routeParams, $q, auth, admin, appConfig) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app !$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.createWhiteoutAccount = function() { $scope.createWhiteoutAccount = function() {
@ -9,35 +9,37 @@ var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, a
return; return;
} }
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
var emailAddress = $scope.user + '@' + appConfig.config.wmailDomain; var emailAddress = $scope.user + '@' + appConfig.config.wmailDomain;
// set to state for next view return $q(function(resolve) {
auth.setCredentials({ $scope.busy = true;
emailAddress: emailAddress, $scope.errMsg = undefined; // reset error msg
password: $scope.pass, resolve();
realname: $scope.realname
});
// call REST api }).then(function() {
admin.createUser({ // set to state for next view
emailAddress: emailAddress, auth.setCredentials({
password: $scope.pass, emailAddress: emailAddress,
phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number password: $scope.pass,
betaCode: $scope.betaCode.toUpperCase() realname: $scope.realname
}, function(err) { });
// call REST api
return admin.createUser({
emailAddress: emailAddress,
password: $scope.pass,
phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number
betaCode: $scope.betaCode.toUpperCase()
});
}).then(function() {
$scope.busy = false; $scope.busy = false;
if (err) {
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
return;
}
// proceed to login and keygen // proceed to login and keygen
$location.path('/validate-phone'); $location.path('/validate-phone');
$scope.$apply();
}).catch(function(err) {
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
}); });
}; };
}; };

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, keychain) { var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, auth, keychain) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app !$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.confirmPassphrase = function() { $scope.confirmPassphrase = function() {
@ -9,50 +9,39 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, k
return; return;
} }
$scope.busy = true; return $q(function(resolve) {
$scope.errMsg = undefined; $scope.busy = true;
$scope.incorrect = false; $scope.errMsg = undefined;
$scope.incorrect = false;
resolve();
unlockCrypto(); }).then(function() {
}; // key keypair
var userId = auth.emailAddress;
return keychain.getUserKeyPair(userId);
function unlockCrypto() { }).then(function(keypair) {
var userId = auth.emailAddress; // unlock email service
keychain.getUserKeyPair(userId, function(err, keypair) { return email.unlock({
if (err) {
displayError(err);
return;
}
email.unlock({
keypair: keypair, keypair: keypair,
passphrase: $scope.passphrase passphrase: $scope.passphrase
}, onUnlock); });
});
}
function onUnlock(err) { }).then(function() {
if (err) { // persist credentials locally
displayError(err); return auth.storeCredentials();
return;
}
auth.storeCredentials(function(err) {
if (err) {
displayError(err);
return;
}
}).then(function() {
// go to main account screen
$location.path('/account'); $location.path('/account');
$scope.$apply();
}); }).catch(displayError);
} };
function displayError(err) { function displayError(err) {
$scope.busy = false; $scope.busy = false;
$scope.incorrect = true; $scope.incorrect = true;
$scope.errMsg = err.errMsg || err.message; $scope.errMsg = err.errMsg || err.message;
$scope.$apply();
} }
}; };

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, email, auth) { var LoginInitialCtrl = function($scope, $location, $routeParams, $q, newsletter, email, auth) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app !$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
var emailAddress = auth.emailAddress; var emailAddress = auth.emailAddress;
@ -51,24 +51,23 @@ var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, ema
// go to set keygen screen // go to set keygen screen
$scope.setState(states.PROCESSING); $scope.setState(states.PROCESSING);
email.unlock({ return $q(function(resolve) {
passphrase: undefined // generate key without passphrase resolve();
}, function(err) { }).then(function() {
if (err) { // generate key without passphrase
displayError(err); return email.unlock({
return; passphrase: undefined
}
auth.storeCredentials(function(err) {
if (err) {
displayError(err);
return;
}
$location.path('/account');
$scope.$apply();
}); });
});
}).then(function() {
// persist credentials locally
return auth.storeCredentials();
}).then(function() {
// go to main account screen
$location.path('/account');
}).catch(displayError);
}; };
$scope.setState = function(state) { $scope.setState = function(state) {
@ -78,7 +77,6 @@ var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, ema
function displayError(err) { function displayError(err) {
$scope.setState(states.IDLE); $scope.setState(states.IDLE);
$scope.errMsg = err.errMsg || err.message; $scope.errMsg = err.errMsg || err.message;
$scope.$apply();
} }
}; };

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, pgp, keychain) { var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, auth, pgp, keychain) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app !$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.incorrect = false; $scope.incorrect = false;
@ -11,31 +11,28 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
return; return;
} }
$scope.busy = true; var userId = auth.emailAddress,
$scope.errMsg = undefined; // reset error msg keypair;
$scope.incorrect = false;
unlockCrypto(); return $q(function(resolve) {
}; $scope.busy = true;
$scope.errMsg = undefined; // reset error msg
$scope.incorrect = false;
resolve();
function unlockCrypto() { }).then(function() {
var userId = auth.emailAddress; // check if user already has a public key on the key server
// check if user already has a public key on the key server return keychain.getUserKeyPair(userId);
keychain.getUserKeyPair(userId, function(err, keypair) {
if (err) {
$scope.displayError(err);
return;
}
keypair = keypair || {}; }).then(function(keys) {
keypair = keys || {};
// extract public key from private key block if missing in key file // 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('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) {
try { try {
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored); $scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
} catch (e) { } catch (e) {
$scope.displayError(new Error('Error reading PGP key!')); throw new Error('Error reading PGP key!');
return;
} }
} }
@ -45,8 +42,7 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored); privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
pubKeyParams = pgp.getKeyParams($scope.key.publicKeyArmored); pubKeyParams = pgp.getKeyParams($scope.key.publicKeyArmored);
} catch (e) { } catch (e) {
$scope.displayError(new Error('Error reading key params!')); throw new Error('Error reading key paramaters!');
return;
} }
// set parsed private key // set parsed private key
@ -68,43 +64,33 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
} }
// import and validate keypair // import and validate keypair
email.unlock({ return email.unlock({
keypair: keypair, keypair: keypair,
passphrase: $scope.passphrase passphrase: $scope.passphrase
}, function(err) { }).catch(function(err) {
if (err) { $scope.incorrect = true;
$scope.incorrect = true; throw err;
$scope.displayError(err);
return;
}
keychain.putUserKeyPair(keypair, onUnlock);
}); });
});
}
function onUnlock(err) { }).then(function() {
if (err) { // perist keys locally
$scope.displayError(err); return keychain.putUserKeyPair(keypair);
return;
}
auth.storeCredentials(function(err) { }).then(function() {
if (err) { // persist credentials locally
$scope.displayError(err); return auth.storeCredentials();
return;
}
}).then(function() {
// go to main account screen
$location.path('/account'); $location.path('/account');
$scope.$apply();
});
}
$scope.displayError = function(err) { }).catch(displayError);
};
function displayError(err) {
$scope.busy = false; $scope.busy = false;
$scope.errMsg = err.errMsg || err.message; $scope.errMsg = err.errMsg || err.message;
$scope.$apply(); }
};
}; };
module.exports = LoginExistingCtrl; module.exports = LoginExistingCtrl;

View File

@ -4,88 +4,63 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
// check for app update // check for app update
updateHandler.checkForUpdate(); updateHandler.checkForUpdate();
// initialize the user account // initialize the user account
initializeUser(); auth.init().then(function() {
// get email address
return auth.getEmailAddress();
function initializeUser() { }).then(function(info) {
// init the auth modules // check if account needs to be selected
auth.init(function(err) { if (!info.emailAddress) {
if (err) { return $scope.goTo('/add-account');
return dialog.error(err); }
}
// get OAuth token from chrome // initiate the account by initializing the email dao and user storage
auth.getEmailAddress(function(err, info) { return account.init({
if (err) { emailAddress: info.emailAddress,
dialog.error(err); realname: info.realname
return; }).then(function(availableKeys) {
} return redirect(availableKeys);
// check if account needs to be selected
if (!info.emailAddress) {
$scope.goTo('/add-account');
return;
}
// initiate the account by initializing the email dao and user storage
account.init({
emailAddress: info.emailAddress,
realname: info.realname
}, function(err, availableKeys) {
if (err) {
dialog.error(err);
return;
}
redirect(availableKeys);
});
});
}); });
}
}).catch(dialog.error);
function redirect(availableKeys) { function redirect(availableKeys) {
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) { if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
// public and private key available, try empty passphrase // public and private key available, try empty passphrase
email.unlock({ return email.unlock({
keypair: availableKeys, keypair: availableKeys,
passphrase: undefined passphrase: undefined
}, function(err) { }).then(function() {
if (err) { // no passphrase set... go to main screen
$scope.goTo('/login-existing'); return auth.storeCredentials().then(function() {
return; return $scope.goTo('/account');
}
auth.storeCredentials(function(err) {
if (err) {
return dialog.error(err);
}
$scope.goTo('/account');
}); });
}).catch(function() {
// passphrase set... ask for passphrase
return $scope.goTo('/login-existing');
}); });
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) { } else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
// check if private key is synced // check if private key is synced
keychain.requestPrivateKeyDownload({ return keychain.requestPrivateKeyDownload({
userId: availableKeys.publicKey.userId, userId: availableKeys.publicKey.userId,
keyId: availableKeys.publicKey._id, keyId: availableKeys.publicKey._id,
}, function(err, privateKeySynced) { }).then(function(privateKeySynced) {
if (err) {
dialog.error(err);
return;
}
if (privateKeySynced) { if (privateKeySynced) {
// private key is synced, proceed to download // private key is synced, proceed to download
$scope.goTo('/login-privatekey-download'); return $scope.goTo('/login-privatekey-download');
return; } else {
// no private key, import key file
return $scope.goTo('/login-new-device');
} }
// no private key, import key file
$scope.goTo('/login-new-device');
}); });
} else { } else {
// no public key available, start onboarding process // no public key available, start onboarding process
$scope.goTo('/login-initial'); return $scope.goTo('/login-initial');
} }
} }

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, auth, admin) { var ValidatePhoneCtrl = function($scope, $location, $routeParams, $q, mailConfig, auth, admin) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app !$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.validateUser = function() { $scope.validateUser = function() {
@ -9,23 +9,25 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, au
return; return;
} }
$scope.busy = true; return $q(function(resolve) {
$scope.errMsg = undefined; // reset error msg $scope.busy = true;
$scope.errMsg = undefined; // reset error msg
resolve();
// verify user to REST api }).then(function() {
admin.validateUser({ // verify user to REST api
emailAddress: auth.emailAddress, return admin.validateUser({
token: $scope.token.toUpperCase() emailAddress: auth.emailAddress,
}, function(err) { token: $scope.token.toUpperCase()
if (err) { });
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
return;
}
}).then(function() {
// proceed to login // proceed to login
$scope.login(); $scope.login();
}).catch(function(err) {
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
}); });
}; };