* allow deletion of OSDB login

This commit is contained in:
Reinhard Pointner 2015-05-09 06:38:47 +00:00
parent a5652f4d0e
commit a2722d22f4
3 changed files with 32 additions and 14 deletions

View File

@ -8,7 +8,6 @@ import static net.filebot.util.FileUtilities.*;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
@ -287,16 +286,27 @@ public final class WebServices {
}
public static void setLogin(String id, String user, String password) {
if (user == null || password == null || user.contains(LOGIN_SEPARATOR) || (user.isEmpty() && !password.isEmpty()) || (!user.isEmpty() && password.isEmpty())) {
throw new IllegalArgumentException(String.format("Illegal login: %s:%s", user, password));
}
if (LOGIN_OPENSUBTITLES.equals(id)) {
String password_md5 = md5(StandardCharsets.UTF_8.encode(password));
OpenSubtitles.setUser(user, password_md5);
Settings.forPackage(WebServices.class).put(id, String.join(LOGIN_SEPARATOR, user, password_md5));
// delete login
if ((user == null || user.isEmpty()) && (password == null || password.isEmpty())) {
if (LOGIN_OPENSUBTITLES.equals(id)) {
OpenSubtitles.setUser("", "");
Settings.forPackage(WebServices.class).remove(id);
} else {
throw new IllegalArgumentException();
}
} else {
throw new IllegalArgumentException();
// enter login
if (user == null || password == null || user.contains(LOGIN_SEPARATOR) || (user.isEmpty() && !password.isEmpty()) || (!user.isEmpty() && password.isEmpty())) {
throw new IllegalArgumentException(String.format("Illegal login: %s:%s", user, password));
}
if (LOGIN_OPENSUBTITLES.equals(id)) {
String password_md5 = md5(password);
OpenSubtitles.setUser(user, password_md5);
Settings.forPackage(WebServices.class).put(id, String.join(LOGIN_SEPARATOR, user, password_md5));
} else {
throw new IllegalArgumentException();
}
}
}

View File

@ -3,6 +3,7 @@ package net.filebot.ui.subtitle;
import static net.filebot.Settings.*;
import static net.filebot.ui.LanguageComboBoxModel.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.SwingUI.*;
import java.awt.Color;
@ -291,9 +292,9 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
osdbGroup.add(osdbPass, "growx, wrap unrel");
// restore values
String[] osdbAuth = WebServices.getLogin("osdb.user");
String[] osdbAuth = WebServices.getLogin(WebServices.LOGIN_OPENSUBTITLES);
osdbUser.setText(osdbAuth[0]);
osdbPass.setText(osdbAuth[1]);
// osdbPass.setText(osdbAuth[1]); // password is stored as MD5 hash so we can't restore it
if (osdbUser.getText().isEmpty()) {
osdbGroup.add(new LinkButton("Register", "Register to increase your download quota", WebServices.OpenSubtitles.getIcon(), URI.create("http://www.opensubtitles.org/en/newuser")), "spanx 2, tag left");
@ -317,7 +318,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
try {
if (osdbUser.getText().length() > 0 && osdbPass.getPassword().length > 0) {
final OpenSubtitlesClient osdb = new OpenSubtitlesClient(getApplicationName(), getApplicationVersion());
osdb.setUser(osdbUser.getText(), new String(osdbPass.getPassword()));
osdb.setUser(osdbUser.getText(), md5(new String(osdbPass.getPassword())));
osdb.login();
// do some status checks in background (since OpenSubtitles can be really really slow)
@ -333,6 +334,8 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
Logger.getLogger(SubtitlePanel.class.getName()).log(Level.WARNING, e.toString());
}
});
} else if (osdbUser.getText().isEmpty()) {
WebServices.setLogin(WebServices.LOGIN_OPENSUBTITLES, null, null); // delete login details
}
} catch (Exception e) {
UILogger.log(Level.WARNING, "OpenSubtitles: " + e.getMessage());
@ -341,7 +344,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
authPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if (approved) {
WebServices.setLogin("osdb.user", osdbUser.getText(), new String(osdbPass.getPassword()));
WebServices.setLogin(WebServices.LOGIN_OPENSUBTITLES, osdbUser.getText(), new String(osdbPass.getPassword()));
authPanel.setVisible(false);
}
}

View File

@ -16,6 +16,7 @@ import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
@ -630,6 +631,10 @@ public final class FileUtilities {
return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(replacement);
}
public static String md5(String string) {
return md5(StandardCharsets.UTF_8.encode(string));
}
public static String md5(byte[] data) {
return md5(ByteBuffer.wrap(data));
}