1 22 package org.jboss.resource.security; 23 24 import java.security.acl.Group ; 25 import java.security.Principal ; 26 import java.security.NoSuchAlgorithmException ; 27 import java.security.InvalidKeyException ; 28 import java.util.Map ; 29 import java.math.BigInteger ; 30 import javax.resource.spi.security.PasswordCredential ; 31 import javax.security.auth.Subject ; 32 import javax.security.auth.callback.CallbackHandler ; 33 import javax.security.auth.login.LoginException ; 34 import javax.crypto.NoSuchPaddingException; 35 import javax.crypto.BadPaddingException; 36 import javax.crypto.IllegalBlockSizeException; 37 import javax.crypto.Cipher; 38 import javax.crypto.spec.SecretKeySpec; 39 40 import org.jboss.security.SimplePrincipal; 41 import org.jboss.logging.Logger; 42 43 64 public class SecureIdentityLoginModule 65 extends AbstractPasswordCredentialLoginModule 66 { 67 70 private static final Logger log = Logger.getLogger(SecureIdentityLoginModule.class); 71 72 private String username; 73 private String password; 74 75 public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) 76 { 77 super.initialize(subject, handler, sharedState, options); 78 username = (String ) options.get("username"); 80 if( username == null ) 81 { 82 username = (String ) options.get("userName"); 84 if( username == null ) 85 { 86 throw new IllegalArgumentException ("The user name is a required option"); 87 } 88 } 89 password = (String ) options.get("password"); 90 if( password == null ) 91 { 92 throw new IllegalArgumentException ("The password is a required option"); 93 } 94 } 95 96 public boolean login() throws LoginException 97 { 98 log.trace("login called"); 99 if( super.login() == true ) 100 return true; 101 102 super.loginOk = true; 103 return true; 104 } 105 106 public boolean commit() throws LoginException 107 { 108 Principal principal = new SimplePrincipal(username); 109 SubjectActions.addPrincipals(subject, principal); 110 sharedState.put("javax.security.auth.login.name", username); 111 try 113 { 114 char[] decodedPassword = decode(password); 115 PasswordCredential cred = new PasswordCredential (username, decodedPassword); 116 cred.setManagedConnectionFactory(getMcf()); 117 SubjectActions.addCredentials(subject, cred); 118 } 119 catch(Exception e) 120 { 121 log.debug("Failed to decode password", e); 122 throw new LoginException ("Failed to decode password: "+e.getMessage()); 123 } 124 return true; 125 } 126 127 public boolean abort() 128 { 129 username = null; 130 password = null; 131 return true; 132 } 133 134 protected Principal getIdentity() 135 { 136 log.trace("getIdentity called, username="+username); 137 Principal principal = new SimplePrincipal(username); 138 return principal; 139 } 140 141 protected Group [] getRoleSets() throws LoginException 142 { 143 Group [] empty = new Group [0]; 144 return empty; 145 } 146 147 private static String encode(String secret) 148 throws NoSuchPaddingException, NoSuchAlgorithmException , 149 InvalidKeyException , BadPaddingException, IllegalBlockSizeException 150 { 151 byte[] kbytes = "jaas is the way".getBytes(); 152 SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish"); 153 154 Cipher cipher = Cipher.getInstance("Blowfish"); 155 cipher.init(Cipher.ENCRYPT_MODE, key); 156 byte[] encoding = cipher.doFinal(secret.getBytes()); 157 BigInteger n = new BigInteger (encoding); 158 return n.toString(16); 159 } 160 161 private static char[] decode(String secret) 162 throws NoSuchPaddingException, NoSuchAlgorithmException , 163 InvalidKeyException , BadPaddingException, IllegalBlockSizeException 164 { 165 byte[] kbytes = "jaas is the way".getBytes(); 166 SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish"); 167 168 BigInteger n = new BigInteger (secret, 16); 169 byte[] encoding = n.toByteArray(); 170 171 Cipher cipher = Cipher.getInstance("Blowfish"); 172 cipher.init(Cipher.DECRYPT_MODE, key); 173 byte[] decode = cipher.doFinal(encoding); 174 return new String (decode).toCharArray(); 175 } 176 177 182 public static void main(String [] args) throws Exception 183 { 184 String encode = encode(args[0]); 185 System.out.println("Encoded password: "+encode); 186 } 187 } 188 | Popular Tags |