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