| 1 28 package net.sf.jguard.ext.authentication.loginmodules; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 import javax.security.auth.Subject ; 38 import javax.security.auth.callback.CallbackHandler ; 39 import javax.security.auth.login.FailedLoginException ; 40 import javax.security.auth.login.LoginException ; 41 import javax.security.auth.spi.LoginModule ; 42 43 import net.sf.jguard.core.CoreConstants; 44 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 45 import net.sf.jguard.ext.authentication.AuthenticationException; 46 import net.sf.jguard.ext.authentication.manager.AuthenticationManagerFactory; 47 import net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager; 48 49 50 66 public class XmlLoginModule extends UserLoginModule implements LoginModule { 67 68 69 private static final String LOGIN_ERROR = "login.error"; 70 private static final String PASSWORD = "password"; 71 private static final String LOGIN = "login"; 72 73 74 private static final Logger logger = Logger.getLogger(XmlLoginModule.class.getName()); 75 76 private Set users; 77 private Set globalPrincipals; 78 private Set globalPrivateCredentials; 79 private Set globalPublicCredentials; 80 81 82 83 90 public void initialize(Subject subj,CallbackHandler cbkHandler,Map sState,Map opts) { 91 super.initialize(subj,cbkHandler,sState,opts); 92 93 if (AuthenticationManagerFactory.getAuthenticationManager() == null){ 94 95 Map newOpts = new HashMap (); 96 newOpts.putAll(opts); 97 98 if (opts.get(CoreConstants.APPLICATION_NAME) != null){ 99 newOpts.put(CoreConstants.APPLICATION_NAME, opts.get(CoreConstants.APPLICATION_NAME)); 101 102 }else{ 103 String appNameProp = System.getProperty("net.sf.jguard.application.name"); 104 105 if (appNameProp != null){ 106 newOpts.put(CoreConstants.APPLICATION_NAME, appNameProp); 108 109 }else{ 110 String appNameJMXProp = System.getProperty("com.sun.management.jmxremote.login.config"); 111 112 if (appNameJMXProp != null){ 113 logger.warning("Using JMX config for application name! " + 114 "If you're not running JMX, prefer XmlLoginModule options or net.sf.jguard.applicationName vmarg"); 115 newOpts.put(CoreConstants.APPLICATION_NAME, appNameJMXProp); 116 117 }else{ 118 newOpts.put(CoreConstants.APPLICATION_NAME, CoreConstants.DEFAULT_APPLICATION_NAME); 120 } 121 } 122 } 123 124 try { 125 AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(), newOpts); 126 } catch (AuthenticationException e) { 127 logger.log(Level.SEVERE, " initialize ", e); 128 } 129 } 130 try { 131 users = AuthenticationManagerFactory.getAuthenticationManager().getUsers(); 132 } catch (AuthenticationException e) { 133 logger.log(Level.SEVERE, " initialize ", e); 134 } 135 } 136 137 143 public boolean login() throws LoginException { 144 super.login(); 145 146 JGuardCredential loginCredential = new JGuardCredential(); 147 loginCredential.setId(XmlLoginModule.LOGIN); 148 loginCredential.setValue(login); 149 150 JGuardCredential passwordCredential = new JGuardCredential(); 151 passwordCredential.setId(XmlLoginModule.PASSWORD); 152 passwordCredential.setValue(new String (password)); 153 154 Subject user; 155 Iterator it = users.iterator(); 156 boolean authenticationSucceed = false; 157 158 while(it.hasNext()){ 159 user = (Subject )it.next(); 160 Set privateCredentialsTemp = user.getPrivateCredentials(); 161 if(privateCredentialsTemp.contains(loginCredential)){ 162 if((password!=null && privateCredentialsTemp.contains(passwordCredential)) 163 ||skipPasswordCheck){ 164 165 globalPrincipals = user.getPrincipals(); 167 globalPrivateCredentials = user.getPrivateCredentials(); 168 globalPublicCredentials = user.getPublicCredentials(); 169 authenticationSucceed = true; 170 } 171 break; 172 } 173 } 174 175 if(authenticationSucceed==false){ 176 loginOK = false; 177 throw new FailedLoginException (XmlLoginModule.LOGIN_ERROR); 178 } 179 180 return true; 181 } 182 183 184 188 public boolean commit() throws LoginException { 189 if(!loginOK){ 190 return false; 191 } 192 Set principals = subject.getPrincipals(); 193 if(globalPrincipals!=null){ 194 principals.addAll(globalPrincipals); 195 } 196 Set privCredentials = subject.getPrivateCredentials(); 197 if(globalPrivateCredentials!=null){ 198 privCredentials.addAll(globalPrivateCredentials); 199 } 200 Set pubCredentials = subject.getPublicCredentials(); 201 if(globalPublicCredentials!= null){ 202 pubCredentials.addAll(globalPublicCredentials); 203 } 204 return true; 205 } 206 207 208 } 209 | Popular Tags |