1 7 8 package org.jboss.security; 9 10 11 import java.util.Map ; 12 import java.util.Set ; 13 import java.security.Principal ; 14 import javax.security.auth.Subject ; 15 import javax.security.auth.callback.Callback ; 16 import javax.security.auth.callback.CallbackHandler ; 17 import javax.security.auth.callback.NameCallback ; 18 import javax.security.auth.callback.PasswordCallback ; 19 import javax.security.auth.callback.UnsupportedCallbackException ; 20 import javax.security.auth.login.LoginException ; 21 import javax.security.auth.spi.LoginModule ; 22 23 48 public class AltClientLoginModule implements LoginModule 49 { 50 private Subject subject; 51 private CallbackHandler callbackHandler; 52 53 private Map sharedState; 54 55 private boolean useFirstPass; 56 private String username; 57 private char[] password = null; 58 59 62 public void initialize(Subject subject, CallbackHandler callbackHandler, 63 Map sharedState, Map options) 64 { 65 this.subject = subject; 66 this.callbackHandler = callbackHandler; 67 this.sharedState = sharedState; 68 String mt = (String ) options.get("multi-threaded"); 70 if( mt != null && Boolean.valueOf(mt).booleanValue() == true ) 71 { 74 SecurityAssociationActions.setServer(); 75 } 76 77 81 String passwordStacking = (String ) options.get("password-stacking"); 82 useFirstPass = passwordStacking != null; 83 } 84 85 88 public boolean login() throws LoginException 89 { 90 if( useFirstPass == true ) 92 { 93 return true; 94 } 95 96 99 if (callbackHandler == null) 100 throw new LoginException ("Error: no CallbackHandler available " + 101 "to garner authentication information from the user"); 102 103 PasswordCallback pc = new PasswordCallback ("Password: ", false); 104 NameCallback nc = new NameCallback ("User name: ", "guest"); 105 Callback [] callbacks = {nc, pc}; 106 try 107 { 108 char[] tmpPassword; 109 110 callbackHandler.handle(callbacks); 111 username = nc.getName(); 112 tmpPassword = pc.getPassword(); 113 if (tmpPassword != null) 114 { 115 password = new char[tmpPassword.length]; 116 System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length); 117 pc.clearPassword(); 118 } 119 } 120 catch (java.io.IOException ioe) 121 { 122 throw new LoginException (ioe.toString()); 123 } 124 catch (UnsupportedCallbackException uce) 125 { 126 throw new LoginException ("Error: " + uce.getCallback().toString() + 127 " not available to garner authentication information " + 128 "from the user"); 129 } 130 return true; 131 } 132 133 143 public boolean commit() throws LoginException 144 { 145 Set principals = subject.getPrincipals(); 146 Principal p = null; 147 Object credential = password; 148 if( useFirstPass == true ) 149 { 150 Object user = sharedState.get("javax.security.auth.login.name"); 151 if( (user instanceof Principal ) == false ) 152 { 153 username = user != null ? user.toString() : ""; 154 p = new SimplePrincipal(username); 155 } 156 else 157 { 158 p = (Principal ) user; 159 } 160 credential = sharedState.get("javax.security.auth.login.password"); 161 } 162 else 163 { 164 p = new SimplePrincipal(username); 165 } 166 167 if( principals.isEmpty() == false ) 168 p = (Principal ) principals.iterator().next(); 169 SecurityAssociationActions.setPrincipalInfo(p, credential, subject); 170 return true; 171 } 172 173 176 public boolean abort() throws LoginException 177 { 178 int length = password != null ? password.length : 0; 179 for(int n = 0; n < length; n ++) 180 password[n] = 0; 181 SecurityAssociationActions.clear(); 182 return true; 183 } 184 185 public boolean logout() throws LoginException 186 { 187 SecurityAssociationActions.clear(); 188 return true; 189 } 190 } 191 | Popular Tags |