Refactor auth manager and async storage manager
This commit is contained in:
@@ -34,7 +34,7 @@ angular.module('app')
|
|||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
function load() {
|
function async load() {
|
||||||
// pass keys to storageManager to decrypt storage
|
// pass keys to storageManager to decrypt storage
|
||||||
// Update: Wait, why? passcodeManager already handles this.
|
// Update: Wait, why? passcodeManager already handles this.
|
||||||
// storageManager.setKeys(passcodeManager.keys());
|
// storageManager.setKeys(passcodeManager.keys());
|
||||||
@@ -73,10 +73,10 @@ angular.module('app')
|
|||||||
function initiateSync() {
|
function initiateSync() {
|
||||||
authManager.loadInitialData();
|
authManager.loadInitialData();
|
||||||
|
|
||||||
syncManager.setKeyRequestHandler(() => {
|
syncManager.setKeyRequestHandler(async () => {
|
||||||
let offline = authManager.offline();
|
let offline = authManager.offline();
|
||||||
let version = offline ? passcodeManager.protocolVersion() : authManager.protocolVersion();
|
let version = offline ? passcodeManager.protocolVersion() : await authManager.protocolVersion();
|
||||||
let keys = offline ? passcodeManager.keys() : authManager.keys();
|
let keys = offline ? passcodeManager.keys() : await authManager.keys();
|
||||||
return {
|
return {
|
||||||
keys: keys,
|
keys: keys,
|
||||||
offline: offline,
|
offline: offline,
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ class PasswordWizard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.validateCurrentPassword = function(callback) {
|
$scope.validateCurrentPassword = async function(callback) {
|
||||||
let currentPassword = $scope.formData.currentPassword;
|
let currentPassword = $scope.formData.currentPassword;
|
||||||
let newPass = $scope.securityUpdate ? currentPassword : $scope.formData.newPassword;
|
let newPass = $scope.securityUpdate ? currentPassword : $scope.formData.newPassword;
|
||||||
|
|
||||||
@@ -175,8 +175,8 @@ class PasswordWizard {
|
|||||||
// Ensure value for current password matches what's saved
|
// Ensure value for current password matches what's saved
|
||||||
let authParams = authManager.getAuthParams();
|
let authParams = authManager.getAuthParams();
|
||||||
let password = $scope.formData.currentPassword;
|
let password = $scope.formData.currentPassword;
|
||||||
SFJS.crypto.computeEncryptionKeysForUser(password, authParams).then((keys) => {
|
SFJS.crypto.computeEncryptionKeysForUser(password, authParams).then(async (keys) => {
|
||||||
let success = keys.mk === authManager.keys().mk;
|
let success = keys.mk === (await authManager.keys()).mk;
|
||||||
if(success) {
|
if(success) {
|
||||||
this.currentServerPw = keys.pw;
|
this.currentServerPw = keys.pw;
|
||||||
} else {
|
} else {
|
||||||
@@ -209,7 +209,7 @@ class PasswordWizard {
|
|||||||
|
|
||||||
// perform a sync beforehand to pull in any last minutes changes before we change the encryption key (and thus cant decrypt new changes)
|
// perform a sync beforehand to pull in any last minutes changes before we change the encryption key (and thus cant decrypt new changes)
|
||||||
syncManager.sync((response) => {
|
syncManager.sync((response) => {
|
||||||
authManager.changePassword(currentServerPw, newKeys, newAuthParams, (response) => {
|
authManager.changePassword(authManager.user.email, currentServerPw, newKeys, newAuthParams, (response) => {
|
||||||
if(response.error) {
|
if(response.error) {
|
||||||
alert(response.error.message ? response.error.message : "There was an error changing your password. Please try again.");
|
alert(response.error.message ? response.error.message : "There was an error changing your password. Please try again.");
|
||||||
$timeout(() => callback(false));
|
$timeout(() => callback(false));
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ class ActionsManager {
|
|||||||
case "get": {
|
case "get": {
|
||||||
this.httpManager.getAbsolute(action.url, {}, (response) => {
|
this.httpManager.getAbsolute(action.url, {}, (response) => {
|
||||||
action.error = false;
|
action.error = false;
|
||||||
handleResponseDecryption(response, this.authManager.keys(), true);
|
handleResponseDecryption(response, await this.authManager.keys(), true);
|
||||||
}, (response) => {
|
}, (response) => {
|
||||||
action.error = true;
|
action.error = true;
|
||||||
customCallback(null);
|
customCallback(null);
|
||||||
@@ -136,7 +136,7 @@ class ActionsManager {
|
|||||||
|
|
||||||
this.httpManager.getAbsolute(action.url, {}, (response) => {
|
this.httpManager.getAbsolute(action.url, {}, (response) => {
|
||||||
action.error = false;
|
action.error = false;
|
||||||
handleResponseDecryption(response, this.authManager.keys(), false);
|
handleResponseDecryption(response, await this.authManager.keys(), false);
|
||||||
}, (response) => {
|
}, (response) => {
|
||||||
action.error = true;
|
action.error = true;
|
||||||
customCallback(null);
|
customCallback(null);
|
||||||
@@ -175,11 +175,11 @@ class ActionsManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async outgoingParamsForItem(item, extension, decrypted = false) {
|
async outgoingParamsForItem(item, extension, decrypted = false) {
|
||||||
var keys = this.authManager.keys();
|
var keys = await this.authManager.keys();
|
||||||
if(decrypted) {
|
if(decrypted) {
|
||||||
keys = null;
|
keys = null;
|
||||||
}
|
}
|
||||||
var itemParams = new SFItemParams(item, keys, this.authManager.protocolVersion());
|
var itemParams = new SFItemParams(item, keys, await this.authManager.protocolVersion());
|
||||||
return itemParams.paramsForExtension();
|
return itemParams.paramsForExtension();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class ArchiveManager {
|
|||||||
Public
|
Public
|
||||||
*/
|
*/
|
||||||
|
|
||||||
downloadBackup(encrypted) {
|
async downloadBackup(encrypted) {
|
||||||
// download in Standard File format
|
// download in Standard File format
|
||||||
var keys, authParams, protocolVersion;
|
var keys, authParams, protocolVersion;
|
||||||
if(encrypted) {
|
if(encrypted) {
|
||||||
@@ -19,9 +19,9 @@ class ArchiveManager {
|
|||||||
authParams = this.passcodeManager.passcodeAuthParams();
|
authParams = this.passcodeManager.passcodeAuthParams();
|
||||||
protocolVersion = authParams.version;
|
protocolVersion = authParams.version;
|
||||||
} else {
|
} else {
|
||||||
keys = this.authManager.keys();
|
keys = await this.authManager.keys();
|
||||||
authParams = this.authManager.getAuthParams();
|
authParams = this.authManager.getAuthParams();
|
||||||
protocolVersion = this.authManager.protocolVersion();
|
protocolVersion = await this.authManager.protocolVersion();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.__itemsData(keys, authParams, protocolVersion).then((data) => {
|
this.__itemsData(keys, authParams, protocolVersion).then((data) => {
|
||||||
|
|||||||
@@ -1,330 +1,203 @@
|
|||||||
angular.module('app')
|
class AuthManager extends SFAuthManager {
|
||||||
.provider('authManager', function () {
|
|
||||||
|
|
||||||
function domainName() {
|
constructor(modelManager, singletonManager, storageManager, dbManager, httpManager, $rootScope, $timeout, $compile) {
|
||||||
var domain_comps = location.hostname.split(".");
|
super(storageManager, httpManager, $timeout);
|
||||||
var domain = domain_comps[domain_comps.length - 2] + "." + domain_comps[domain_comps.length - 1];
|
this.$rootScope = $rootScope;
|
||||||
return domain;
|
this.$compile = $compile;
|
||||||
|
this.modelManager = modelManager;
|
||||||
|
this.singletonManager = singletonManager;
|
||||||
|
this.storageManager = storageManager;
|
||||||
|
this.dbManager = dbManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
loadInitialData() {
|
||||||
|
var userData = this.storageManager.getItemSync("user");
|
||||||
|
if(userData) {
|
||||||
|
this.user = JSON.parse(userData);
|
||||||
|
} else {
|
||||||
|
// legacy, check for uuid
|
||||||
|
var idData = this.storageManager.getItemSync("uuid");
|
||||||
|
if(idData) {
|
||||||
|
this.user = {uuid: idData};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$get = function($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile) {
|
this.configureUserPrefs();
|
||||||
return new AuthManager($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile);
|
this.checkForSecurityUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
offline() {
|
||||||
|
return !this.user;
|
||||||
|
}
|
||||||
|
|
||||||
|
isEphemeralSession() {
|
||||||
|
if(this.ephemeral == null || this.ephemeral == undefined) {
|
||||||
|
this.ephemeral = JSON.parse(this.storageManager.getItemSync("ephemeral", StorageManager.Fixed));
|
||||||
|
}
|
||||||
|
return this.ephemeral;
|
||||||
|
}
|
||||||
|
|
||||||
|
setEphemeral(ephemeral) {
|
||||||
|
this.ephemeral = ephemeral;
|
||||||
|
if(ephemeral) {
|
||||||
|
this.storageManager.setModelStorageMode(StorageManager.Ephemeral);
|
||||||
|
this.storageManager.setItemsMode(StorageManager.Ephemeral);
|
||||||
|
} else {
|
||||||
|
this.storageManager.setModelStorageMode(StorageManager.Fixed);
|
||||||
|
this.storageManager.setItemsMode(this.storageManager.hasPasscode() ? StorageManager.FixedEncrypted : StorageManager.Fixed);
|
||||||
|
this.storageManager.setItem("ephemeral", JSON.stringify(false), StorageManager.Fixed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getAuthParams() {
|
||||||
|
if(!this._authParams) {
|
||||||
|
this._authParams = JSON.parse(this.storageManager.getItemSync("auth_params"));
|
||||||
|
}
|
||||||
|
return this._authParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
keys() {
|
||||||
|
if(!this._keys) {
|
||||||
|
var mk = this.storageManager.getItemSync("mk");
|
||||||
|
if(!mk) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
this._keys = {mk: mk, ak: this.storageManager.getItemSync("ak")};
|
||||||
|
}
|
||||||
|
return this._keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
async protocolVersion() {
|
||||||
|
var authParams = this.getAuthParams();
|
||||||
|
if(authParams && authParams.version) {
|
||||||
|
return authParams.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
function AuthManager($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile) {
|
var keys = await this.keys();
|
||||||
|
if(keys && keys.ak) {
|
||||||
|
// If there's no version stored, and there's an ak, it has to be 002. Newer versions would have thier version stored in authParams.
|
||||||
|
return "002";
|
||||||
|
} else {
|
||||||
|
return "001";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.loadInitialData = function() {
|
getAuthParamsForEmail(url, email, extraParams, callback) {
|
||||||
var userData = storageManager.getItem("user");
|
super.getAuthParamsForEmail(url, email, extraParams, (response) => {
|
||||||
if(userData) {
|
callback(response);
|
||||||
this.user = JSON.parse(userData);
|
})
|
||||||
} else {
|
}
|
||||||
// legacy, check for uuid
|
|
||||||
var idData = storageManager.getItem("uuid");
|
|
||||||
if(idData) {
|
|
||||||
this.user = {uuid: idData};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
login(url, email, password, ephemeral, strictSignin, extraParams, callback) {
|
||||||
|
super.login(url, email, password, ephemeral, strictSignin, extraParams, (response) => {
|
||||||
|
if(!response.error) {
|
||||||
|
this.setEphemeral(ephemeral);
|
||||||
|
this.handleAuthResponse(response, email, url, authParams, keys);
|
||||||
this.checkForSecurityUpdate();
|
this.checkForSecurityUpdate();
|
||||||
}
|
}
|
||||||
|
this.$timeout(() => callback(response));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
this.offline = function() {
|
handleAuthResponse(response, email, url, authParams, keys) {
|
||||||
return !this.user;
|
try {
|
||||||
}
|
super.handleAuthResponse(response, email, url, authParams, keys);
|
||||||
|
this._authParams = authParams;
|
||||||
this.isEphemeralSession = function() {
|
this.user = response.user;
|
||||||
if(this.ephemeral == null || this.ephemeral == undefined) {
|
this.storageManager.setItem("user", JSON.stringify(response.user));
|
||||||
this.ephemeral = JSON.parse(storageManager.getItem("ephemeral", StorageManager.Fixed));
|
} catch (e) {
|
||||||
}
|
this.dbManager.displayOfflineAlert();
|
||||||
return this.ephemeral;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setEphemeral = function(ephemeral) {
|
|
||||||
this.ephemeral = ephemeral;
|
|
||||||
if(ephemeral) {
|
|
||||||
storageManager.setModelStorageMode(StorageManager.Ephemeral);
|
|
||||||
storageManager.setItemsMode(StorageManager.Ephemeral);
|
|
||||||
} else {
|
|
||||||
storageManager.setModelStorageMode(StorageManager.Fixed);
|
|
||||||
storageManager.setItemsMode(storageManager.hasPasscode() ? StorageManager.FixedEncrypted : StorageManager.Fixed);
|
|
||||||
storageManager.setItem("ephemeral", JSON.stringify(false), StorageManager.Fixed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getAuthParams = function() {
|
|
||||||
if(!this._authParams) {
|
|
||||||
this._authParams = JSON.parse(storageManager.getItem("auth_params"));
|
|
||||||
}
|
|
||||||
return this._authParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.keys = function() {
|
|
||||||
if(!this._keys) {
|
|
||||||
var mk = storageManager.getItem("mk");
|
|
||||||
if(!mk) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
this._keys = {mk: mk, ak: storageManager.getItem("ak")};
|
|
||||||
}
|
|
||||||
return this._keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.protocolVersion = function() {
|
|
||||||
var authParams = this.getAuthParams();
|
|
||||||
if(authParams && authParams.version) {
|
|
||||||
return authParams.version;
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = this.keys();
|
|
||||||
if(keys && keys.ak) {
|
|
||||||
// If there's no version stored, and there's an ak, it has to be 002. Newer versions would have thier version stored in authParams.
|
|
||||||
return "002";
|
|
||||||
} else {
|
|
||||||
return "001";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isProtocolVersionSupported = function(version) {
|
|
||||||
return SFJS.supportedVersions().includes(version);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getAuthParamsForEmail = function(url, email, extraParams, callback) {
|
|
||||||
var requestUrl = url + "/auth/params";
|
|
||||||
httpManager.getAbsolute(requestUrl, _.merge({email: email}, extraParams), function(response){
|
|
||||||
callback(response);
|
|
||||||
}, function(response){
|
|
||||||
console.error("Error getting auth params", response);
|
|
||||||
if(typeof response !== 'object') {
|
|
||||||
response = {error: {message: "A server error occurred while trying to sign in. Please try again."}};
|
|
||||||
}
|
|
||||||
callback(response);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.login = function(url, email, password, ephemeral, strictSignin, extraParams, callback) {
|
|
||||||
this.getAuthParamsForEmail(url, email, extraParams, (authParams) => {
|
|
||||||
|
|
||||||
// SF3 requires a unique identifier in the auth params
|
|
||||||
authParams.identifier = email;
|
|
||||||
|
|
||||||
if(authParams.error) {
|
|
||||||
callback(authParams);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!authParams || !authParams.pw_cost) {
|
|
||||||
callback({error : {message: "Invalid email or password."}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!this.isProtocolVersionSupported(authParams.version)) {
|
|
||||||
var message;
|
|
||||||
if(SFJS.isVersionNewerThanLibraryVersion(authParams.version)) {
|
|
||||||
// The user has a new account type, but is signing in to an older client.
|
|
||||||
message = "This version of the application does not support your newer account type. Please upgrade to the latest version of Standard Notes to sign in.";
|
|
||||||
} else {
|
|
||||||
// The user has a very old account type, which is no longer supported by this client
|
|
||||||
message = "The protocol version associated with your account is outdated and no longer supported by this application. Please visit standardnotes.org/help/security for more information.";
|
|
||||||
}
|
|
||||||
callback({error: {message: message}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(SFJS.isProtocolVersionOutdated(authParams.version)) {
|
|
||||||
let message = `The encryption version for your account, ${authParams.version}, is outdated and requires upgrade. You may proceed with login, but are advised to follow prompts for Security Updates once inside. Please visit standardnotes.org/help/security for more information.\n\nClick 'OK' to proceed with login.`
|
|
||||||
if(!confirm(message)) {
|
|
||||||
callback({error: {}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!SFJS.supportsPasswordDerivationCost(authParams.pw_cost)) {
|
|
||||||
let message = "Your account was created on a platform with higher security capabilities than this browser supports. " +
|
|
||||||
"If we attempted to generate your login keys here, it would take hours. " +
|
|
||||||
"Please use a browser with more up to date security capabilities, like Google Chrome or Firefox, to log in."
|
|
||||||
callback({error: {message: message}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var minimum = SFJS.costMinimumForVersion(authParams.version);
|
|
||||||
if(authParams.pw_cost < minimum) {
|
|
||||||
let message = "Unable to login due to insecure password parameters. Please visit standardnotes.org/help/security for more information.";
|
|
||||||
callback({error: {message: message}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(strictSignin) {
|
|
||||||
// Refuse sign in if authParams.version is anything but the latest version
|
|
||||||
var latestVersion = SFJS.version();
|
|
||||||
if(authParams.version !== latestVersion) {
|
|
||||||
let message = `Strict sign in refused server sign in parameters. The latest security version is ${latestVersion}, but your account is reported to have version ${authParams.version}. If you'd like to proceed with sign in anyway, please disable strict sign in and try again.`;
|
|
||||||
callback({error: {message: message}});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SFJS.crypto.computeEncryptionKeysForUser(password, authParams).then((keys) => {
|
|
||||||
var requestUrl = url + "/auth/sign_in";
|
|
||||||
var params = _.merge({password: keys.pw, email: email}, extraParams);
|
|
||||||
|
|
||||||
httpManager.postAbsolute(requestUrl, params, (response) => {
|
|
||||||
this.setEphemeral(ephemeral);
|
|
||||||
this.handleAuthResponse(response, email, url, authParams, keys);
|
|
||||||
this.checkForSecurityUpdate();
|
|
||||||
$timeout(() => callback(response));
|
|
||||||
}, (response) => {
|
|
||||||
console.error("Error logging in", response);
|
|
||||||
if(typeof response !== 'object') {
|
|
||||||
response = {error: {message: "A server error occurred while trying to sign in. Please try again."}};
|
|
||||||
}
|
|
||||||
$timeout(() => callback(response));
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.handleAuthResponse = function(response, email, url, authParams, keys) {
|
|
||||||
try {
|
|
||||||
if(url) {
|
|
||||||
storageManager.setItem("server", url);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.user = response.user;
|
|
||||||
storageManager.setItem("user", JSON.stringify(response.user));
|
|
||||||
|
|
||||||
this._authParams = authParams;
|
|
||||||
storageManager.setItem("auth_params", JSON.stringify(authParams));
|
|
||||||
|
|
||||||
storageManager.setItem("jwt", response.token);
|
|
||||||
this.saveKeys(keys);
|
|
||||||
} catch(e) {
|
|
||||||
dbManager.displayOfflineAlert();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.saveKeys = function(keys) {
|
|
||||||
this._keys = keys;
|
|
||||||
// pw doesn't need to be saved.
|
|
||||||
// storageManager.setItem("pw", keys.pw);
|
|
||||||
storageManager.setItem("mk", keys.mk);
|
|
||||||
storageManager.setItem("ak", keys.ak);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.register = function(url, email, password, ephemeral, callback) {
|
|
||||||
SFJS.crypto.generateInitialKeysAndAuthParamsForUser(email, password).then((results) => {
|
|
||||||
let keys = results.keys;
|
|
||||||
let authParams = results.authParams;
|
|
||||||
|
|
||||||
var requestUrl = url + "/auth";
|
|
||||||
var params = _.merge({password: keys.pw, email: email}, authParams);
|
|
||||||
|
|
||||||
httpManager.postAbsolute(requestUrl, params, (response) => {
|
|
||||||
this.setEphemeral(ephemeral);
|
|
||||||
this.handleAuthResponse(response, email, url, authParams, keys);
|
|
||||||
callback(response);
|
|
||||||
}, (response) => {
|
|
||||||
console.error("Registration error", response);
|
|
||||||
if(typeof response !== 'object') {
|
|
||||||
response = {error: {message: "A server error occurred while trying to register. Please try again."}};
|
|
||||||
}
|
|
||||||
callback(response);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.changePassword = function(current_server_pw, newKeys, newAuthParams, callback) {
|
|
||||||
let email = this.user.email;
|
|
||||||
let newServerPw = newKeys.pw;
|
|
||||||
|
|
||||||
var requestUrl = storageManager.getItem("server") + "/auth/change_pw";
|
|
||||||
var params = _.merge({new_password: newServerPw, current_password: current_server_pw}, newAuthParams);
|
|
||||||
|
|
||||||
httpManager.postAbsolute(requestUrl, params, (response) => {
|
|
||||||
this.handleAuthResponse(response, email, null, newAuthParams, newKeys);
|
|
||||||
callback(response);
|
|
||||||
|
|
||||||
// Allows security update status to be changed if neccessary
|
|
||||||
this.checkForSecurityUpdate();
|
|
||||||
}, (response) => {
|
|
||||||
if(typeof response !== 'object') {
|
|
||||||
response = {error: {message: "Something went wrong while changing your password. Your password was not changed. Please try again."}}
|
|
||||||
}
|
|
||||||
callback(response);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.checkForSecurityUpdate = function() {
|
|
||||||
if(this.offline()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let latest = SFJS.version();
|
|
||||||
let updateAvailable = this.protocolVersion() !== latest;
|
|
||||||
if(updateAvailable !== this.securityUpdateAvailable) {
|
|
||||||
this.securityUpdateAvailable = updateAvailable;
|
|
||||||
$rootScope.$broadcast("security-update-status-changed");
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.securityUpdateAvailable;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.presentPasswordWizard = function(type) {
|
|
||||||
var scope = $rootScope.$new(true);
|
|
||||||
scope.type = type;
|
|
||||||
var el = $compile( "<password-wizard type='type'></password-wizard>" )(scope);
|
|
||||||
angular.element(document.body).append(el);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.staticifyObject = function(object) {
|
|
||||||
return JSON.parse(JSON.stringify(object));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.signOut = function() {
|
|
||||||
this._keys = null;
|
|
||||||
this.user = null;
|
|
||||||
this._authParams = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* User Preferences */
|
|
||||||
|
|
||||||
let prefsContentType = "SN|UserPreferences";
|
|
||||||
|
|
||||||
singletonManager.registerSingleton({content_type: prefsContentType}, (resolvedSingleton) => {
|
|
||||||
this.userPreferences = resolvedSingleton;
|
|
||||||
this.userPreferencesDidChange();
|
|
||||||
}, (valueCallback) => {
|
|
||||||
// Safe to create. Create and return object.
|
|
||||||
var prefs = new SFItem({content_type: prefsContentType});
|
|
||||||
modelManager.addItem(prefs);
|
|
||||||
prefs.setDirty(true);
|
|
||||||
$rootScope.sync("authManager singletonCreate");
|
|
||||||
valueCallback(prefs);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.userPreferencesDidChange = function() {
|
|
||||||
$rootScope.$broadcast("user-preferences-changed");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.syncUserPreferences = function() {
|
|
||||||
this.userPreferences.setDirty(true);
|
|
||||||
$rootScope.sync("syncUserPreferences");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getUserPrefValue = function(key, defaultValue) {
|
|
||||||
if(!this.userPreferences) { return defaultValue; }
|
|
||||||
var value = this.userPreferences.getAppDataItem(key);
|
|
||||||
return (value !== undefined && value != null) ? value : defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setUserPrefValue = function(key, value, sync) {
|
|
||||||
if(!this.userPreferences) { console.log("Prefs are null, not setting value", key); return; }
|
|
||||||
this.userPreferences.setAppDataItem(key, value);
|
|
||||||
if(sync) {
|
|
||||||
this.syncUserPreferences();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
register(url, email, password, ephemeral, callback) {
|
||||||
|
super.register(url, email, password, ephemeral, (response) => {
|
||||||
|
if(!response.error) {
|
||||||
|
this.setEphemeral(ephemeral);
|
||||||
|
}
|
||||||
|
callback(response);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
changePassword(email, current_server_pw, newKeys, newAuthParams, callback) {
|
||||||
|
super.changePassword(email, current_server_pw, newKeys, newAuthParams, (response) => {
|
||||||
|
if(!response.error) {
|
||||||
|
// Allows security update status to be changed if neccessary
|
||||||
|
this.checkForSecurityUpdate();
|
||||||
|
}
|
||||||
|
callback(response);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
checkForSecurityUpdate() {
|
||||||
|
if(this.offline()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let latest = SFJS.version();
|
||||||
|
let updateAvailable = this.protocolVersion() !== latest;
|
||||||
|
if(updateAvailable !== this.securityUpdateAvailable) {
|
||||||
|
this.securityUpdateAvailable = updateAvailable;
|
||||||
|
this.$rootScope.$broadcast("security-update-status-changed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.securityUpdateAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
|
presentPasswordWizard(type) {
|
||||||
|
var scope = this.$rootScope.$new(true);
|
||||||
|
scope.type = type;
|
||||||
|
var el = this.$compile( "<password-wizard type='type'></password-wizard>" )(scope);
|
||||||
|
angular.element(document.body).append(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
signOut() {
|
||||||
|
this._keys = null;
|
||||||
|
this.user = null;
|
||||||
|
this._authParams = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* User Preferences */
|
||||||
|
|
||||||
|
configureUserPrefs() {
|
||||||
|
let prefsContentType = "SN|UserPreferences";
|
||||||
|
|
||||||
|
this.singletonManager.registerSingleton({content_type: prefsContentType}, (resolvedSingleton) => {
|
||||||
|
this.userPreferences = resolvedSingleton;
|
||||||
|
this.userPreferencesDidChange();
|
||||||
|
}, (valueCallback) => {
|
||||||
|
// Safe to create. Create and return object.
|
||||||
|
var prefs = new SFItem({content_type: prefsContentType});
|
||||||
|
this.modelManager.addItem(prefs);
|
||||||
|
prefs.setDirty(true);
|
||||||
|
this.$rootScope.sync("authManager singletonCreate");
|
||||||
|
valueCallback(prefs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
userPreferencesDidChange() {
|
||||||
|
this.$rootScope.$broadcast("user-preferences-changed");
|
||||||
|
}
|
||||||
|
|
||||||
|
syncUserPreferences() {
|
||||||
|
this.userPreferences.setDirty(true);
|
||||||
|
this.$rootScope.sync("syncUserPreferences");
|
||||||
|
}
|
||||||
|
|
||||||
|
getUserPrefValue(key, defaultValue) {
|
||||||
|
if(!this.userPreferences) { return defaultValue; }
|
||||||
|
var value = this.userPreferences.getAppDataItem(key);
|
||||||
|
return (value !== undefined && value != null) ? value : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUserPrefValue(key, value, sync) {
|
||||||
|
if(!this.userPreferences) { console.log("Prefs are null, not setting value", key); return; }
|
||||||
|
this.userPreferences.setAppDataItem(key, value);
|
||||||
|
if(sync) {
|
||||||
|
this.syncUserPreferences();
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
angular.module('app').service('authManager', AuthManager);
|
||||||
|
|||||||
@@ -129,16 +129,16 @@ class DesktopManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
desktop_requestBackupFile(callback) {
|
async desktop_requestBackupFile(callback) {
|
||||||
var keys, authParams, protocolVersion;
|
var keys, authParams, protocolVersion;
|
||||||
if(this.authManager.offline() && this.passcodeManager.hasPasscode()) {
|
if(this.authManager.offline() && this.passcodeManager.hasPasscode()) {
|
||||||
keys = this.passcodeManager.keys();
|
keys = this.passcodeManager.keys();
|
||||||
authParams = this.passcodeManager.passcodeAuthParams();
|
authParams = this.passcodeManager.passcodeAuthParams();
|
||||||
protocolVersion = authParams.version;
|
protocolVersion = authParams.version;
|
||||||
} else {
|
} else {
|
||||||
keys = this.authManager.keys();
|
keys = await this.authManager.keys();
|
||||||
authParams = this.authManager.getAuthParams();
|
authParams = this.authManager.getAuthParams();
|
||||||
protocolVersion = this.authManager.protocolVersion();
|
protocolVersion = await this.authManager.protocolVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.modelManager.getAllItemsJSONData(
|
this.modelManager.getAllItemsJSONData(
|
||||||
|
|||||||
326
app/assets/javascripts/app/services/old_authManager.js
Normal file
326
app/assets/javascripts/app/services/old_authManager.js
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
// angular.module('app')
|
||||||
|
// .provider('authManager', function () {
|
||||||
|
//
|
||||||
|
// function domainName() {
|
||||||
|
// var domain_comps = location.hostname.split(".");
|
||||||
|
// var domain = domain_comps[domain_comps.length - 2] + "." + domain_comps[domain_comps.length - 1];
|
||||||
|
// return domain;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.$get = function($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile) {
|
||||||
|
// return new AuthManager($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function AuthManager($rootScope, $timeout, httpManager, modelManager, dbManager, storageManager, singletonManager, $compile) {
|
||||||
|
//
|
||||||
|
// this.loadInitialData = function() {
|
||||||
|
// var userData = storageManager.getItem("user");
|
||||||
|
// if(userData) {
|
||||||
|
// this.user = JSON.parse(userData);
|
||||||
|
// } else {
|
||||||
|
// // legacy, check for uuid
|
||||||
|
// var idData = storageManager.getItem("uuid");
|
||||||
|
// if(idData) {
|
||||||
|
// this.user = {uuid: idData};
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.checkForSecurityUpdate();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.offline = function() {
|
||||||
|
// return !this.user;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.isEphemeralSession = function() {
|
||||||
|
// if(this.ephemeral == null || this.ephemeral == undefined) {
|
||||||
|
// this.ephemeral = JSON.parse(storageManager.getItem("ephemeral", StorageManager.Fixed));
|
||||||
|
// }
|
||||||
|
// return this.ephemeral;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.setEphemeral = function(ephemeral) {
|
||||||
|
// this.ephemeral = ephemeral;
|
||||||
|
// if(ephemeral) {
|
||||||
|
// storageManager.setModelStorageMode(StorageManager.Ephemeral);
|
||||||
|
// storageManager.setItemsMode(StorageManager.Ephemeral);
|
||||||
|
// } else {
|
||||||
|
// storageManager.setModelStorageMode(StorageManager.Fixed);
|
||||||
|
// storageManager.setItemsMode(storageManager.hasPasscode() ? StorageManager.FixedEncrypted : StorageManager.Fixed);
|
||||||
|
// storageManager.setItem("ephemeral", JSON.stringify(false), StorageManager.Fixed);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.getAuthParams = function() {
|
||||||
|
// if(!this._authParams) {
|
||||||
|
// this._authParams = JSON.parse(storageManager.getItem("auth_params"));
|
||||||
|
// }
|
||||||
|
// return this._authParams;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.keys = function() {
|
||||||
|
// if(!this._keys) {
|
||||||
|
// var mk = storageManager.getItem("mk");
|
||||||
|
// if(!mk) {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
// this._keys = {mk: mk, ak: storageManager.getItem("ak")};
|
||||||
|
// }
|
||||||
|
// return this._keys;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.protocolVersion = function() {
|
||||||
|
// var authParams = this.getAuthParams();
|
||||||
|
// if(authParams && authParams.version) {
|
||||||
|
// return authParams.version;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// var keys = this.keys();
|
||||||
|
// if(keys && keys.ak) {
|
||||||
|
// // If there's no version stored, and there's an ak, it has to be 002. Newer versions would have thier version stored in authParams.
|
||||||
|
// return "002";
|
||||||
|
// } else {
|
||||||
|
// return "001";
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.getAuthParamsForEmail = function(url, email, extraParams, callback) {
|
||||||
|
// var requestUrl = url + "/auth/params";
|
||||||
|
// httpManager.getAbsolute(requestUrl, _.merge({email: email}, extraParams), function(response){
|
||||||
|
// callback(response);
|
||||||
|
// }, function(response){
|
||||||
|
// console.error("Error getting auth params", response);
|
||||||
|
// if(typeof response !== 'object') {
|
||||||
|
// response = {error: {message: "A server error occurred while trying to sign in. Please try again."}};
|
||||||
|
// }
|
||||||
|
// callback(response);
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.login = function(url, email, password, ephemeral, strictSignin, extraParams, callback) {
|
||||||
|
// this.getAuthParamsForEmail(url, email, extraParams, (authParams) => {
|
||||||
|
//
|
||||||
|
// // SF3 requires a unique identifier in the auth params
|
||||||
|
// authParams.identifier = email;
|
||||||
|
//
|
||||||
|
// if(authParams.error) {
|
||||||
|
// callback(authParams);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(!authParams || !authParams.pw_cost) {
|
||||||
|
// callback({error : {message: "Invalid email or password."}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(!SFJS.supportedVersions().includes(authParams.version)) {
|
||||||
|
// var message;
|
||||||
|
// if(SFJS.isVersionNewerThanLibraryVersion(authParams.version)) {
|
||||||
|
// // The user has a new account type, but is signing in to an older client.
|
||||||
|
// message = "This version of the application does not support your newer account type. Please upgrade to the latest version of Standard Notes to sign in.";
|
||||||
|
// } else {
|
||||||
|
// // The user has a very old account type, which is no longer supported by this client
|
||||||
|
// message = "The protocol version associated with your account is outdated and no longer supported by this application. Please visit standardnotes.org/help/security for more information.";
|
||||||
|
// }
|
||||||
|
// callback({error: {message: message}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(SFJS.isProtocolVersionOutdated(authParams.version)) {
|
||||||
|
// let message = `The encryption version for your account, ${authParams.version}, is outdated and requires upgrade. You may proceed with login, but are advised to follow prompts for Security Updates once inside. Please visit standardnotes.org/help/security for more information.\n\nClick 'OK' to proceed with login.`
|
||||||
|
// if(!confirm(message)) {
|
||||||
|
// callback({error: {}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(!SFJS.supportsPasswordDerivationCost(authParams.pw_cost)) {
|
||||||
|
// let message = "Your account was created on a platform with higher security capabilities than this browser supports. " +
|
||||||
|
// "If we attempted to generate your login keys here, it would take hours. " +
|
||||||
|
// "Please use a browser with more up to date security capabilities, like Google Chrome or Firefox, to log in."
|
||||||
|
// callback({error: {message: message}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// var minimum = SFJS.costMinimumForVersion(authParams.version);
|
||||||
|
// if(authParams.pw_cost < minimum) {
|
||||||
|
// let message = "Unable to login due to insecure password parameters. Please visit standardnotes.org/help/security for more information.";
|
||||||
|
// callback({error: {message: message}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(strictSignin) {
|
||||||
|
// // Refuse sign in if authParams.version is anything but the latest version
|
||||||
|
// var latestVersion = SFJS.version();
|
||||||
|
// if(authParams.version !== latestVersion) {
|
||||||
|
// let message = `Strict sign in refused server sign in parameters. The latest security version is ${latestVersion}, but your account is reported to have version ${authParams.version}. If you'd like to proceed with sign in anyway, please disable strict sign in and try again.`;
|
||||||
|
// callback({error: {message: message}});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// SFJS.crypto.computeEncryptionKeysForUser(password, authParams).then((keys) => {
|
||||||
|
// var requestUrl = url + "/auth/sign_in";
|
||||||
|
// var params = _.merge({password: keys.pw, email: email}, extraParams);
|
||||||
|
//
|
||||||
|
// httpManager.postAbsolute(requestUrl, params, (response) => {
|
||||||
|
// this.setEphemeral(ephemeral);
|
||||||
|
// this.handleAuthResponse(response, email, url, authParams, keys);
|
||||||
|
// this.checkForSecurityUpdate();
|
||||||
|
// $timeout(() => callback(response));
|
||||||
|
// }, (response) => {
|
||||||
|
// console.error("Error logging in", response);
|
||||||
|
// if(typeof response !== 'object') {
|
||||||
|
// response = {error: {message: "A server error occurred while trying to sign in. Please try again."}};
|
||||||
|
// }
|
||||||
|
// $timeout(() => callback(response));
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// });
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.handleAuthResponse = function(response, email, url, authParams, keys) {
|
||||||
|
// try {
|
||||||
|
// if(url) {
|
||||||
|
// storageManager.setItem("server", url);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.user = response.user;
|
||||||
|
// storageManager.setItem("user", JSON.stringify(response.user));
|
||||||
|
//
|
||||||
|
// this._authParams = authParams;
|
||||||
|
// storageManager.setItem("auth_params", JSON.stringify(authParams));
|
||||||
|
//
|
||||||
|
// storageManager.setItem("jwt", response.token);
|
||||||
|
// this.saveKeys(keys);
|
||||||
|
// } catch(e) {
|
||||||
|
// dbManager.displayOfflineAlert();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.saveKeys = function(keys) {
|
||||||
|
// this._keys = keys;
|
||||||
|
// // pw doesn't need to be saved.
|
||||||
|
// // storageManager.setItem("pw", keys.pw);
|
||||||
|
// storageManager.setItem("mk", keys.mk);
|
||||||
|
// storageManager.setItem("ak", keys.ak);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.register = function(url, email, password, ephemeral, callback) {
|
||||||
|
// SFJS.crypto.generateInitialKeysAndAuthParamsForUser(email, password).then((results) => {
|
||||||
|
// let keys = results.keys;
|
||||||
|
// let authParams = results.authParams;
|
||||||
|
//
|
||||||
|
// var requestUrl = url + "/auth";
|
||||||
|
// var params = _.merge({password: keys.pw, email: email}, authParams);
|
||||||
|
//
|
||||||
|
// httpManager.postAbsolute(requestUrl, params, (response) => {
|
||||||
|
// this.setEphemeral(ephemeral);
|
||||||
|
// this.handleAuthResponse(response, email, url, authParams, keys);
|
||||||
|
// callback(response);
|
||||||
|
// }, (response) => {
|
||||||
|
// console.error("Registration error", response);
|
||||||
|
// if(typeof response !== 'object') {
|
||||||
|
// response = {error: {message: "A server error occurred while trying to register. Please try again."}};
|
||||||
|
// }
|
||||||
|
// callback(response);
|
||||||
|
// })
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.changePassword = function(current_server_pw, newKeys, newAuthParams, callback) {
|
||||||
|
// let email = this.user.email;
|
||||||
|
// let newServerPw = newKeys.pw;
|
||||||
|
//
|
||||||
|
// var requestUrl = storageManager.getItem("server") + "/auth/change_pw";
|
||||||
|
// var params = _.merge({new_password: newServerPw, current_password: current_server_pw}, newAuthParams);
|
||||||
|
//
|
||||||
|
// httpManager.postAbsolute(requestUrl, params, (response) => {
|
||||||
|
// this.handleAuthResponse(response, email, null, newAuthParams, newKeys);
|
||||||
|
// callback(response);
|
||||||
|
//
|
||||||
|
// // Allows security update status to be changed if neccessary
|
||||||
|
// this.checkForSecurityUpdate();
|
||||||
|
// }, (response) => {
|
||||||
|
// if(typeof response !== 'object') {
|
||||||
|
// response = {error: {message: "Something went wrong while changing your password. Your password was not changed. Please try again."}}
|
||||||
|
// }
|
||||||
|
// callback(response);
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.checkForSecurityUpdate = function() {
|
||||||
|
// if(this.offline()) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// let latest = SFJS.version();
|
||||||
|
// let updateAvailable = this.protocolVersion() !== latest;
|
||||||
|
// if(updateAvailable !== this.securityUpdateAvailable) {
|
||||||
|
// this.securityUpdateAvailable = updateAvailable;
|
||||||
|
// $rootScope.$broadcast("security-update-status-changed");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return this.securityUpdateAvailable;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.presentPasswordWizard = function(type) {
|
||||||
|
// var scope = $rootScope.$new(true);
|
||||||
|
// scope.type = type;
|
||||||
|
// var el = $compile( "<password-wizard type='type'></password-wizard>" )(scope);
|
||||||
|
// angular.element(document.body).append(el);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.staticifyObject = function(object) {
|
||||||
|
// return JSON.parse(JSON.stringify(object));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.signOut = function() {
|
||||||
|
// this._keys = null;
|
||||||
|
// this.user = null;
|
||||||
|
// this._authParams = null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// /* User Preferences */
|
||||||
|
//
|
||||||
|
// let prefsContentType = "SN|UserPreferences";
|
||||||
|
//
|
||||||
|
// singletonManager.registerSingleton({content_type: prefsContentType}, (resolvedSingleton) => {
|
||||||
|
// this.userPreferences = resolvedSingleton;
|
||||||
|
// this.userPreferencesDidChange();
|
||||||
|
// }, (valueCallback) => {
|
||||||
|
// // Safe to create. Create and return object.
|
||||||
|
// var prefs = new SFItem({content_type: prefsContentType});
|
||||||
|
// modelManager.addItem(prefs);
|
||||||
|
// prefs.setDirty(true);
|
||||||
|
// $rootScope.sync("authManager singletonCreate");
|
||||||
|
// valueCallback(prefs);
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// this.userPreferencesDidChange = function() {
|
||||||
|
// $rootScope.$broadcast("user-preferences-changed");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.syncUserPreferences = function() {
|
||||||
|
// this.userPreferences.setDirty(true);
|
||||||
|
// $rootScope.sync("syncUserPreferences");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.getUserPrefValue = function(key, defaultValue) {
|
||||||
|
// if(!this.userPreferences) { return defaultValue; }
|
||||||
|
// var value = this.userPreferences.getAppDataItem(key);
|
||||||
|
// return (value !== undefined && value != null) ? value : defaultValue;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// this.setUserPrefValue = function(key, value, sync) {
|
||||||
|
// if(!this.userPreferences) { console.log("Prefs are null, not setting value", key); return; }
|
||||||
|
// this.userPreferences.setAppDataItem(key, value);
|
||||||
|
// if(sync) {
|
||||||
|
// this.syncUserPreferences();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// });
|
||||||
@@ -7,7 +7,7 @@ angular.module('app')
|
|||||||
|
|
||||||
function PasscodeManager($rootScope, $timeout, modelManager, dbManager, authManager, storageManager) {
|
function PasscodeManager($rootScope, $timeout, modelManager, dbManager, authManager, storageManager) {
|
||||||
|
|
||||||
this._hasPasscode = storageManager.getItem("offlineParams", StorageManager.Fixed) != null;
|
this._hasPasscode = storageManager.getItemSync("offlineParams", StorageManager.Fixed) != null;
|
||||||
this._locked = this._hasPasscode;
|
this._locked = this._hasPasscode;
|
||||||
|
|
||||||
this.isLocked = function() {
|
this.isLocked = function() {
|
||||||
@@ -23,7 +23,7 @@ angular.module('app')
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.passcodeAuthParams = function() {
|
this.passcodeAuthParams = function() {
|
||||||
return JSON.parse(storageManager.getItem("offlineParams", StorageManager.Fixed));
|
return JSON.parse(storageManager.getItemSync("offlineParams", StorageManager.Fixed));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.protocolVersion = function() {
|
this.protocolVersion = function() {
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class StorageManager extends SFStorageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setItem(key, value, vaultKey) {
|
async setItem(key, value, vaultKey) {
|
||||||
var storage = this.getVault(vaultKey);
|
var storage = this.getVault(vaultKey);
|
||||||
storage.setItem(key, value);
|
storage.setItem(key, value);
|
||||||
|
|
||||||
@@ -110,25 +110,21 @@ class StorageManager extends SFStorageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getItem(key, vault) {
|
async getItem(key, vault) {
|
||||||
|
return this.getItemSync(key, vault);
|
||||||
|
}
|
||||||
|
|
||||||
|
getItemSync(key, vault) {
|
||||||
var storage = this.getVault(vault);
|
var storage = this.getVault(vault);
|
||||||
return storage.getItem(key);
|
return storage.getItem(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
setBooleanValue(key, value, vault) {
|
async removeItem(key, vault) {
|
||||||
this.setItem(key, JSON.stringify(value), vault);
|
|
||||||
}
|
|
||||||
|
|
||||||
getBooleanValue(key, vault) {
|
|
||||||
return JSON.parse(this.getItem(key, vault));
|
|
||||||
}
|
|
||||||
|
|
||||||
removeItem(key, vault) {
|
|
||||||
var storage = this.getVault(vault);
|
var storage = this.getVault(vault);
|
||||||
storage.removeItem(key);
|
return storage.removeItem(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
async clear() {
|
||||||
this.memoryStorage.clear();
|
this.memoryStorage.clear();
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
}
|
}
|
||||||
@@ -197,36 +193,44 @@ class StorageManager extends SFStorageManager {
|
|||||||
this.modelStorageMode = mode;
|
this.modelStorageMode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllModels(callback) {
|
async getAllModels() {
|
||||||
if(this.modelStorageMode == StorageManager.Fixed) {
|
return new Promise((resolve, reject) => {
|
||||||
this.dbManager.getAllModels(callback);
|
if(this.modelStorageMode == StorageManager.Fixed) {
|
||||||
} else {
|
this.dbManager.getAllModels(resolve);
|
||||||
callback && callback();
|
} else {
|
||||||
}
|
resolve();
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
saveModel(item) {
|
async saveModel(item) {
|
||||||
this.saveModels([item]);
|
return this.saveModels([item]);
|
||||||
}
|
}
|
||||||
|
|
||||||
saveModels(items, onsuccess, onerror) {
|
async saveModels(items, onsuccess, onerror) {
|
||||||
if(this.modelStorageMode == StorageManager.Fixed) {
|
return new Promise((resolve, reject) => {
|
||||||
this.dbManager.saveModels(items, onsuccess, onerror);
|
if(this.modelStorageMode == StorageManager.Fixed) {
|
||||||
} else {
|
this.dbManager.saveModels(items, resolve, reject);
|
||||||
onsuccess && onsuccess();
|
} else {
|
||||||
}
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteModel(item, callback) {
|
async deleteModel(item) {
|
||||||
if(this.modelStorageMode == StorageManager.Fixed) {
|
return new Promise((resolve, reject) => {
|
||||||
this.dbManager.deleteModel(item, callback);
|
if(this.modelStorageMode == StorageManager.Fixed) {
|
||||||
} else {
|
this.dbManager.deleteModel(item, resolve);
|
||||||
callback && callback();
|
} else {
|
||||||
}
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
clearAllModels(callback) {
|
async clearAllModels() {
|
||||||
this.dbManager.clearAllModels(callback);
|
return new Promise((resolve, reject) => {
|
||||||
|
this.dbManager.clearAllModels(resolve);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user