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,10 +9,14 @@ 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;
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
resolve();
}).then(function() {
// set to state for next view // set to state for next view
auth.setCredentials({ auth.setCredentials({
emailAddress: emailAddress, emailAddress: emailAddress,
@ -21,23 +25,21 @@ var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, a
}); });
// call REST api // call REST api
admin.createUser({ return admin.createUser({
emailAddress: emailAddress, emailAddress: emailAddress,
password: $scope.pass, password: $scope.pass,
phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number
betaCode: $scope.betaCode.toUpperCase() betaCode: $scope.betaCode.toUpperCase()
}, function(err) { });
}).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;
} }
return $q(function(resolve) {
$scope.busy = true; $scope.busy = true;
$scope.errMsg = undefined; $scope.errMsg = undefined;
$scope.incorrect = false; $scope.incorrect = false;
resolve();
unlockCrypto(); }).then(function() {
}; // key keypair
function unlockCrypto() {
var userId = auth.emailAddress; var userId = auth.emailAddress;
keychain.getUserKeyPair(userId, function(err, keypair) { return keychain.getUserKeyPair(userId);
if (err) {
displayError(err);
return;
}
email.unlock({ }).then(function(keypair) {
// unlock email service
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) { }).then(function() {
if (err) { // persist credentials locally
displayError(err); return auth.storeCredentials();
return;
}
}).then(function() {
// go to main account screen
$location.path('/account'); $location.path('/account');
$scope.$apply();
}); }).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;
} }
var userId = auth.emailAddress,
keypair;
return $q(function(resolve) {
$scope.busy = true; $scope.busy = true;
$scope.errMsg = undefined; // reset error msg $scope.errMsg = undefined; // reset error msg
$scope.incorrect = false; $scope.incorrect = false;
resolve();
unlockCrypto(); }).then(function() {
};
function unlockCrypto() {
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
keychain.getUserKeyPair(userId, function(err, keypair) { return keychain.getUserKeyPair(userId);
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;
$scope.displayError(err); throw 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
function initializeUser() { return auth.getEmailAddress();
// init the auth modules
auth.init(function(err) {
if (err) {
return dialog.error(err);
}
// get OAuth token from chrome
auth.getEmailAddress(function(err, info) {
if (err) {
dialog.error(err);
return;
}
}).then(function(info) {
// check if account needs to be selected // check if account needs to be selected
if (!info.emailAddress) { if (!info.emailAddress) {
$scope.goTo('/add-account'); return $scope.goTo('/add-account');
return;
} }
// initiate the account by initializing the email dao and user storage // initiate the account by initializing the email dao and user storage
account.init({ return account.init({
emailAddress: info.emailAddress, emailAddress: info.emailAddress,
realname: info.realname realname: info.realname
}, function(err, availableKeys) { }).then(function(availableKeys) {
if (err) { return redirect(availableKeys);
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 // no private key, import key file
$scope.goTo('/login-new-device'); return $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;
} }
return $q(function(resolve) {
$scope.busy = true; $scope.busy = true;
$scope.errMsg = undefined; // reset error msg $scope.errMsg = undefined; // reset error msg
resolve();
}).then(function() {
// verify user to REST api // verify user to REST api
admin.validateUser({ return admin.validateUser({
emailAddress: auth.emailAddress, emailAddress: auth.emailAddress,
token: $scope.token.toUpperCase() token: $scope.token.toUpperCase()
}, function(err) { });
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;
}); });
}; };