JAL-3633 Memory only proxy auth password storage. Open Preferences->Connection tab...
[jalview.git] / src / jalview / bin / Cache.java
index 6648099..8051336 100755 (executable)
@@ -52,13 +52,16 @@ import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
 
 import jalview.datamodel.PDBEntry;
+import jalview.gui.Preferences;
 import jalview.gui.UserDefinedColours;
+import jalview.jbgui.GPreferences;
 import jalview.schemes.ColourSchemeLoader;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.UserColourScheme;
 import jalview.structure.StructureImportSettings;
 import jalview.urls.IdOrgSettings;
 import jalview.util.ColorUtils;
+import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.sifts.SiftsSettings;
 
@@ -292,6 +295,9 @@ public class Cache
 
   public final static String PROXYTYPE_CUSTOM = "true";
 
+  // in-memory only storage of proxy password, safer to use char array
+  public static char[] proxyAuthPassword = null;
+
   /** Jalview Properties */
   public static Properties applicationProperties = new Properties()
   {
@@ -431,7 +437,7 @@ public class Cache
     switch (proxyType)
     {
     case PROXYTYPE_NONE:
-      setProxyProperties(null, null, null, null, null, null, null, null);
+      clearProxyProperties();
       break;
     case PROXYTYPE_SYSTEM: // use system settings
       resetProxyProperties();
@@ -442,14 +448,11 @@ public class Cache
       String httpsHost = getDefault("PROXY_SERVER_HTTPS", httpHost);
       String httpsPort = getDefault("PROXY_PORT_HTTPS", httpPort);
       String httpUser = getDefault("PROXY_AUTH_USER", null);
-      String httpPassword = getDefault("PROXY_AUTH_PASSWORD", null);
       // https.proxyUser and https.proxyPassword are not able to be
-      // independently set in Preferences yet
+      // independently set in Preferences yet (or http.nonProxyHosts)
       String httpsUser = getDefault("PROXY_AUTH_USER_HTTPS", httpUser);
-      String httpsPassword = getDefault("PROXY_AUTH_PASSWORD_HTTPS",
-              httpPassword);
       setProxyProperties(httpHost, httpPort, httpsHost, httpsPort, httpUser,
-              httpPassword, httpsUser, httpsPassword);
+              proxyAuthPassword, httpsUser, proxyAuthPassword, "localhost");
       break;
     default:
       String message = "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
@@ -1268,12 +1271,23 @@ public class Cache
   }
 
   // proxy properties methods
+  public static void clearProxyProperties()
+  {
+    setProxyProperties(null, null, null, null, null, null, null, null,
+            null);
+  }
+
   public static void resetProxyProperties()
   {
     setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
             startupProxyProperties[2], startupProxyProperties[3],
-            startupProxyProperties[4], startupProxyProperties[5],
-            startupProxyProperties[6], startupProxyProperties[7]);
+            startupProxyProperties[4],
+            startupProxyProperties[5] == null ? null
+                    : startupProxyProperties[5].toCharArray(),
+            startupProxyProperties[6],
+            startupProxyProperties[7] == null ? null
+                    : startupProxyProperties[7].toCharArray(),
+            startupProxyProperties[8]);
     StringBuilder sb = new StringBuilder();
     sb.append("Setting proxy properties to: http.proxyHost=")
             .append(startupProxyProperties[0]).append(", http.proxyPort=")
