1 9 package org.jboss.portal.core.security.jaas; 10 11 import java.util.Collections ; 12 import java.util.Map ; 13 import java.util.WeakHashMap ; 14 import java.io.Serializable ; 15 16 import javax.security.auth.Subject ; 17 import javax.security.auth.callback.Callback ; 18 import javax.security.auth.callback.CallbackHandler ; 19 import javax.security.auth.callback.PasswordCallback ; 20 import javax.security.auth.callback.UnsupportedCallbackException ; 21 import javax.security.auth.login.LoginException ; 22 import javax.security.auth.spi.LoginModule ; 23 24 32 public class CacheLoginModule implements LoginModule 33 { 34 35 36 private static final Map cache = Collections.synchronizedMap(new WeakHashMap ()); 37 38 48 public static Handle cache(String key, String userName, String password) 49 { 50 Handle handle = new Handle(key); 51 cache.put(handle, new UserNamePassword(userName, password)); 52 return handle; 53 } 54 55 private Subject subject; 56 private CallbackHandler callbackHandler; 57 private Map sharedState; 58 private Map options; 59 60 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) 61 { 62 this.subject = subject; 63 this.callbackHandler = callbackHandler; 64 this.sharedState = sharedState; 65 this.options = options; 66 } 67 68 public boolean login() throws LoginException 69 { 70 String key = null; 71 try 72 { 73 PasswordCallback pc = new PasswordCallback ("Password: ", false);; 74 Callback [] callbacks = {pc}; 75 callbackHandler.handle(callbacks); 76 char[] tmp = pc.getPassword(); 77 if( tmp != null ) 78 { 79 char[] keyAsChars = new char[tmp.length]; 80 System.arraycopy(tmp, 0, keyAsChars, 0, tmp.length); 81 pc.clearPassword(); 82 key = new String (keyAsChars); 83 } 84 } 85 catch(java.io.IOException ioe) 86 { 87 throw new LoginException (ioe.toString()); 88 } 89 catch(UnsupportedCallbackException uce) 90 { 91 throw new LoginException ("CallbackHandler does not support: " + uce.getCallback()); 92 } 93 94 UserNamePassword up = null; 96 if (key != null) 97 { 98 Handle handle = new Handle(key); 99 up = (UserNamePassword)cache.remove(handle); 100 if (up != null) 101 { 102 sharedState.put("javax.security.auth.login.name", up.userName); 103 sharedState.put("javax.security.auth.login.password", up.password); 104 } 105 else 106 { 107 } 109 } 110 else 111 { 112 } 114 return true; 115 } 116 117 public boolean commit() throws LoginException 118 { 119 return true; 120 } 121 122 public boolean abort() throws LoginException 123 { 124 return true; 125 } 126 127 public boolean logout() throws LoginException 128 { 129 return true; 130 } 131 132 138 private static class UserNamePassword 139 { 140 private final String userName; 141 private final String password; 142 private UserNamePassword(String userName, String password) 143 { 144 if (userName == null) 145 { 146 throw new IllegalArgumentException ("No null user name"); 147 } 148 if (password == null) 149 { 150 throw new IllegalArgumentException ("No null password"); 151 } 152 this.userName = userName; 153 this.password = password; 154 } 155 } 156 157 160 public static class Handle implements Serializable 161 { 162 private final String key; 163 private Handle(String key) 164 { 165 if (key == null) 166 { 167 throw new IllegalArgumentException (); 168 } 169 this.key = key; 170 } 171 public int hashCode() 172 { 173 return key.hashCode(); 174 } 175 public boolean equals(Object obj) 176 { 177 if (obj == this) 178 { 179 return true; 180 } 181 if (obj instanceof Handle) 182 { 183 Handle other = (Handle)obj; 184 return other.key.equals(key); 185 } 186 return false; 187 } 188 } 189 } 190 | Popular Tags |