IDEA-66556 github should store password in IDEA password safe

This commit is contained in:
Oleg Shpynov
2011-03-25 22:23:50 +03:00
parent 8b40f0ceba
commit 6ee7295deb
@@ -1,10 +1,27 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.github;
import com.intellij.ide.passwordSafe.PasswordSafe;
import com.intellij.ide.passwordSafe.PasswordSafeException;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.util.PasswordUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
@@ -24,13 +41,14 @@ public class GithubSettings implements PersistentStateComponent<Element> {
private static final String GITHUB_SETTINGS_TAG = "GithubSettings";
private static final String LOGIN = "Login";
private static final String PASSWORD = "Password";
private static final String HOST = "Host";
private static final String GITHUB = "github.com";
private static final String GITHUB_SETTINGS_PASSWORD_KEY = "GITHUB_SETTINGS_PASSWORD_KEY";
private String myLogin;
private String myPassword;
private String myHost;
private static final Logger LOG = Logger.getInstance(GithubSettings.class.getName());
public static GithubSettings getInstance(){
return ServiceManager.getService(GithubSettings.class);
@@ -40,31 +58,23 @@ public class GithubSettings implements PersistentStateComponent<Element> {
if (StringUtil.isEmptyOrSpaces(myLogin) && StringUtil.isEmptyOrSpaces(myPassword) && StringUtil.isEmptyOrSpaces(myHost)) {
return null;
}
try {
PasswordSafe.getInstance().storePassword(null, GithubSettings.class, GITHUB_SETTINGS_PASSWORD_KEY, myPassword);
}
catch (PasswordSafeException e) {
LOG.error(e);
}
final Element element = new Element(GITHUB_SETTINGS_TAG);
element.setAttribute(LOGIN, getLogin());
element.setAttribute(PASSWORD, getEncodedPassword());
element.setAttribute(HOST, getHost());
return element;
}
public String getEncodedPassword() {
return PasswordUtil.encodePassword(getPassword());
}
public void setEncodedPassword(final String password) {
try {
setPassword(PasswordUtil.decodePassword(password));
}
catch (NumberFormatException e) {
// do nothing
}
}
public void loadState(@NotNull final Element element) {
try {
setLogin(element.getAttributeValue(LOGIN));
setEncodedPassword(element.getAttributeValue(PASSWORD));
setHost(element.getAttributeValue(HOST));
setPassword(PasswordSafe.getInstance().getPassword(null, GithubSettings.class, GITHUB_SETTINGS_PASSWORD_KEY));
}
catch (Exception e) {
// ignore