Cleans up auth logic
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 21 May 2013 16:55:03 +0000 (12:55 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 21 May 2013 16:55:03 +0000 (12:55 -0400)
  Removes special classes (ie ServiceProxyUser) and associated
  method calls for auth logic, basically treating the auth commands
  more like any other commands.

src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java
src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/AuthenticationEntity.java [deleted file]
src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java [deleted file]

index 1a1b1f9..1c825a8 100644 (file)
@@ -30,7 +30,6 @@ import com.indexdata.mkjsf.pazpar2.data.RecordResponse;
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
 import com.indexdata.mkjsf.pazpar2.data.Responses;\r
-import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.pazpar2.state.StateListener;\r
 import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
 import com.indexdata.mkjsf.utils.Utils;\r
@@ -60,8 +59,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   @Inject StateManager stateMgr;\r
   @Inject Pazpar2Commands pzreq;\r
   @Inject Responses pzresp;\r
-  @Inject ErrorCentral errors;\r
-  @Inject ServiceProxyUser user;\r
+  @Inject ErrorCentral errors;  \r
   \r
   protected ResultsPager pager = null; \r
   \r
@@ -402,8 +400,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     if (url!=null && searchClient != null && !url.equals(searchClient.getServiceUrl())) {\r
       pzreq.getRecord().removeParametersInState();\r
       pzreq.getSearch().removeParametersInState();\r
-      pzresp.getSp().resetAuthAndBeyond(true);\r
-      user.clear();\r
+      pzresp.getSp().resetAuthAndBeyond(true);      \r
       searchClient.setServiceUrl(url);\r
     }    \r
   }\r
index ff69dcc..da21541 100644 (file)
@@ -21,7 +21,6 @@ import com.indexdata.mkjsf.pazpar2.data.ResponseParser;
 import com.indexdata.mkjsf.pazpar2.data.Responses;\r
 import com.indexdata.mkjsf.pazpar2.data.sp.AuthResponse;\r
 import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse;\r
-import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
 @Named("pz2x") @SessionScoped\r
@@ -34,7 +33,6 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
   private InitDocUpload initDocUpload; \r
     \r
   @Inject ConfigurationReader configurator;  \r
-  @Inject ServiceProxyUser user;    \r
   @Inject Pz2Bean pz2;\r
   @Inject Pazpar2Commands pzreq;\r
   @Inject Responses pzresp;\r
@@ -47,28 +45,23 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
   }\r
    \r
 \r
+  \r
   public void login(String un, String pw) {\r
-    if (user.isAuthenticated() && user.getName().equals(un) /* && pz2.spClient.checkAuthentication(user) */) {\r
-      logger.info("Repeat request from UI to authenticate user. Auth verified for given user name so skipping log-in.");\r
-    } else {\r
       logger.info("doing un/pw login");\r
-      user.setName(un);\r
-      user.setPassword(pw);\r
+      pzreq.getSp().getAuth().setUsername(un);\r
+      pzreq.getSp().getAuth().setPassword(pw);\r
       login("");\r
-    }\r
   }\r
 \r
 \r
   \r
   @Override  \r
   public String login(String navigateTo) {\r
-    logger.info("doing login by " + user + " using " + pz2 + " and client " + pz2.getSpClient());\r
+    logger.info("doing login using " + pz2 + " and client " + pz2.getSpClient());\r
     pz2.resetSearchAndRecordCommands();\r
     pzresp.getSp().resetAuthAndBeyond(true);\r
     AuthCommand auth = pzreq.getSp().getAuth(); \r
-    auth.setParametersInState(new CommandParameter("action","=","login"),\r
-                              new CommandParameter("username","=",user.getName()),\r
-                              new CommandParameter("password","=",user.getPassword()));\r
+    auth.setParameterInState(new CommandParameter("action","=","login"));\r
     ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);\r
     String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth");\r
     commandResponse.setResponseToParse(renamedResponse);\r
@@ -78,39 +71,24 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
     }\r
     String responseStr = commandResponse.getResponseString();\r
     logger.info(responseStr);      \r
