From e48d11f9f00272af3b0637ad4e3e0d5fd1e38374 Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Sun, 21 Apr 2013 09:18:30 -0400 Subject: [PATCH] Adds IP authentication to SP client --- .../com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java | 10 ++- .../mkjsf/pazpar2/sp/ServiceProxyClient.java | 66 ++++++++++++-------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java index fce82ca..4209aa2 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java @@ -48,7 +48,7 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { } public void login(String un, String pw) { - if (user.isAuthenticated() && user.getName().equals(un) && ((ServiceProxyClient) searchClient).checkAuthentication()) { + if (user.isAuthenticated() && user.getName().equals(un) && ((ServiceProxyClient) searchClient).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"); @@ -67,6 +67,12 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { pzresp.reset(); return navigateTo; } + + public void ipAuthenticate (ServiceProxyUser user) { + if (!user.isAuthenticated()) { + ((ServiceProxyClient)searchClient).ipAuthenticate(user); + } + } @Override public void setServiceProxyUrl(String url) { @@ -129,6 +135,4 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { return null; // return getCommandParameterValue("record","acefilter",""); } - - } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java index 244496a..520ca3e 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java @@ -12,6 +12,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.enterprise.context.SessionScoped; +import javax.inject.Inject; + import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; @@ -40,11 +43,9 @@ import com.indexdata.mkjsf.pazpar2.CommandResponse; import com.indexdata.mkjsf.pazpar2.SearchClient; import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; -import com.indexdata.mkjsf.pazpar2.sp.auth.AuthenticationEntity; import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.mkjsf.utils.Utils; - public class ServiceProxyClient implements SearchClient { private static final long serialVersionUID = -4031644009579840277L; @@ -57,16 +58,15 @@ public class ServiceProxyClient implements SearchClient { private Configuration config = null; ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler(); - private HttpClient client; - private ServiceProxyUser user; + private transient HttpClient client; private Pazpar2Command checkAuth = null; - + private Pazpar2Command ipAuth = null; public ServiceProxyClient () { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); - client = new DefaultHttpClient(cm); + client = new DefaultHttpClient(cm); } @Override @@ -78,6 +78,8 @@ public class ServiceProxyClient implements SearchClient { this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); checkAuth = new Pazpar2Command("auth",null); checkAuth.setParameterInState(new CommandParameter("action","=","check")); + ipAuth = new Pazpar2Command("auth",null); + ipAuth.setParameterInState(new CommandParameter("action","=","ipauth")); } catch (ConfigurationException c) { c.printStackTrace(); } catch (MissingMandatoryParameterException mmp) { @@ -93,10 +95,9 @@ public class ServiceProxyClient implements SearchClient { } } - public boolean authenticate (AuthenticationEntity user) { + public boolean authenticate (ServiceProxyUser user) { try { - logger.info("Authenticating [" + user.getProperty("name") + "]"); - this.user = (ServiceProxyUser) user; + logger.info("Authenticating [" + user.getProperty("name") + "]"); Pazpar2Command auth = new Pazpar2Command("auth",null); auth.setParametersInState(new CommandParameter("action","=","login"), new CommandParameter("username","=",user.getProperty("name")), @@ -105,10 +106,10 @@ public class ServiceProxyClient implements SearchClient { String responseStr = new String(response,"UTF-8"); logger.info(responseStr); if (responseStr.contains("FAIL")) { - this.user.isAuthenticated(false); + user.isAuthenticated(false); return false; } else { - this.user.isAuthenticated(true); + user.isAuthenticated(true); return true; } } catch (ClientProtocolException e) { @@ -122,16 +123,39 @@ public class ServiceProxyClient implements SearchClient { } } - public boolean checkAuthentication () { + public boolean checkAuthentication (ServiceProxyUser user) { try { byte[] response = send(checkAuth); logger.info(new String(response,"UTF-8")); String responseStr = new String(response,"UTF-8"); + if (responseStr.contains("FAIL")) { + user.isAuthenticated(false); + return false; + } else { + user.isAuthenticated(true); + return true; + } + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + } + + public boolean ipAuthenticate (ServiceProxyUser user) { + try { + byte[] response = send(ipAuth); + logger.info(new String(response,"UTF-8")); + String responseStr = new String(response,"UTF-8"); if (responseStr.contains("FAIL")) { - this.user.isAuthenticated(false); + user.isAuthenticated(false); return false; } else { - this.user.isAuthenticated(true); + user.isAuthenticated(true); return true; } } catch (ClientProtocolException e) { @@ -143,15 +167,16 @@ public class ServiceProxyClient implements SearchClient { e.printStackTrace(); return false; } + } public boolean isAuthenticatingClient () { return true; } - public boolean isAuthenticated () { + public boolean isAuthenticated (ServiceProxyUser user) { if (user.getProperty("name") != null && user.getProperty("password") != null) { - return checkAuthentication(); + return checkAuthentication(user); } else { return false; } @@ -172,15 +197,6 @@ public class ServiceProxyClient implements SearchClient { return response; } - private byte[] send (String queryString) throws ClientProtocolException, IOException { - String url = serviceUrl + "?" + queryString; - logger.info("Sending request "+url); - HttpGet httpget = new HttpGet(url); - byte[] response = client.execute(httpget, handler); - return response; - - } - public class ProxyPz2ResponseHandler implements ResponseHandler { private StatusLine statusLine = null; public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { -- 1.7.10.4