1 25 26 package org.objectweb.jonas.security.auth.spi; 27 28 import java.util.ArrayList ; 29 import java.util.Map ; 30 31 import javax.naming.InitialContext ; 32 import javax.security.auth.Subject ; 33 import javax.security.auth.callback.Callback ; 34 import javax.security.auth.callback.CallbackHandler ; 35 import javax.security.auth.callback.NameCallback ; 36 import javax.security.auth.callback.PasswordCallback ; 37 import javax.security.auth.login.LoginException ; 38 import javax.security.auth.spi.LoginModule ; 39 40 import org.objectweb.jonas.security.auth.JGroup; 41 import org.objectweb.jonas.security.auth.JPrincipal; 42 import org.objectweb.jonas.security.auth.JRole; 43 import org.objectweb.jonas.security.auth.callback.CertificateCallback; 44 import org.objectweb.jonas.security.realm.factory.JResource; 45 import org.objectweb.jonas.security.realm.factory.JResourceException; 46 import org.objectweb.jonas.security.realm.principals.User; 47 48 55 public class JDirectResourceLoginModule implements LoginModule { 56 57 60 private Subject subject = null; 61 62 65 private CallbackHandler callbackHandler = null; 66 67 70 private Map options = null; 71 72 75 private String principalName = null; 76 77 80 private String password = null; 81 82 85 private ArrayList principalRoles = null; 86 87 90 private boolean loginWasDoneWithSuccess = false;; 91 92 105 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 106 this.subject = subject; 107 this.callbackHandler = callbackHandler; 108 this.options = options; 109 } 110 111 121 public boolean login() throws LoginException { 122 123 if (callbackHandler == null) { 125 throw new LoginException ("No handler has been defined."); 126 } 127 128 String resourceName = (String ) options.get("resourceName"); 130 131 String certCallback = (String ) options.get("certCallback"); 133 134 if (resourceName == null) { 136 throw new LoginException ( 137 "You have to give an argument to this login module. The 'resourceName' parameter is required."); 138 } 139 140 try { 142 InitialContext ictx = new InitialContext (); 143 JResource jResource = null; 144 try { 145 jResource = (JResource) ictx.lookup(resourceName); 146 } catch (Exception e) { 147 throw createChainedLoginException("Cannot retrieve the resource '" + resourceName 148 + "'. Check that this resource is bound in the registry", e); 149 } 150 151 NameCallback nameCallback = new NameCallback ("User :"); 153 PasswordCallback passwordCallback = new PasswordCallback ("Password :", false); 155 CertificateCallback certificateCallback = new CertificateCallback(); 156 Callback [] callbacks = null; 157 158 if ((certCallback != null) && (Boolean.valueOf(certCallback).booleanValue())) { 159 callbacks = new Callback [] {nameCallback, passwordCallback, certificateCallback}; 160 } else { 161 callbacks = new Callback [] {nameCallback, passwordCallback}; 162 } 163 callbackHandler.handle(callbacks); 164 165 principalName = nameCallback.getName(); 167 if (principalName == null) { 168 throw new LoginException ("A null username is not a valid username"); 169 } 170 if (principalName.startsWith("##DN##") && (certificateCallback.getUserCertificate() == null)) { 171 throw new LoginException ("Name must have a certificate to access this certificate based access login"); 172 } 173 char[] arrayPass = passwordCallback.getPassword(); 174 if (arrayPass == null) { 175 throw new LoginException ("A null password is not a valid password"); 176 } 177 178 User user = null; 180 try { 181 user = jResource.findUser(principalName); 182 } catch (Exception jre) { 183 throw createChainedLoginException("Can not find the user", jre); 185 } 186 if (user == null) { 188 throw new LoginException ("User '" + principalName + "' not found."); 189 } 190 191 boolean validated = jResource.isValidUser(user, new String (arrayPass)); 193 if (!validated) { 194 throw new LoginException ("The password for the user '" + principalName + "' is not valid"); 195 } 196 197 if (user.getPassword() != null) { 199 this.password = user.getPassword(); 200 } else { 201 this.password = new String (arrayPass); 202 } 203 204 try { 206 principalRoles = jResource.getArrayListCombinedRoles(user); 207 } catch (JResourceException jre) { 208 throw createChainedLoginException(jre.getMessage(), jre); 209 } 210 211 } catch (Exception e) { 212 throw createChainedLoginException("Error during the login phase : " + e.getMessage(), e); 213 } 214 loginWasDoneWithSuccess = true; 215 return true; 216 } 217 218 224 private static LoginException createChainedLoginException(String msg, Exception e) { 225 LoginException le = new LoginException (msg); 226 le.initCause(e); 227 return le; 228 } 229 230 244 public boolean commit() throws LoginException { 245 246 if (loginWasDoneWithSuccess) { 248 subject.getPrincipals().add(new JPrincipal(principalName)); 250 subject.getPrivateCredentials().add(password); 251 JGroup group = new JGroup("Roles"); 252 253 String [] roles = new String [principalRoles.size()]; 255 roles = (String []) principalRoles.toArray(roles); 256 int size = principalRoles.size(); 257 for (int i = 0; i < size; i++) { 258 group.addMember(new JRole(roles[i])); 259 } 260 261 subject.getPrincipals().add(group); 263 } 264 return loginWasDoneWithSuccess; 265 } 266 267 278 public boolean abort() throws LoginException { 279 if (loginWasDoneWithSuccess) { 281 principalName = null; 283 principalRoles = null; 284 } 285 return loginWasDoneWithSuccess; 286 } 287 288 295 public boolean logout() throws LoginException { 296 297 if (loginWasDoneWithSuccess) { 299 subject.getPrincipals().remove(new JPrincipal(principalName)); 301 } 302 303 return loginWasDoneWithSuccess; 304 } 305 306 } | Popular Tags |