-    if (responseStr.contains("FAIL")) {\r
-      user.credentialsAuthenticationSucceeded(false);    \r
-    } else {\r
-      user.credentialsAuthenticationSucceeded(true);    \r
-    }      \r
     return navigateTo;\r
   }\r
   \r
   \r
-  public void ipAuthenticate (ServiceProxyUser user) {\r
-    if (!user.isIpAuthenticated()) {\r
-      if (user.isAuthenticated()) {\r
-        user.clear();\r
-      }\r
-      pz2.resetSearchAndRecordCommands();\r
-      pzresp.getSp().resetAuthAndBeyond(true);\r
-      AuthCommand auth = pzreq.getSp().getAuth(); \r
-      auth.setParameterInState(new CommandParameter("action","=","ipAuth"));\r
-      ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);      \r
-      String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth");\r
-      commandResponse.setResponseToParse(renamedResponse);\r
-      ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(commandResponse);\r
-      if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
-        pzresp.put(auth.getCommandName(), responseObject);\r
-      }\r
-      String responseStr = commandResponse.getResponseString();\r
-      logger.info(responseStr);      \r
-      if (responseStr.contains("FAIL")) {\r
-        user.credentialsAuthenticationSucceeded(false);    \r
-      } else {\r
-        user.credentialsAuthenticationSucceeded(true);    \r
-      }      \r
+  public void ipAuthenticate () {  \r
+    pz2.resetSearchAndRecordCommands();\r
+    pzresp.getSp().resetAuthAndBeyond(true);\r
+    AuthCommand auth = pzreq.getSp().getAuth(); \r
+    auth.setParameterInState(new CommandParameter("action","=","ipAuth"));\r
+    ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);      \r
+    String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth");\r
+    commandResponse.setResponseToParse(renamedResponse);\r
+    ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(commandResponse);\r
+    if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+      pzresp.put(auth.getCommandName(), responseObject);\r
     }\r
