From 8ff427b73c71fa994f74edd35b106185cf94d22a Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Tue, 30 Apr 2013 21:13:14 -0400 Subject: [PATCH] Adds methods for displaying authentication status --- .../com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java | 4 ++ .../mkjsf/pazpar2/sp/ServiceProxyClient.java | 13 ++-- .../pazpar2/sp/auth/AuthenticationEntity.java | 3 - .../mkjsf/pazpar2/sp/auth/ServiceProxyUser.java | 67 +++++++++++++++----- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java index 09e500a..f5c0ad0 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java @@ -94,6 +94,10 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { return ((ServiceProxyClient)searchClient).getServiceProxyUrl(); } + public boolean getServiceProxyUrlIsDefined() { + return ((ServiceProxyClient)searchClient).getServiceProxyUrl().length()>0; + } + public List getServiceProxyUrls() { List urls = new ArrayList(); urls.add(""); 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 fb3c867..92e46a7 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java @@ -109,10 +109,10 @@ public class ServiceProxyClient implements SearchClient { String responseStr = new String(response,"UTF-8"); logger.info(responseStr); if (responseStr.contains("FAIL")) { - user.isAuthenticated(false); + user.credentialsAuthenticationSucceeded(false); return false; } else { - user.isAuthenticated(true); + user.credentialsAuthenticationSucceeded(true); return true; } } catch (ClientProtocolException e) { @@ -132,10 +132,9 @@ public class ServiceProxyClient implements SearchClient { logger.info(new String(response,"UTF-8")); String responseStr = new String(response,"UTF-8"); if (responseStr.contains("FAIL")) { - user.isAuthenticated(false); + user.authenticationCheckFailed(); return false; - } else { - user.isAuthenticated(true); + } else { return true; } } catch (ClientProtocolException e) { @@ -155,10 +154,10 @@ public class ServiceProxyClient implements SearchClient { logger.info(new String(response,"UTF-8")); String responseStr = new String(response,"UTF-8"); if (responseStr.contains("FAIL")) { - user.isAuthenticated(false); + user.ipAuthenticationSucceeded(false); return false; } else { - user.isAuthenticated(true); + user.ipAuthenticationSucceeded(true); return true; } } catch (ClientProtocolException e) { 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 index 9f8b0cb..8d05fd6 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/AuthenticationEntity.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/AuthenticationEntity.java @@ -1,7 +1,6 @@ package com.indexdata.mkjsf.pazpar2.sp.auth; import java.io.Serializable; -import java.util.List; import java.util.Map; public interface AuthenticationEntity extends Serializable{ @@ -10,7 +9,5 @@ public interface AuthenticationEntity extends Serializable{ public String getProperty(String key); public Map getPropertyMap(); - - public List getPossibleProperties(); } 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 index 43971df..0c4cb92 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java @@ -1,8 +1,6 @@ package com.indexdata.mkjsf.pazpar2.sp.auth; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import javax.enterprise.context.SessionScoped; @@ -16,11 +14,12 @@ import com.indexdata.mkjsf.utils.Utils; public class ServiceProxyUser implements AuthenticationEntity { private static final long serialVersionUID = 2351542518778803071L; - private List possibleProperties = Arrays.asList("name","password","realm"); private Map actualProperties = new HashMap(); private static Logger logger = Logger.getLogger(ServiceProxyUser.class); - private boolean authenticated = false; + 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)); @@ -50,22 +49,59 @@ public class ServiceProxyUser implements AuthenticationEntity { return actualProperties.get("realm"); } - public void isAuthenticated(boolean authenticated) { - this.authenticated = authenticated; + public void credentialsAuthenticationSucceeded (boolean success) { + this.credsAuthFailure = !success; + this.credsAuthenticated = success; + this.ipAuthenticated = false; + this.ipAuthFailure = false; } - public void isIpAuthenticated (boolean authenticated) { - this.ipAuthenticated = authenticated; + public void ipAuthenticationSucceeded (boolean success) { + this.ipAuthFailure = !success; + this.ipAuthenticated = success; + this.credsAuthenticated = false; + this.credsAuthFailure = false; } public boolean isAuthenticated() { - return authenticated; + 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) { @@ -76,16 +112,15 @@ public class ServiceProxyUser implements AuthenticationEntity { public Map getPropertyMap() { return actualProperties; } - - @Override - public List getPossibleProperties() { - return possibleProperties; - } public void clear() { actualProperties = new HashMap(); - authenticated = false; - ipAuthenticated = false; + credsAuthenticated = false; + ipAuthenticated = false; + } + + public void setSpResponse (String responseXml) { + } -- 1.7.10.4