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';
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
$scope.createWhiteoutAccount = function() {
@ -9,35 +9,37 @@ var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, a
return;
}
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
var emailAddress = $scope.user + '@' + appConfig.config.wmailDomain;
// set to state for next view
auth.setCredentials({
emailAddress: emailAddress,
password: $scope.pass,
realname: $scope.realname
});
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
resolve();
// call REST api
admin.createUser({
emailAddress: emailAddress,
password: $scope.pass,
phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number
betaCode: $scope.betaCode.toUpperCase()
}, function(err) {
}).then(function() {
// set to state for next view
auth.setCredentials({
emailAddress: emailAddress,
password: $scope.pass,
realname: $scope.realname
});
// 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;
if (err) {
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
return;
}
// proceed to login and keygen
$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';
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
$scope.confirmPassphrase = function() {
@ -9,50 +9,39 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, k
return;
}
$scope.busy = true;
$scope.errMsg = undefined;
$scope.incorrect = false;
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined;
$scope.incorrect = false;
resolve();
unlockCrypto();
};
}).then(function() {
// key keypair
var userId = auth.emailAddress;
return keychain.getUserKeyPair(userId);
function unlockCrypto() {
var userId = auth.emailAddress;
keychain.getUserKeyPair(userId, function(err, keypair) {
if (err) {
displayError(err);
return;
}
email.unlock({
}).then(function(keypair) {
// unlock email service
return email.unlock({
keypair: keypair,
passphrase: $scope.passphrase
}, onUnlock);
});
}
});
function onUnlock(err) {
if (err) {
displayError(err);
return;
}
auth.storeCredentials(function(err) {
if (err) {
displayError(err);
return;
}
}).then(function() {
// persist credentials locally
return auth.storeCredentials();
}).then(function() {
// go to main account screen
$location.path('/account');
$scope.$apply();
});
}
}).catch(displayError);
};
function displayError(err) {
$scope.busy = false;
$scope.incorrect = true;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
}
};

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
'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
$scope.validateUser = function() {
@ -9,23 +9,25 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, au
return;
}
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
resolve();
// verify user to REST api
admin.validateUser({
emailAddress: auth.emailAddress,
token: $scope.token.toUpperCase()
}, function(err) {
if (err) {
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
return;
}
}).then(function() {
// verify user to REST api
return admin.validateUser({
emailAddress: auth.emailAddress,
token: $scope.token.toUpperCase()
});
}).then(function() {
// proceed to login
$scope.login();
}).catch(function(err) {
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
});
};
@ -60,4 +62,4 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, au
};
};
module.exports = ValidatePhoneCtrl;
module.exports = ValidatePhoneCtrl;