+    String responseStr = commandResponse.getResponseString();\r
+    logger.info(responseStr);      \r
   }\r
   \r
   private String renameResponseElement(String responseString, String newName) {\r
index eeddd9c..67ecb54 100644 (file)
@@ -1,5 +1,6 @@
 package com.indexdata.mkjsf.pazpar2.commands.sp;\r
 \r
+import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
 import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
 \r
@@ -11,6 +12,30 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand {
     super("auth", stateMgr);\r
   }\r
   \r
+  public void setAction (String action) {\r
+    setParameterInState(new CommandParameter("action","=",action));\r
+  }  \r
+  \r
+  public String getAction () {\r
+    return getParameterValue("action");\r
+  }\r
+  \r
+  public void setUsername(String username) {\r
+    setParameterInState(new CommandParameter("username","=",username));\r
+  }\r
+  \r
+  public String getUsername () {\r
+    return getParameterValue("username");\r
+  }\r
+  \r
+  public void setPassword (String password) {\r
+    setParameterInState(new CommandParameter("password","=",password));\r
+  }\r
+  \r
+  public String getPassword () {\r
+    return getParameterValue("password");\r
+  }\r
+    \r
   public AuthCommand copy () {\r
     AuthCommand newCommand = new AuthCommand(stateMgr);\r
     for (String parameterName : parameters.keySet()) {\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/AuthenticationEntity.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/AuthenticationEntity.java
deleted file mode 100644 (file)
index 8d05fd6..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.indexdata.mkjsf.pazpar2.sp.auth;\r
-\r
-import java.io.Serializable;\r
-import java.util.Map;\r
-\r
-public interface AuthenticationEntity extends Serializable{\r
-  \r
-  \r
-  public String getProperty(String key); \r
-  \r
-  public Map<String,String> getPropertyMap();\r
-  \r
-}\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java
deleted file mode 100644 (file)
index 0c4cb92..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-package com.indexdata.mkjsf.pazpar2.sp.auth;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.inject.Named;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.mkjsf.utils.Utils;\r
-\r
-@Named("user") @SessionScoped\r
-public class ServiceProxyUser implements AuthenticationEntity {\r
-\r
-  private static final long serialVersionUID = 2351542518778803071L;\r
-  private Map<String,String> actualProperties = new HashMap<String,String>();\r
-  private static Logger logger = Logger.getLogger(ServiceProxyUser.class);\r
-  private boolean credsAuthenticated = false;\r
-  private boolean ipAuthenticated = false;\r
-  private boolean ipAuthFailure = false;\r
-  private boolean credsAuthFailure = false;  \r
-\r
-  public ServiceProxyUser()  {\r
-    logger.debug("ServiceProxyUser instantiated: " + Utils.objectId(this));\r
-  }\r
-  \r
-  public String getName() { \r
-    return actualProperties.get("name"); \r
-  }\r
-  \r
-  public void setName(String newValue) { \r
-    actualProperties.put("name", newValue); \r
-  }\r
-  \r
-  public String getPassword() { \r
-    return actualProperties.get("password"); \r
-  }\r
-  \r
-  public void setPassword(String newValue) { \r
-    actualProperties.put("password", newValue);\r
-  }\r
-  \r
-  public void setRealm(String realm) {\r
-    actualProperties.put("realm", realm);\r
-  }\r
-  \r
-  public String getRealm() {\r
-    return actualProperties.get("realm");\r
-  }\r
-   \r
-  public void credentialsAuthenticationSucceeded (boolean success) {\r
-    this.credsAuthFailure = !success;\r
-    this.credsAuthenticated = success;\r
-    this.ipAuthenticated = false;\r
-    this.ipAuthFailure = false;\r
-  }\r
-  \r
-  public void ipAuthenticationSucceeded (boolean success) {\r
-    this.ipAuthFailure = !success;    \r
-    this.ipAuthenticated = success;\r
-    this.credsAuthenticated = false;\r
-    this.credsAuthFailure = false;\r
-  }\r
-  \r
-  public boolean isAuthenticated() {\r
-    return (ipAuthenticated || credsAuthenticated);\r
-  }\r
-  \r
-  public boolean isIpAuthenticated () {\r
-    return ipAuthenticated;\r
-  }\r
-  \r
-  public boolean isCredentialsAuthenticated () {\r
-    return credsAuthenticated;\r
-  }\r
-  \r
-  public boolean hasIpAuthFailure () {\r
-    return ipAuthFailure;\r
-  }\r
-  \r
-  public boolean hasCredsAuthFailure () {\r
-    return credsAuthFailure;\r
-  }\r
-  \r
-  public boolean hasAuthenticationFailure () {\r
-    return credsAuthFailure || ipAuthFailure;\r
-  }\r
-  \r
-  public void authenticationCheckFailed () {\r
-    ipAuthenticated = false;\r
-    credsAuthenticated = false;\r
-  }\r
-  \r
-  public String getAuthenticationStatus () {\r
-    return (isAuthenticated() ? \r
-              (isIpAuthenticated()? "IP authenticated" : \r
-                 (isCredentialsAuthenticated() ? "Authenticated by credentials" : "Unknown authentication method")) :\r
-              (hasAuthenticationFailure() ? \r
-                  (hasIpAuthFailure() ? "Authentication by IP address failed" :\r
-                      (hasCredsAuthFailure() ? "Authentication by credentials failed" : "Unknown authentication failure")) :\r
-                "Not authenticated"));\r
-  }\r
-  \r
-\r
-  @Override\r
-  public String getProperty(String key) {\r
-    return actualProperties.get(key);\r
-  }\r
-\r
-  @Override\r
-  public Map<String, String> getPropertyMap() {\r
-    return actualProperties;\r
-  }\r
-  \r
-  public void clear() {\r
-    actualProperties = new HashMap<String,String>();\r
-    credsAuthenticated = false;\r
-    ipAuthenticated = false;    \r
-  }\r
-  \r
-  public void setSpResponse (String responseXml) {\r
-    \r
-  }\r
-  \r
-\r
-}\r