1 19 20 package com.sslexplorer.security; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Calendar ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import com.sslexplorer.policyframework.AbstractResource; 36 import com.sslexplorer.policyframework.PolicyConstants; 37 38 44 public class DefaultAuthenticationScheme extends AbstractResource implements AuthenticationScheme, Serializable { 45 46 final static Log log = LogFactory.getLog(DefaultAuthenticationScheme.class); 47 48 50 private List <String > modules; 51 private HttpSession servletSession; 52 private int current; 53 private List <AuthenticationModule> authenticationModules; 54 private User user; 55 private List <Credentials> allCredentials; 56 private AccountLock lock; 57 private boolean enabled; 58 private int priority; 59 60 71 public DefaultAuthenticationScheme(int realmID, int resourceId, String resourceName, String resourceDescription, Calendar dateAmended, Calendar dateCreated, boolean enabled, int priority) { 72 super(realmID, PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, resourceId, resourceName, resourceDescription, dateAmended, dateCreated); 73 current = -1; 74 modules = new ArrayList <String >(); 75 allCredentials = new ArrayList <Credentials>(); 76 this.enabled = enabled; 77 this.priority = priority; 78 } 79 80 83 public void addModule(String module) { 84 if(!modules.contains(module)) { 85 modules.add(module); 86 } 87 } 88 89 92 public boolean hasModule(String name) { 93 return modules.contains(name); 94 } 95 96 99 public void removeModule(String module) { 100 modules.remove(module); 101 } 102 103 106 public Iterator <String > modules() { 107 return modules.iterator(); 108 } 109 110 113 public void init(HttpSession servletSession) throws Exception { 114 this.servletSession = servletSession; 115 authenticationModules = new ArrayList <AuthenticationModule>(); 116 for(Iterator <String > i = modules.iterator(); i.hasNext(); ) { 117 String moduleId = i.next(); 118 AuthenticationModule module = AuthenticationModuleManager.getInstance().createModule(moduleId); 119 authenticationModules.add(module); 120 module.init(this); 121 } 122 } 123 124 127 public int getCurrentModuleIndex() { 128 return current; 129 } 130 131 134 public User getUser() { 135 return user; 136 } 137 138 141 public void setUser(User user) { 142 this.user = user; 143 } 144 145 148 public HttpSession getServletSession() { 149 return servletSession; 150 } 151 152 155 public AuthenticationModule nextAuthenticationModule() { 156 if( ( current + 1 ) < authenticationModules.size()) { 157 AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(++current); 158 return mod; 159 } 160 return null; 161 } 162 163 166 public AuthenticationModule currentAuthenticationModule() { 167 if(current != -1) { 168 AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(current); 169 return mod; 170 } 171 return null; 172 } 173 174 177 public void authenticationComplete(HttpServletRequest request, HttpServletResponse response) throws Exception { 178 for(Iterator i = authenticationModules.iterator();i.hasNext(); ) { 179 AuthenticationModule mod = (AuthenticationModule)i.next(); 180 if (log.isDebugEnabled()) 181 log.debug("Informing module " + mod.getName() + " that authentication is complete"); 182 mod.authenticationComplete(); 183 184 if(request.getSession().getAttribute(Constants.SESSION_LOCKED) != null) { 186 break; 187 } 188 } 189 LogonControllerFactory.getInstance().logon(request, response, this); 190 191 } 192 193 196 public String getUsername() { 197 return user.getPrincipalName(); 198 } 199 200 201 204 public void addCredentials(Credentials credentials) { 205 allCredentials.add(credentials); 206 } 207 208 211 public Iterator credentials() { 212 return allCredentials.iterator(); 213 } 214 215 218 public void setAccountLock(AccountLock lock) { 219 this.lock = lock; 220 } 221 222 225 public AccountLock getAccountLock() { 226 return lock; 227 } 228 229 232 public void moveUp(String module) { 233 int idx = modules.indexOf(module); 234 if(idx > 0) { 235 String swap = modules.get(idx - 1); 236 modules.remove(idx - 1); 237 modules.add(idx, swap); 238 } 239 } 240 241 244 public void clearModules() { 245 modules.clear(); 246 247 } 248 249 252 public void moveDown(String module) { 253 int idx = modules.indexOf(module); 254 if( ( idx + 1 ) < modules.size() ) { 255 String swap = modules.get(idx + 1); 256 modules.remove(idx + 1); 257 modules.add(idx, swap); 258 } 259 } 260 261 264 public int getModuleCount() { 265 return modules.size(); 266 } 267 268 271 public String getModule(int index) { 272 return modules.get(index); 273 } 274 275 278 public boolean getSessionLocked() { 279 return getServletSession() != null ? ( getServletSession().getAttribute(Constants.SESSION_LOCKED) != null ) : false; 280 } 281 282 285 public String getSchemeName() { 286 return getResourceName(); 287 } 288 289 292 public boolean getEnabled() { 293 return enabled; 294 } 295 296 299 public void setEnabled(boolean enabled) { 300 this.enabled = enabled; 301 } 302 303 306 public boolean isSystemScheme() { 307 if(modules.size() == 0) { 308 return false; 309 } 310 for(Iterator i = modules(); i.hasNext(); ) { 311 String mod = (String )i.next(); 312 if(!AuthenticationModuleManager.getInstance().getModuleDefinition(mod).getSystem()) { 313 return false; 314 } 315 } 316 return true; 317 } 318 319 322 public String [] getModules(){ 323 return modules.toArray(new String [modules.size()]); 324 } 325 326 329 public String getPriority() { 330 return String.valueOf(priority); 331 } 332 333 336 public int getPriorityInt(){ 337 return priority; 338 } 339 340 343 public void setPriorityInt(int priority){ 344 this.priority = priority; 345 } 346 } 347 | Popular Tags |