1 25 26 package org.objectweb.jonas.security.auth.spi; 27 28 import java.util.Map ; 29 30 import javax.naming.InitialContext ; 31 import javax.rmi.PortableRemoteObject ; 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.JonasSecurityServiceImpl; 41 import org.objectweb.jonas.security.auth.JSubject; 42 import org.objectweb.jonas.security.auth.callback.CertificateCallback; 43 import org.objectweb.jonas.security.realm.factory.JResourceRemote; 44 45 51 public class JResourceLoginModule implements LoginModule { 52 53 56 private static final String DEFAULT_SERVER_NAME = "jonas"; 57 58 61 private Subject subject = null; 62 63 66 private Subject remoteSubject = null; 67 68 71 private CallbackHandler callbackHandler = null; 72 73 76 private Map options = null; 77 78 81 private String password = null; 82 83 86 private boolean loginWasDoneWithSuccess = false;; 87 88 101 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 102 this.subject = subject; 103 this.callbackHandler = callbackHandler; 104 this.options = options; 105 } 106 107 117 public boolean login() throws LoginException { 118 119 if (callbackHandler == null) { 121 throw new LoginException ("No handler has been defined."); 122 } 123 124 String resourceName = (String ) options.get("resourceName"); 126 127 String serverName = (String ) options.get("serverName"); 129 if (serverName == null) { 130 serverName = DEFAULT_SERVER_NAME; 131 } 132 133 String certCallback = (String ) options.get("certCallback"); 135 136 if (resourceName == null) { 138 throw new LoginException ( 139 "You have to give an argument to this login module. The 'resourceName' parameter is required."); 140 } 141 142 String remoteResourceName = serverName + JonasSecurityServiceImpl.REMOTE_RESOUCE; 144 145 146 try { 148 InitialContext ictx = new InitialContext (); 149 JResourceRemote jResourceRemote = null; 150 try { 151 Object o = ictx.lookup(remoteResourceName); 152 jResourceRemote = (JResourceRemote) PortableRemoteObject.narrow(o, JResourceRemote.class); 153 } catch (Exception e) { 154 throw createChainedLoginException("Cannot retrieve the resource '" + remoteResourceName 155 + "'. Check that this resource is bound in the registry and that the server name is correct", e); 156 } 157 158 NameCallback nameCallback = new NameCallback ("User :"); 160 PasswordCallback passwordCallback = new PasswordCallback ("Password :", false); 162 CertificateCallback certificateCallback = new CertificateCallback(); 163 Callback [] callbacks = null; 164 165 if ((certCallback != null) && (Boolean.valueOf(certCallback).booleanValue())) { 166 callbacks = new Callback [] {nameCallback, passwordCallback, certificateCallback}; 167 } else { 168 callbacks = new Callback [] {nameCallback, passwordCallback}; 169 } 170 callbackHandler.handle(callbacks); 171 172 String principalName = nameCallback.getName(); 174 if (principalName == null) { 175 throw new LoginException ("A null username is not a valid username"); 176 } 177 if (principalName.startsWith("##DN##") && (certificateCallback.getUserCertificate() == null)) { 178 throw new LoginException ("Name must have a certificate to access this certificate based access login"); 179 } 180 char[] arrayPass = passwordCallback.getPassword(); 181 if (arrayPass == null) { 182 throw new LoginException ("A null password is not a valid password"); 183 } 184 185 password = new String (arrayPass); 186 JSubject jSubject = null; 187 try { 188 jSubject = jResourceRemote.authenticate(principalName, arrayPass, resourceName); 189 } catch (Exception e) { 190 throw createChainedLoginException("Cannot authenticate with principal name = '" + principalName + "' : " + e.getMessage(), e); 191 } 192 remoteSubject = new Subject (); 193 remoteSubject.getPrivateCredentials().add(password); 194 remoteSubject.getPrincipals().add(jSubject.getName()); 195 remoteSubject.getPrincipals().add(jSubject.getGroup()); 196 197 } catch (Exception e) { 198 throw createChainedLoginException("Error during the login phase : " + e.getMessage(), e); 199 } 200 loginWasDoneWithSuccess = true; 201 return true; 202 } 203 204 210 private static LoginException createChainedLoginException(String msg, Exception e) { 211 LoginException le = new LoginException (msg); 212 le.initCause(e); 213 return le; 214 } 215 216 230 public boolean commit() throws LoginException { 231 232 if (loginWasDoneWithSuccess && remoteSubject != null) { 234 subject.getPrincipals().addAll(remoteSubject.getPrincipals()); 236 subject.getPrivateCredentials().add(password); 237 } 238 return loginWasDoneWithSuccess; 239 } 240 241 252 public boolean abort() throws LoginException { 253 if (loginWasDoneWithSuccess && remoteSubject != null) { 255 remoteSubject = null; 257 } 258 return loginWasDoneWithSuccess; 259 } 260 261 268 public boolean logout() throws LoginException { 269 270 if (loginWasDoneWithSuccess && remoteSubject != null) { 272 subject.getPrincipals().remove(remoteSubject.getPrincipals()); 274 } 275 276 return loginWasDoneWithSuccess; 277 } 278 279 } | Popular Tags |