| author | Maciej Bendkowski | 2012-09-24 09:22:53 (EDT) |
|---|---|---|
| committer | Malgorzata Janczarska | 2012-09-27 05:02:12 (EDT) |
| commit | b6cec515287d05e1899ce5f106ffcde29f368c13 (patch) (side-by-side diff) | |
| tree | 4aa237b9d418839e4bd0b58356c653fd86b72ff9 | |
| parent | 1ddd7e1fe65ed2af1a49df72098da685513c06a6 (diff) | |
| download | org.eclipse.orion.client-b6cec515287d05e1899ce5f106ffcde29f368c13.zip org.eclipse.orion.client-b6cec515287d05e1899ce5f106ffcde29f368c13.tar.gz org.eclipse.orion.client-b6cec515287d05e1899ce5f106ffcde29f368c13.tar.bz2 | |
Bug 389041 - Unify git credentials storage and preference storagev20120927-0902
7 files changed, 388 insertions, 424 deletions
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/preferences.js b/bundles/org.eclipse.orion.client.core/web/orion/preferences.js index d7e938d..1dd1fa0 100644 --- a/bundles/org.eclipse.orion.client.core/web/orion/preferences.js +++ b/bundles/org.eclipse.orion.client.core/web/orion/preferences.js @@ -398,7 +398,6 @@ define(['require', 'orion/Deferred', 'orion/xhr'], function(require, Deferred, x * @param {String} name A slash-delimited path to the preference node to return */ getPreferences: function(name, optScope) { - if (!optScope || typeof(optScope) !== "number" || optScope > 7 || optScope < 1) { //$NON-NLS-0$ optScope = PreferencesService.DEFAULT_SCOPE | PreferencesService.LOCAL_SCOPE | PreferencesService.USER_SCOPE; } diff --git a/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/LabeledCommand.js b/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/LabeledCommand.js index 95c5390..6e8a2e2 100644 --- a/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/LabeledCommand.js +++ b/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/LabeledCommand.js @@ -14,7 +14,7 @@ /* This SettingsContainer widget is a dojo border container with a left and right side. The left is for choosing a
category, the right shows the resulting HTML for that category. */
-define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/util', 'orion/commands', 'orion/git/GitCredentialsStorage'], function(messages, require, dojo, dijit, mUtil, mCommands, GitCredentialsStorage) {
+define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/util', 'orion/commands', 'orion/git/gitPreferenceStorage'], function(messages, require, dojo, dijit, mUtil, mCommands, GitPreferenceStorage) {
dojo.declare("orion.widgets.settings.LabeledCommand",[dijit._Widget, dijit._Templated],{ //$NON-NLS-0$
@@ -28,14 +28,7 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u destroy: function(){
dojo.empty(this.domNode);
},
-
- change: function(){
- var gitCredentialsStorage = new GitCredentialsStorage();
- if(gitCredentialsStorage.isBrowserEnabled()){
- gitCredentialsStorage.erasePrompt(this.mylabel.innerHTML);
- }
- },
-
+
postCreate: function(){
this.inherited( arguments );
this.mylabel.innerHTML = this.fieldlabel; //$NON-NLS-0$
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/UserSettings.js b/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/UserSettings.js index f6c6de6..bc6cc19 100644 --- a/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/UserSettings.js +++ b/bundles/org.eclipse.orion.client.core/web/orion/widgets/settings/UserSettings.js @@ -14,7 +14,7 @@ /* This SettingsContainer widget is a dojo border container with a left and right side. The left is for choosing a
category, the right shows the resulting HTML for that category. */
-define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/util', 'orion/commands', 'orion/section', 'orion/git/GitCredentialsStorage', 'orion/widgets/settings/LabeledTextfield', 'orion/widgets/settings/LabeledCheckbox', 'orion/widgets/settings/LabeledCommand', 'orion/widgets/settings/LabeledToggle', 'profile/UsersService', 'orion/widgets/settings/Section' ], function(messages, require, dojo, dijit, mUtil, mCommands, mSection, GitCredentialsStorage) { +define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/util', 'orion/commands', 'orion/section', 'orion/git/gitPreferenceStorage', 'orion/widgets/settings/LabeledTextfield', 'orion/widgets/settings/LabeledCheckbox', 'orion/widgets/settings/LabeledCommand', 'orion/widgets/settings/LabeledToggle', 'profile/UsersService', 'orion/widgets/settings/Section' ], function(messages, require, dojo, dijit, mUtil, mCommands, mSection, GitPreferenceStorage) { dojo.declare("orion.widgets.settings.UserSettings", [dijit._Widget, dijit._Templated], { //$NON-NLS-0$
@@ -59,10 +59,13 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u },
refreshGitCredentials : function(){
- var gitCredentialsStorage = new GitCredentialsStorage();
- if(gitCredentialsStorage.isBrowserEnabled()){
- this.gitCredentialsFields[0].setChecked(gitCredentialsStorage.isUserEnabled());
- }
+ var that = this;
+ var gitPreferenceStorage = new GitPreferenceStorage(this.registry);
+ gitPreferenceStorage.isEnabled().then(
+ function(isEnabled){
+ that.gitCredentialsFields[0].setChecked(isEnabled);
+ }
+ );
},
postCreate: function(){
@@ -91,29 +94,6 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u var passwordSection = new orion.widgets.settings.Section( {sectionName:messages['Password'], container: this.sections, sections: this.passwordFields } );
- /* - linked ------------------------------------------------------ */
-
-// this.linkedFields = [];
-//
-// var toggleLabels = [ messages['Google'], messages['Yahoo'], messages['AOL'], messages['OpenId'] ];
-//
-// var openIds = [ 'https://www.google.com/accounts/o8/id', 'http://me.yahoo.com', 'http://openid.aol.com/', 'http://myopenid.com' ];//$NON-NLS-0$ //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-//
-// var toggleData = {toggleOnState:messages["Linked"], toggleOffState:messages["Unlinked"], toggleOnSwitch:messages['Link'], toggleOffSwitch:messages['Unlink']};
-//
-// for( var toggle = 0; toggle< 4; toggle++ ){
-// toggleData.fieldlabel = toggleLabels[toggle];
-//
-// var toggleWidget = new orion.widgets.settings.LabeledToggle( toggleData );
-//
-// toggleWidget.onAction = dojo.hitch( this, 'confirmOpenId', openIds[toggle] ); //$NON-NLS-0$
-//
-// this.linkedFields.push( toggleWidget );
-//
-// }
-//
-// var linkedSection = new orion.widgets.settings.Section( {sectionName:messages['Linked Accounts'], container: this.sections, sections: this.linkedFields } );
-
/* - git --------------------------------------------------------- */
this.gitFields = [];
@@ -154,12 +134,13 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u var messageService = that.registry.getService("orion.page.message");
- var gitCredentialsStorage = new GitCredentialsStorage();
- if(gitCredentialsStorage.isBrowserEnabled()){
- gitCredentialsStorage.erasePrompt(repository);
- messageService.setProgressResult("Deleted private key for " + repository);
- that.gitCredentialsFields[keyIndex+1].destroy();
- }
+ var gitPreferenceStorage = new GitPreferenceStorage(that.registry);
+ gitPreferenceStorage.remove(repository).then(
+ function(){
+ messageService.setProgressResult("Deleted private key for " + repository);
+ that.gitCredentialsFields[keyIndex+1].destroy();
+ }
+ );
},
visibleWhen : function(item){
return true;
@@ -168,34 +149,34 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u this.commandService.addCommand(erasePrivateKeyCommand);
this.commandService.registerCommandContribution("repositoryItemCommands", "eclipse.orion.git.erasePrivateKey", 1);
- var gitCredentialsStorage = new GitCredentialsStorage();
- if(gitCredentialsStorage.isBrowserEnabled()){
- var repositories = gitCredentialsStorage.getRepositories();
-
- for(var i=0; i<repositories.length; ++i){
- if(!gitCredentialsStorage.getPrompt(repositories[i])){
- var labeledCommand = new orion.widgets.settings.LabeledCommand( {keyIndex: i, fieldlabel: repositories[i], commandService: this.commandService, scopeId: "repositoryItemCommands"} );
- this.gitCredentialsFields.push(labeledCommand);
+ var that = this;
+ var gitPreferenceStorage = new GitPreferenceStorage(this.registry);
+ gitPreferenceStorage.getRepositories().then(
+ function(repositories){
+ for(var i=0; i<repositories.length; ++i){
+ var labeledCommand = new orion.widgets.settings.LabeledCommand( {keyIndex: i, fieldlabel: repositories[i], commandService: that.commandService, scopeId: "repositoryItemCommands"} );
+ that.gitCredentialsFields.push(labeledCommand);
}
+
+ gitCredentialsSection = new orion.widgets.settings.Section( {sectionName:"", container: that.gitSections, sections: that.gitCredentialsFields} );
+
+ var updateGitCredentialsCommand = new mCommands.Command({
+ name: messages["Update"],
+ tooltip: messages["Update Git Credentials"],
+ id: "orion.updateGitCredentials", //$NON-NLS-0$
+ callback: dojo.hitch(that, function(data){
+ that.updateGitCredentials(data.items);
+ })
+
+ });
+
+ that.commandService.addCommand(updateGitCredentialsCommand);
+ that.commandService.registerCommandContribution('gitProfileCommands', "orion.updateGitCredentials", 2); //$NON-NLS-1$ //$NON-NLS-0$
+
+ that.commandService.renderCommands('gitProfileCommands', dojo.byId( 'gitCommands' ), that, that, "button"); //$NON-NLS-1$ //$NON-NLS-0$
+
}
- }
-
- gitCredentialsSection = new orion.widgets.settings.Section( {sectionName:"", container: this.gitSections, sections: this.gitCredentialsFields} );
- - var updateGitCredentialsCommand = new mCommands.Command({
- name: messages["Update"],
- tooltip: messages["Update Git Credentials"],
- id: "orion.updateGitCredentials", //$NON-NLS-0$
- callback: dojo.hitch(this, function(data){
- this.updateGitCredentials(data.items);
- })
-
- });
-
- this.commandService.addCommand(updateGitCredentialsCommand);
- this.commandService.registerCommandContribution('gitProfileCommands', "orion.updateGitCredentials", 2); //$NON-NLS-1$ //$NON-NLS-0$
-
- this.commandService.renderCommands('gitProfileCommands', dojo.byId( 'gitCommands' ), this, this, "button"); //$NON-NLS-1$ //$NON-NLS-0$
+ );
this.linkedAccountSection = new mSection.Section(this.linkedSection, {
id: "linkedAccountSection", //$NON-NLS-0$
@@ -298,16 +279,22 @@ define(['i18n!orion/settings/nls/messages', 'require', 'dojo', 'dijit', 'orion/u updateGitCredentials : function(data){
var messageService = this.registry.getService("orion.page.message"); //$NON-NLS-0$
- // git authentication update
- var gitCredentialsStorage = new GitCredentialsStorage();
- if(gitCredentialsStorage.isBrowserEnabled()){
- if( this.gitCredentialsFields[0].isChecked() ){
- if(window.confirm(messages["Please be aware that your credentials will be stored persistently in the browser."] + '\n' + messages["Do you wish to enable the Key Storage?"])){
- gitCredentialsStorage.userEnable();
- } else { return; }
- } else { gitCredentialsStorage.userDisable(); }
-
- messageService.setProgressResult( messages['Git Credentials successfully updated.'] );
+ // git authentication update + var gitPreferenceStorage = new GitPreferenceStorage(this.registry);
+ if( this.gitCredentialsFields[0].isChecked() ){
+ if(window.confirm(messages["Please be aware that your credentials will be stored persistently in the browser."] + '\n' + messages["Do you wish to enable the Key Storage?"])){
+ gitPreferenceStorage.enable().then(
+ function(){
+ messageService.setProgressResult( messages['Git Credentials successfully updated.'] );
+ }
+ );
+ } else { return; }
+ } else {
+ gitPreferenceStorage.disable().then(
+ function(){
+ messageService.setProgressResult( messages['Git Credentials successfully updated.'] );
+ }
+ ); }
},
diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/GitCredentialsStorage.js b/bundles/org.eclipse.orion.client.git/web/orion/git/GitCredentialsStorage.js deleted file mode 100644 index 24bfcfa..0000000 --- a/bundles/org.eclipse.orion.client.git/web/orion/git/GitCredentialsStorage.js +++ b/dev/null @@ -1,243 +0,0 @@ -/*******************************************************************************
- * @license
- * Copyright (c) 2012 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License v1.0
- * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
- * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
- *
- * Contributors: IBM Corporation - initial API and implementation
- *******************************************************************************/
- /*global define console window document navigator */
-
- define(function(){
-
- /**
- * Local storage support class.
- *
- * @param prefix [required] Used to create a separate namespace for storage.
- */
- function LocalStorage(prefix){
- this._prefix = prefix;
- }
-
- LocalStorage.prototype = {
-
- /**
- * [interface] Stores the pair (key, value)
- */
- set : function(key, value){
- var cKey = this._prefix + "." + key;
- window.localStorage.setItem(cKey, value);
- },
-
- /**
- * [interface] Retrieves the value for the given key.
- * Returns an empty string if key is not present.
- */
- get : function(key){
- var cKey = this._prefix + "." + key;
- return (window.localStorage.getItem(cKey) === null ? "" : window.localStorage.getItem(cKey));
- },
-
- /**
- * [interface] Erases the value for the given key.
- */
- destroy : function(key){
- var cKey = this._prefix + "." + key;
- window.localStorage.removeItem(cKey);
- },
-
- /**
- * [interface] Tests whether local storage is enabled or not.
- */
- available : function(){
- return (window.localStorage ? true : false);
- },
-
- /**
- * [interface] Retrieves all saved keys without the prefix.
- */
- getKeys : function(){
- var result = [];
- for(var i=0; i<window.localStorage.length; ++i){
- var key = window.localStorage.key(i);
- if(key.indexOf(this._prefix) === 0){
- result.push(key.substring(this._prefix.length + 1, key.length));
- }
- }
-
- return result;
- },
-
- /**
- * [interface] Clears all saved data.
- */
- clear : function(){
- var keys = this.getKeys();
-
- for(var i=0; i<keys.length; ++i){
- var key = keys[i];
- this.destroy(key);
- }
- }
- };
-
- LocalStorage.prototype.constructor = LocalStorage;
-
- /**
- * Stores user git credentials for automatic authentication support.
- */
- function GitCredentialsStorage(){
- this._prefix = "orion.git.credentials";
- this._browserEnabled = false;
-
- // detect and use underlying browser storage mechanism
- if(this._supportsLocalStorage()){ this._store = new LocalStorage(this._prefix); this._browserEnabled = true; }
- }
-
- GitCredentialsStorage.prototype = {
-
- /**
- * Detects if local storage is enabled.
- */
- _supportsLocalStorage : function(){
- var tmp = new LocalStorage();
- return tmp.available();
- },
-
- /**
- * Determines whether the storage feature is enabled or not.
- */
- isUserEnabled : function(){
- return (this._store.get("isUserEnabled") !== "" ? true : false);
- },
-
- /**
- * Enables the use of credentials storage.
- */
- userEnable : function(){
- this._store.set("isUserEnabled", "true");
- },
-
- /**
- * Disables the use of credentials storage.
- */
- userDisable : function(){
- this._store.destroy("isUserEnabled");
- },
-
- /**
- * Informs if browser support credentials storage.
- */
- isBrowserEnabled : function(){
- return this._browserEnabled;
- },
-
- /**
- * Informs if automatic authentication support
- * is enabled or not. If no brower storage mechanism
- * is present the support cannot be enabled and we will
- * have to switch to manual authentication instead.
- */
- isEnabled : function(){
- if(this._browserEnabled){ return this.isUserEnabled(); }
- else { return false; }
- },
-
- /**
- * Retrieves the stored private key.
- * Returns an empty string if no key was stored.
- */
- getPrivateKey : function(repository){
- return this._store.get(repository + "::" + "privateKey");
- },
-
- /**
- * Stores the private key.
- */
- setPrivateKey : function(repository, privateKey){
- this._store.set(repository + "::" + "privateKey", privateKey);
- },
-
- /**
- * Erases the private key.
- */
- erasePrivateKey : function(repository){
- this._store.destroy(repository + "::" + "privateKey");
- },
-
- /**
- * Retrieves the stored passphrase.
- * Returns an empty string if no passphrase was found.
- */
- getPassphrase : function(repository){
- return this._store.get(repository + "::" + "passphrase");
- },
-
- /**
- * Stores the passphrase.
- */
- setPassphrase : function(repository, passphrase){
- this._store.set(repository + "::" + "passphrase", passphrase);
- },
-
- /**
- * Erases the passphrase.
- */
- erasePassphrase : function(repository){
- this._store.destroy(repository + "::" + "passphrase");
- },
-
- /**
- * Returns whether the given repository should prompt for credentials or not.
- */
- getPrompt : function(repository){
- return (this._store.get(repository + "::" + "prompt") === "false" ? false : true);
- },
-
- /**
- * Stores the information not to prompt for credentials.
- */
- setPrompt : function(repository){
- this._store.set(repository + "::" + "prompt", "false");
- },
-
- /**
- * Erases the information to prompt for credentials.
- */
- erasePrompt : function(repository){
- this._store.destroy(repository + "::" + "prompt");
- },
-
- /**
- * Clears all credentials.
- */
- clearCredentials : function(){
- this._store.clear();
- },
-
- /**
- * Returns repositories which have stored credentials.
- */
- getRepositories : function(){
- var keys = this._store.getKeys();
- var result = [];
-
- for(var i=0; i<keys.length; ++i){
- if(keys[i] !== "isUserEnabled"){
- var repository = keys[i].substring(0, keys[i].indexOf("::"));
- if(result.indexOf(repository) === -1){
- result.push(repository);
- }
- }
- }
-
- return result;
- }
- };
-
- GitCredentialsStorage.prototype.constructor = GitCredentialsStorage;
-
- return GitCredentialsStorage;
- });
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js b/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js index c5b868d..7d59dad 100644 --- a/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js +++ b/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js @@ -11,11 +11,11 @@ /*global alert confirm orion window widgets eclipse:true serviceRegistry define */ /*jslint browser:true eqeqeq:false laxbreak:true */ -define(['i18n!git/nls/gitmessages', 'require', 'dojo', 'orion/commands', 'orion/util', 'orion/git/util', 'orion/compare/compareUtils', 'orion/git/GitCredentialsStorage', 'orion/git/widgets/CloneGitRepositoryDialog', +define(['i18n!git/nls/gitmessages', 'require', 'dojo', 'orion/commands', 'orion/util', 'orion/git/util', 'orion/compare/compareUtils', 'orion/git/gitPreferenceStorage', 'orion/git/widgets/CloneGitRepositoryDialog', 'orion/git/widgets/AddRemoteDialog', 'orion/git/widgets/GitCredentialsDialog', 'orion/widgets/NewItemDialog', 'orion/git/widgets/RemotePrompterDialog', 'orion/git/widgets/ApplyPatchDialog', 'orion/git/widgets/OpenCommitDialog', 'orion/git/widgets/ConfirmPushDialog', 'orion/git/widgets/ReviewRequestDialog', 'orion/git/widgets/ContentDialog', 'orion/git/widgets/CommitDialog'], - function(messages, require, dojo, mCommands, mUtil, mGitUtil, mCompareUtils, GitCredentialsStorage) { + function(messages, require, dojo, mCommands, mUtil, mGitUtil, mCompareUtils, GitPreferenceStorage) { /** * @namespace The global container for eclipse APIs. @@ -116,38 +116,40 @@ var exports = {}; exports.handleSshAuthenticationError = function(serviceRegistry, errorData, options, func, title){ var repository = errorData.Url; - var gitCredentialsStorage = new GitCredentialsStorage(); - var loadedPrivateKey = "", loadedPassphrase = ""; - - if(gitCredentialsStorage.isEnabled() && !gitCredentialsStorage.getPrompt(repository)){ - loadedPrivateKey = gitCredentialsStorage.getPrivateKey(repository); - loadedPassphrase = gitCredentialsStorage.getPassphrase(repository); - } - if(loadedPrivateKey !== ""){ + var failure = function(){ + var credentialsDialog = new orion.git.widgets.GitCredentialsDialog({ + title: title, + serviceRegistry: serviceRegistry, + func: func, + errordata: options.errordata, + failedOperation: options.failedOperation + }); + + credentialsDialog.startup(); + credentialsDialog.show(); if(options.failedOperation){ var progressService = serviceRegistry.getService("orion.page.progress"); //$NON-NLS-0$ dojo.hitch(progressService, progressService.removeOperation)(options.failedOperation.Location, options.failedOperation.Id); } + }; - func({ knownHosts: options.knownHosts, gitSshUsername: "", gitSshPassword: "", gitPrivateKey: loadedPrivateKey, gitPassphrase: loadedPassphrase}); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$ - return; - } - - var credentialsDialog = new orion.git.widgets.GitCredentialsDialog({ - title: title, - serviceRegistry: serviceRegistry, - func: func, - errordata: options.errordata, - failedOperation: options.failedOperation - }); - - credentialsDialog.startup(); - credentialsDialog.show(); - if(options.failedOperation){ - var progressService = serviceRegistry.getService("orion.page.progress"); //$NON-NLS-0$ - dojo.hitch(progressService, progressService.removeOperation)(options.failedOperation.Location, options.failedOperation.Id); - } + var gitPreferenceStorage = new GitPreferenceStorage(serviceRegistry); + gitPreferenceStorage.get(repository).then( + function(credentials){ + if(credentials.gitPrivateKey !== "" || credentials.gitSshUsername !== "" || credentials.gitSshPassword !== ""){ + if(options.failedOperation){ + var progressService = serviceRegistry.getService("orion.page.progress"); //$NON-NLS-0$ + dojo.hitch(progressService, progressService.removeOperation)(options.failedOperation.Location, options.failedOperation.Id); + } + + func({ knownHosts: options.knownHosts, gitSshUsername: credentials.gitSshUsername, gitSshPassword: credentials.gitSshPassword, gitPrivateKey: credentials.gitPrivateKey, gitPassphrase: credentials.gitPassphrase}); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$ + return; + } + + failure(); + }, failure + ); }; exports.getDefaultSshOptions = function(serviceRegistry, authParameters){ @@ -232,7 +234,7 @@ var exports = {}; exports.gatherSshCredentials = function(serviceRegistry, data, title){ var def = new dojo.Deferred(); - var repository = undefined; + var repository; //TODO This should be somehow unified if(data.items.RemoteLocation !== undefined){ repository = data.items.RemoteLocation[0].GitUrl; } @@ -260,56 +262,65 @@ var exports = {}; var sshService = serviceRegistry.getService("orion.net.ssh"); //$NON-NLS-0$ sshService.addKnownHosts(errorData.Host + " " + errorData.KeyType + " " + errorData.HostKey).then( //$NON-NLS-1$ //$NON-NLS-0$ function(){ - var gitCredentialsStorage = new GitCredentialsStorage(); - var loadedPrivateKey = "", loadedPassphrase = ""; - - if(gitCredentialsStorage.isEnabled() && !gitCredentialsStorage.getPrompt(repository)){ - loadedPrivateKey = gitCredentialsStorage.getPrivateKey(repository); - loadedPassphrase = gitCredentialsStorage.getPassphrase(repository); - } - - triggerCallback({ gitSshUsername: "", gitSshPassword: "", gitPrivateKey: loadedPrivateKey, gitPassphrase: loadedPassphrase}); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$ + var gitPreferenceStorage = new GitPreferenceStorage(serviceRegistry); + gitPreferenceStorage.get(repository).then( + function(credentials){ + triggerCallback(credentials); + }, + function(){ + triggerCallback({ + gitSshUsername: "", + gitSshPassword: "", + gitPrivateKey: "", + gitPassphrase: "" + }); + } + ); } - ); + ); } return def; } - var gitCredentialsStorage = new GitCredentialsStorage(); - if(gitCredentialsStorage.isEnabled() && !gitCredentialsStorage.getPrompt(repository)){ - var loadedPrivateKey = gitCredentialsStorage.getPrivateKey(repository); - var loadedPassphrase = gitCredentialsStorage.getPassphrase(repository); - - if(loadedPrivateKey !== ""){ - triggerCallback({ gitSshUsername: "", gitSshPassword: "", gitPrivateKey: loadedPrivateKey, gitPassphrase: loadedPassphrase}); - return def; + var failure = function(){ + if (!data.parameters && !data.optionsRequested){ + triggerCallback({gitSshUsername: "", gitSshPassword: "", gitPrivateKey: "", gitPassphrase: ""}); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$ + return; } - } - if (!data.parameters && !data.optionsRequested){ - triggerCallback({gitSshUsername: "", gitSshPassword: "", gitPrivateKey: "", gitPassphrase: ""}); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$ - return def; - } + // try to gather creds from the slideout first + if (data.parameters && !data.optionsRequested) { + var sshUser = data.parameters ? data.parameters.valueFor("sshuser") : ""; //$NON-NLS-0$ + var sshPassword = data.parameters ? data.parameters.valueFor("sshpassword") : ""; //$NON-NLS-0$ + triggerCallback({ gitSshUsername: sshUser, gitSshPassword: sshPassword, gitPrivateKey: "", gitPassphrase: ""}); //$NON-NLS-0$ + return; + } + + // use the old creds dialog + var credentialsDialog = new orion.git.widgets.GitCredentialsDialog({ + title: title, + serviceRegistry: serviceRegistry, + func: triggerCallback, + errordata: errorData + }); + + credentialsDialog.startup(); + credentialsDialog.show(); + return; + }; - // try to gather creds from the slideout first - if (data.parameters && !data.optionsRequested) { - var sshUser = data.parameters ? data.parameters.valueFor("sshuser") : ""; //$NON-NLS-0$ - var sshPassword = data.parameters ? data.parameters.valueFor("sshpassword") : ""; //$NON-NLS-0$ - triggerCallback({ gitSshUsername: sshUser, gitSshPassword: sshPassword, gitPrivateKey: "", gitPassphrase: ""}); //$NON-NLS-0$ - return def; - } - - // use the old creds dialog - var credentialsDialog = new orion.git.widgets.GitCredentialsDialog({ - title: title, - serviceRegistry: serviceRegistry, - func: triggerCallback, - errordata: errorData - }); + var gitPreferenceStorage = new GitPreferenceStorage(serviceRegistry); + gitPreferenceStorage.get(repository).then( + function(credentials){ + if(credentials.gitPrivateKey !== "" || credentials.gitSshUsername !== "" || credentials.gitSshPassword !== ""){ + triggerCallback(credentials); + return; + } + + failure(); + }, failure + ); - credentialsDialog.startup(); - credentialsDialog.show(); - return def; }; @@ -325,13 +336,11 @@ var exports = {}; } if(jsonData.HttpCode == 500 || jsonData.HttpCode == 400){ - var repository = jsonData.JsonData.Url; - - var gitCredentialsStorage = new GitCredentialsStorage(); - if(gitCredentialsStorage.isEnabled()){ - gitCredentialsStorage.erasePrivateKey(repository); - gitCredentialsStorage.erasePassphrase(repository); - gitCredentialsStorage.erasePrompt(repository); + if(jsonData.JsonData !== undefined){ + var repository = jsonData.JsonData.Url; + + var gitPreferenceStorage = new GitPreferenceStorage(serviceRegistry); + gitPreferenceStorage.remove(repository); } } diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/gitPreferenceStorage.js b/bundles/org.eclipse.orion.client.git/web/orion/git/gitPreferenceStorage.js new file mode 100644 index 0000000..7eedcd1 --- a/dev/null +++ b/bundles/org.eclipse.orion.client.git/web/orion/git/gitPreferenceStorage.js @@ -0,0 +1,199 @@ +/*******************************************************************************
+ * @license
+ * Copyright (c) 2012 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0
+ * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
+ * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
+ *
+ * Contributors: IBM Corporation - initial API and implementation
+ *******************************************************************************/
+ /*global define console window document navigator */
+
+ define(['orion/Deferred'], function(Deferred){
+
+ /**
+ * Support class for using the orion preferences as a local
+ * git credentials storage.
+ *
+ * @param serviceRegistry [required] Service registry
+ */
+ function PreferenceStorage(serviceRegistry){
+ this._prefix = "/git/credentials";
+ this._preferenceService = serviceRegistry.getService("orion.core.preference");
+ }
+
+ PreferenceStorage.prototype = {
+ /**
+ * Saves the given git credentials for the given repository.
+ *
+ * @param repository [required] unique repository URL
+ * @param userCredentials.gitSshUsername [optional] git ssh username
+ * @param userCredentials.gitSshPassword [optional] git ssh password
+ * @param userCredentials.gitPrivateKey [optional] git private key
+ * @param userCredentials.gitPassphrase [optional] git passphrase
+ */
+ put : function(repository, userCredentials){
+ var credentials = {
+ gitSshUsername : "",
+ gitSshPassword : "",
+ gitPrivateKey : "",
+ gitPassphrase : ""
+ };
+
+ if(userCredentials !== undefined){
+ if(userCredentials.sshUsername !== undefined) { credentials.gitSshUsername = userCredentials.gitSshUsername; }
+ if(userCredentials.sshPassword !== undefined) { credentials.gitSshPassword = userCredentials.gitSshPassword; }
+ if(userCredentials.gitPrivateKey !== undefined) { credentials.gitPrivateKey = userCredentials.gitPrivateKey; }
+ if(userCredentials.gitPassphrase !== undefined) { credentials.gitPassphrase = userCredentials.gitPassphrase; }
+ }
+
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ var settings = pref.get("settings");
+ if(settings === undefined){
+ d.reject();
+ return;
+ }
+
+ if(settings.enabled){
+ pref.put(repository, credentials);
+ d.resolve();
+ } else { d.reject(); }
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Retrieves git credentials for the given repository.
+ * Some credentials fields may be in form of empty strings if not present in the storage.
+ *
+ * @param repository [required] unique repository URL
+ */
+ get : function(repository){
+ var credentials = {
+ gitSshUsername : "",
+ gitSshPassword : "",
+ gitPrivateKey : "",
+ gitPassphrase : ""
+ };
+
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ var settings = pref.get("settings");
+ if(settings === undefined){
+ d.reject();
+ return;
+ }
+
+ if(settings.enabled){
+ var userCredentials = pref.get(repository);
+ if(userCredentials !== undefined) { d.resolve(userCredentials); }
+ else { d.resolve(credentials); }
+ } else { d.reject(); }
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Determines whether the storage is enabled by the user or not.
+ */
+ isEnabled : function(){
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ var settings = pref.get("settings");
+ if(settings === undefined){
+ d.resolve(false);
+ return;
+ }
+
+ d.resolve(settings.enabled);
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Enables the storage for futher use.
+ */
+ enable : function(){
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ pref.put("settings", { enabled : true });
+ d.resolve();
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Disables the storage.
+ * Please note no credentials are erased.
+ */
+ disable : function(){
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ pref.put("settings", { enabled : false });
+ d.resolve();
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Removes all credentials for the given repository.
+ *
+ * @param repository [required] unique repository URL
+ */
+ remove : function(repository){
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ pref.remove(repository);
+ d.resolve();
+ }
+ );
+
+ return d;
+ },
+
+ /**
+ * Returns all repository URLs, which have stored
+ * credentials in the storage.
+ */
+ getRepositories : function(){
+ var d = new Deferred();
+ this._preferenceService.getPreferences(this._prefix, 2).then(
+ function(pref){
+ var result = [];
+ var repositories = pref.keys();
+
+ for(var i=0; i<repositories.length; ++i){
+ if(repositories[i] !== "settings"){
+ result.push(repositories[i]);
+ }
+ }
+
+ d.resolve(result);
+ }
+ );
+
+ return d;
+ }
+ };
+
+ PreferenceStorage.prototype.constructor = PreferenceStorage;
+ return PreferenceStorage;
+ });
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/widgets/GitCredentialsDialog.js b/bundles/org.eclipse.orion.client.git/web/orion/git/widgets/GitCredentialsDialog.js index 77865cd..b7055f9 100644 --- a/bundles/org.eclipse.orion.client.git/web/orion/git/widgets/GitCredentialsDialog.js +++ b/bundles/org.eclipse.orion.client.git/web/orion/git/widgets/GitCredentialsDialog.js @@ -11,7 +11,7 @@ /*global dojo dijit widgets FileReader define orion*/ /*jslint browser:true*/ -define(['i18n!git/nls/gitmessages', 'dojo', 'dijit', 'dijit/Tooltip', 'orion/git/GitCredentialsStorage', 'dijit/Dialog', 'orion/widgets/_OrionDialogMixin', 'text!orion/git/widgets/templates/GitCredentialsDialog.html'], function(messages, dojo, dijit, Tooltip, GitCredentialsStorage) { +define(['i18n!git/nls/gitmessages', 'dojo', 'dijit', 'dijit/Tooltip', 'orion/git/gitPreferenceStorage', 'dijit/Dialog', 'orion/widgets/_OrionDialogMixin', 'text!orion/git/widgets/templates/GitCredentialsDialog.html'], function(messages, dojo, dijit, Tooltip, GitPreferenceStorage) { /** @@ -84,13 +84,19 @@ dojo.declare("orion.git.widgets.GitCredentialsDialog", [dijit.Dialog, orion.widg setTimeout(function () { self.gitSshPassword.focus(); }, 400); } + var that = this; + // display prompt checkbox only when it makes sense - var gitCredentialsStorage = new GitCredentialsStorage(); - if(!gitCredentialsStorage.isEnabled()){ - dojo.style(this.gitSavePrivateKey, "display", "none"); - dojo.style(this.gitSavePrivateKeyLabel, "display", "none"); - dojo.style(this.gitSavePrivateKeyInfo, "display", "none"); - } + var gitPreferenceStorage = new GitPreferenceStorage(self.options.serviceRegistry); + gitPreferenceStorage.isEnabled().then( + function(isEnabled){ + if(!isEnabled){ + dojo.style(that.gitSavePrivateKey, "display", "none"); + dojo.style(that.gitSavePrivateKeyLabel, "display", "none"); + dojo.style(that.gitSavePrivateKeyInfo, "display", "none"); + } + } + ); dojo.connect(this.gitSshPassword, "onfocus", null, dojo.hitch(this, function(){ this.isSshPassword.checked = true; @@ -155,30 +161,44 @@ dojo.declare("orion.git.widgets.GitCredentialsDialog", [dijit.Dialog, orion.widg reader.onload = (function(f){ return function(e){ loadedPrivateKey = e.target.result; - - //save key - var gitCredentialsStorage = new GitCredentialsStorage(); - if(gitCredentialsStorage.isEnabled() && self.gitSavePrivateKey.checked){ - gitCredentialsStorage.setPrivateKey(repository, loadedPrivateKey); - gitCredentialsStorage.setPassphrase(repository, self.gitPassphrase.value); - gitCredentialsStorage.setPrompt(repository); + + var gitPreferenceStorage = new GitPreferenceStorage(self.options.serviceRegistry); + if(self.gitSavePrivateKey.checked){ + + gitPreferenceStorage.put(repository, { + gitPrivateKey : loadedPrivateKey, + gitPassphrase : self.gitPassphrase.value + }).then( + function(){ + process(loadedPrivateKey); + } + ); + + } else { + process(loadedPrivateKey); } - - process(loadedPrivateKey); }; }(this.privateKeyFile)); reader.readAsText(this.privateKeyFile); } else if(loadedPrivateKey){ - //save key - var gitCredentialsStorage = new GitCredentialsStorage(); - if(gitCredentialsStorage.isEnabled() && self.gitSavePrivateKey.checked){ - gitCredentialsStorage.setPrivateKey(repository, loadedPrivateKey); - gitCredentialsStorage.setPassphrase(repository, self.gitPassphrase.value); - gitCredentialsStorage.setPrompt(repository); - } + + //save key + var gitPreferenceStorage = new GitPreferenceStorage(self.options.serviceRegistry); + if(self.gitSavePrivateKey.checked){ + + gitPreferenceStorage.put(repository, { + gitPrivateKey : loadedPrivateKey, + gitPassphrase : self.gitPassphrase.value + }).then( + function(){ + process(loadedPrivateKey); + } + ); - process(loadedPrivateKey); + } else { + process(loadedPrivateKey); + } } else { process(loadedPrivateKey); } } }); |

