From f7791271668cdcf6e87b638f0e6475ccdea6a3f9 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Fri, 11 Dec 2020 16:34:53 +0000 Subject: [PATCH] JAL-3775 JAL-3633 Make java.net.Authenticator code conditional on not being JalviewJS --- src/jalview/bin/Cache.java | 151 ++++++++++++++++++++--------------- src/jalview/jbgui/GPreferences.java | 89 ++++++++++++--------- 2 files changed, 136 insertions(+), 104 deletions(-) diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index e6f86fe..153eaff 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -1411,80 +1411,92 @@ public class Cache Arrays.fill(displayHttpPw, '*'); Cache.debug("CACHE Proxy: setting new Authenticator with httpUser='" + httpUser + "' httpPassword='" + displayHttpPw + "'"); - Authenticator.setDefault(new Authenticator() + if (!Platform.isJS()) + /* * + * java.net.Authenticator not implemented in SwingJS yet + * + * @j2sIgnore + * + */ { - @Override - protected PasswordAuthentication getPasswordAuthentication() + Authenticator.setDefault(new Authenticator() { - if (getRequestorType() == RequestorType.PROXY) + @Override + protected PasswordAuthentication getPasswordAuthentication() { - String protocol = getRequestingProtocol(); - boolean needProxyPasswordSet = false; - if (customProxySet && - // we have a username but no password for the scheme being - // requested - (protocol.equalsIgnoreCase("http") - && (httpUser != null && httpUser.length() > 0 - && (httpPassword == null - || httpPassword.length == 0))) - || (protocol.equalsIgnoreCase("https") - && (httpsUser != null - && httpsUser.length() > 0 - && (httpsPassword == null - || httpsPassword.length == 0)))) + if (getRequestorType() == RequestorType.PROXY) { - // open Preferences -> Connections - String message = MessageManager - .getString("label.proxy_password_required"); - Preferences.openPreferences(Preferences.CONNECTIONS_TAB, - message); - Preferences.getInstance() - .proxyAuthPasswordCheckHighlight(true, true); - } - else - { - try + String protocol = getRequestingProtocol(); + boolean needProxyPasswordSet = false; + if (customProxySet && + // we have a username but no password for the scheme being + // requested + (protocol.equalsIgnoreCase("http") + && (httpUser != null && httpUser.length() > 0 + && (httpPassword == null + || httpPassword.length == 0))) + || (protocol.equalsIgnoreCase("https") + && (httpsUser != null + && httpsUser.length() > 0 + && (httpsPassword == null + || httpsPassword.length == 0)))) + { + // open Preferences -> Connections + String message = MessageManager + .getString("label.proxy_password_required"); + Preferences.openPreferences(Preferences.CONNECTIONS_TAB, + message); + Preferences.getInstance() + .proxyAuthPasswordCheckHighlight(true, true); + } + else { - if (protocol.equalsIgnoreCase("http") - && getRequestingHost().equalsIgnoreCase(httpHost) - && getRequestingPort() == Integer - .valueOf(httpPort)) + try { - Cache.debug( - "AUTHENTICATOR returning PasswordAuthentication(\"" - + httpUser + "\", '" - + new String(displayHttpPw) + "')"); - return new PasswordAuthentication(httpUser, - httpPassword); - } - if (protocol.equalsIgnoreCase("https") - && getRequestingHost().equalsIgnoreCase(httpsHost) - && getRequestingPort() == Integer - .valueOf(httpsPort)) + if (protocol.equalsIgnoreCase("http") + && getRequestingHost() + .equalsIgnoreCase(httpHost) + && getRequestingPort() == Integer + .valueOf(httpPort)) + { + Cache.debug( + "AUTHENTICATOR returning PasswordAuthentication(\"" + + httpUser + "\", '" + + new String(displayHttpPw) + "')"); + return new PasswordAuthentication(httpUser, + httpPassword); + } + if (protocol.equalsIgnoreCase("https") + && getRequestingHost() + .equalsIgnoreCase(httpsHost) + && getRequestingPort() == Integer + .valueOf(httpsPort)) + { + char[] displayHttpsPw = new char[httpPassword.length]; + Arrays.fill(displayHttpsPw, '*'); + Cache.debug( + "AUTHENTICATOR returning PasswordAuthentication(\"" + + httpsUser + "\", '" + displayHttpsPw + + "'"); + return new PasswordAuthentication(httpsUser, + httpsPassword); + } + } catch (NumberFormatException e) { - char[] displayHttpsPw = new char[httpPassword.length]; - Arrays.fill(displayHttpsPw, '*'); - Cache.debug( - "AUTHENTICATOR returning PasswordAuthentication(\"" - + httpsUser + "\", '" + displayHttpsPw - + "'"); - return new PasswordAuthentication(httpsUser, - httpsPassword); + Cache.error("Problem with proxy port values [http:" + + httpPort + ", https:" + httpsPort + "]"); } - } catch (NumberFormatException e) - { - Cache.error("Problem with proxy port values [http:" - + httpPort + ", https:" + httpsPort + "]"); + Cache.debug( + "AUTHENTICATOR after trying to get PasswordAuthentication"); } - Cache.debug( - "AUTHENTICATOR after trying to get PasswordAuthentication"); } + // non proxy request + Cache.debug("AUTHENTICATOR returning null"); + return null; } - // non proxy request - Cache.debug("AUTHENTICATOR returning null"); - return null; - } - }); + }); + } // end of j2sIgnore for java.net.Authenticator + // required to re-enable basic authentication (should be okay for a // local proxy) Cache.debug( @@ -1503,8 +1515,17 @@ public class Cache /* as noted above, due to bug in java this doesn't work if the sun.net.www.protocol.http.AuthCache * has working credentials. No workaround for Java 11. */ - Cache.debug("AUTHENTICATOR setting default Authenticator to null"); - Authenticator.setDefault(null); + if (!Platform.isJS()) + /* * + * java.net.Authenticator not implemented in SwingJS yet + * + * @j2sIgnore + * + */ + { + Cache.debug("AUTHENTICATOR setting default Authenticator to null"); + Authenticator.setDefault(null); + } } // nonProxyHosts not currently configurable in Preferences diff --git a/src/jalview/jbgui/GPreferences.java b/src/jalview/jbgui/GPreferences.java index 38a4732..5af94e4 100755 --- a/src/jalview/jbgui/GPreferences.java +++ b/src/jalview/jbgui/GPreferences.java @@ -1205,45 +1205,56 @@ public class GPreferences extends JPanel gbc.gridy++; proxyPanel.add(hpPanel, gbc); - // Require authentication checkbox - gbc.gridy++; - proxyPanel.add(proxyAuth, gbc); - - // username and password - JPanel upPanel = new JPanel(); - upPanel.setLayout(new GridBagLayout()); - // username row - c.gridy = 0; - c.gridx = 0; - c.gridwidth = 1; - c.weightx = 0.4; - c.anchor = GridBagConstraints.LINE_END; - upPanel.add(proxyAuthUsernameLabel, c); - - c.gridx++; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - upPanel.add(proxyAuthUsernameTB, c); - - // password row - c.gridy++; - c.gridx = 0; - c.weightx = 0.4; - c.anchor = GridBagConstraints.LINE_END; - upPanel.add(proxyAuthPasswordLabel, c); - - c.gridx++; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - upPanel.add(proxyAuthPasswordPB, c); - - c.gridx++; - c.weightx = 0.4; - c.anchor = GridBagConstraints.LINE_START; - upPanel.add(passwordNotStoredLabel, c); - - gbc.gridy++; - proxyPanel.add(upPanel, gbc); + if (!Platform.isJS()) + /** + * java.net.Authenticator is not implemented in SwingJS. Not displaying the + * Authentication options in Preferences. + * + * @j2sIgnore + * + */ + { + // Require authentication checkbox + gbc.gridy++; + proxyPanel.add(proxyAuth, gbc); + + // username and password + JPanel upPanel = new JPanel(); + upPanel.setLayout(new GridBagLayout()); + // username row + c.gridy = 0; + c.gridx = 0; + c.gridwidth = 1; + c.weightx = 0.4; + c.anchor = GridBagConstraints.LINE_END; + upPanel.add(proxyAuthUsernameLabel, c); + + c.gridx++; + c.weightx = 1.0; + c.anchor = GridBagConstraints.LINE_START; + upPanel.add(proxyAuthUsernameTB, c); + + // password row + c.gridy++; + c.gridx = 0; + c.weightx = 0.4; + c.anchor = GridBagConstraints.LINE_END; + upPanel.add(proxyAuthPasswordLabel, c); + + c.gridx++; + c.weightx = 1.0; + c.anchor = GridBagConstraints.LINE_START; + upPanel.add(proxyAuthPasswordPB, c); + + c.gridx++; + c.weightx = 0.4; + c.anchor = GridBagConstraints.LINE_START; + upPanel.add(passwordNotStoredLabel, c); + + gbc.gridy++; + proxyPanel.add(upPanel, gbc); + + } // end j2sIgnore applyProxyButton.setText(MessageManager.getString("action.apply")); applyProxyButton.addActionListener(new ActionListener() -- 1.7.10.2