@@ -1294,15 +1308,8 @@ public class Cache
 
   public static void setProxyProperties(String httpHost, String httpPort,
           String httpsHost, String httpsPort, String httpUser,
-          String httpPassword)
-  {
-    setProxyProperties(httpHost, httpPort, httpsHost, httpsPort, httpUser,
-            httpPassword, httpUser, httpPassword);
-  }
-
-  public static void setProxyProperties(String httpHost, String httpPort,
-          String httpsHost, String httpsPort, String httpUser,
-          String httpPassword, String httpsUser, String httpsPassword)
+          char[] httpPassword, String httpsUser, char[] httpsPassword,
+          String nonProxyHosts)
   {
     setOrClearSystemProperty("http.proxyHost", httpHost);
     setOrClearSystemProperty("http.proxyPort", httpPort);
@@ -1312,6 +1319,9 @@ public class Cache
     setOrClearSystemProperty("http.proxyPassword", httpPassword);
     setOrClearSystemProperty("https.proxyUser", httpsUser);
     setOrClearSystemProperty("https.proxyPassword", httpsPassword);
+    // are we using a custom proxy (password prompt might be required)?
+    boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
+            .equals(PROXYTYPE_CUSTOM);
     if (httpUser != null || httpsUser != null)
     {
       try
@@ -1323,29 +1333,51 @@ public class Cache
           {
             if (getRequestorType() == RequestorType.PROXY)
             {
-              try
+              String scheme = getRequestingScheme();
+              boolean needProxyPasswordSet = false;
+              if (customProxySet &&
+              // we have a username but no password for the scheme being
+              // requested
+              (scheme.equalsIgnoreCase("http")
+                      && (httpUser != null && httpUser.length() > 0
+                              && (httpPassword == null
+                                      || httpPassword.length == 0)))
+                      || (scheme.equalsIgnoreCase("https")
+                              && (httpsUser != null
+                                      && httpsUser.length() > 0
+                                      && (httpsPassword == null
+                                              || httpsPassword.length == 0))))
               {
-                if (getRequestingScheme().equalsIgnoreCase("http")
-                        && getRequestingHost().equalsIgnoreCase(httpHost)
-                        && getRequestingPort() == Integer.valueOf(httpPort))
+                // open Preferences -> Connections
+                String message = MessageManager
+                        .getString("label.proxy_password_required");
+                openPreferencesConnectionsForProxyPassword(message);
+              }
+              else
+              {
+                try
                 {
-                  return new PasswordAuthentication(httpUser,
-                          httpPassword == null ? new char[] {}
-                                  : httpPassword.toCharArray());
-                }
-                if (getRequestingScheme().equalsIgnoreCase("https")
-                        && getRequestingHost().equalsIgnoreCase(httpsHost)
-                        && getRequestingPort() == Integer
-                                .valueOf(httpsPort))
+                  if (scheme.equalsIgnoreCase("http")
+                          && getRequestingHost().equalsIgnoreCase(httpHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpPort))
+                  {
+                    return new PasswordAuthentication(httpUser,
+                            httpPassword);
+                  }
+                  if (scheme.equalsIgnoreCase("https")
+                          && getRequestingHost().equalsIgnoreCase(httpsHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpsPort))
+                  {
+                    return new PasswordAuthentication(httpsUser,
+                            httpsPassword);
+                  }
+                } catch (NumberFormatException e)
                 {
-                  return new PasswordAuthentication(httpsUser,
-                          httpsPassword == null ? new char[] {}
-                                  : httpsPassword.toCharArray());
+                  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 + "]");
               }
             }
             // non proxy request
@@ -1365,21 +1397,27 @@ public class Cache
     {
       // reset the Authenticator to protect http.proxyUser and
       // http.proxyPassword Just In Case
-      Authenticator.setDefault(new Authenticator()
-      {
-        @Override
-        protected PasswordAuthentication getPasswordAuthentication()
-        {
-          return null;
-        }
-      });
+      Authenticator.setDefault(null);
     }
 
-    // clear localhost from proxying unless nonProxyHosts already set (not
-    // currently configurable in Preferences)
-    String nonProxyHosts = startupProxyProperties[8];
-    System.setProperty("http.nonProxyHosts",
-            nonProxyHosts == null ? "localhost" : nonProxyHosts);
+    // nonProxyHosts not currently configurable in Preferences
+    setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
+  }
+
+  private static void openPreferencesConnectionsForProxyPassword(
+          String message)
+  {
+    //
+    Cache.info("Opening Preferences for proxy password");
+    // Desktop.instance.preferences_actionPerformed(null);
+    Preferences p = new Preferences(GPreferences.CONNECTIONS_TAB);
+    p.grabFocus();
+  }
+
+  public static void setOrClearSystemProperty(String key, char[] value)
+  {
+    setOrClearSystemProperty(key,
+            (value == null) ? null : value.toString());
   }
 
   public static void setOrClearSystemProperty(String key, String value)