0c4cb926a6dd1afa425390e9d8c58a62ff30dc07
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / sp / auth / ServiceProxyUser.java
1 package com.indexdata.mkjsf.pazpar2.sp.auth;\r
2 \r
3 import java.util.HashMap;\r
4 import java.util.Map;\r
5 \r
6 import javax.enterprise.context.SessionScoped;\r
7 import javax.inject.Named;\r
8 \r
9 import org.apache.log4j.Logger;\r
10 \r
11 import com.indexdata.mkjsf.utils.Utils;\r
12 \r
13 @Named("user") @SessionScoped\r
14 public class ServiceProxyUser implements AuthenticationEntity {\r
15 \r
16   private static final long serialVersionUID = 2351542518778803071L;\r
17   private Map<String,String> actualProperties = new HashMap<String,String>();\r
18   private static Logger logger = Logger.getLogger(ServiceProxyUser.class);\r
19   private boolean credsAuthenticated = false;\r
20   private boolean ipAuthenticated = false;\r
21   private boolean ipAuthFailure = false;\r
22   private boolean credsAuthFailure = false;  \r
23 \r
24   public ServiceProxyUser()  {\r
25     logger.debug("ServiceProxyUser instantiated: " + Utils.objectId(this));\r
26   }\r
27   \r
28   public String getName() { \r
29     return actualProperties.get("name"); \r
30   }\r
31   \r
32   public void setName(String newValue) { \r
33     actualProperties.put("name", newValue); \r
34   }\r
35   \r
36   public String getPassword() { \r
37     return actualProperties.get("password"); \r
38   }\r
39   \r
40   public void setPassword(String newValue) { \r
41     actualProperties.put("password", newValue);\r
42   }\r
43   \r
44   public void setRealm(String realm) {\r
45     actualProperties.put("realm", realm);\r
46   }\r
47   \r
48   public String getRealm() {\r
49     return actualProperties.get("realm");\r
50   }\r
51    \r
52   public void credentialsAuthenticationSucceeded (boolean success) {\r
53     this.credsAuthFailure = !success;\r
54     this.credsAuthenticated = success;\r
55     this.ipAuthenticated = false;\r
56     this.ipAuthFailure = false;\r
57   }\r
58   \r
59   public void ipAuthenticationSucceeded (boolean success) {\r
60     this.ipAuthFailure = !success;    \r
61     this.ipAuthenticated = success;\r
62     this.credsAuthenticated = false;\r
63     this.credsAuthFailure = false;\r
64   }\r
65   \r
66   public boolean isAuthenticated() {\r
67     return (ipAuthenticated || credsAuthenticated);\r
68   }\r
69   \r
70   public boolean isIpAuthenticated () {\r
71     return ipAuthenticated;\r
72   }\r
73   \r
74   public boolean isCredentialsAuthenticated () {\r
75     return credsAuthenticated;\r
76   }\r
77   \r
78   public boolean hasIpAuthFailure () {\r
79     return ipAuthFailure;\r
80   }\r
81   \r
82   public boolean hasCredsAuthFailure () {\r
83     return credsAuthFailure;\r
84   }\r
85   \r
86   public boolean hasAuthenticationFailure () {\r
87     return credsAuthFailure || ipAuthFailure;\r
88   }\r
89   \r
90   public void authenticationCheckFailed () {\r
91     ipAuthenticated = false;\r
92     credsAuthenticated = false;\r
93   }\r
94   \r
95   public String getAuthenticationStatus () {\r
96     return (isAuthenticated() ? \r
97               (isIpAuthenticated()? "IP authenticated" : \r
98                  (isCredentialsAuthenticated() ? "Authenticated by credentials" : "Unknown authentication method")) :\r
99               (hasAuthenticationFailure() ? \r
100                   (hasIpAuthFailure() ? "Authentication by IP address failed" :\r
101                       (hasCredsAuthFailure() ? "Authentication by credentials failed" : "Unknown authentication failure")) :\r
102                 "Not authenticated"));\r
103   }\r
104   \r
105 \r
106   @Override\r
107   public String getProperty(String key) {\r
108     return actualProperties.get(key);\r
109   }\r
110 \r
111   @Override\r
112   public Map<String, String> getPropertyMap() {\r
113     return actualProperties;\r
114   }\r
115   \r
116   public void clear() {\r
117     actualProperties = new HashMap<String,String>();\r
118     credsAuthenticated = false;\r
119     ipAuthenticated = false;    \r
120   }\r
121   \r
122   public void setSpResponse (String responseXml) {\r
123     \r
124   }\r
125   \r
126 \r
127 }\r