1 36 package org.columba.ristretto.auth.mechanism; 37 38 import java.io.IOException ; 39 import java.util.Hashtable ; 40 import java.util.logging.ConsoleHandler ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 import javax.security.auth.callback.Callback ; 45 import javax.security.auth.callback.CallbackHandler ; 46 import javax.security.auth.callback.NameCallback ; 47 import javax.security.auth.callback.PasswordCallback ; 48 import javax.security.auth.callback.TextOutputCallback ; 49 import javax.security.auth.callback.UnsupportedCallbackException ; 50 51 import org.columba.ristretto.auth.AuthenticationException; 52 import org.columba.ristretto.auth.AuthenticationMechanism; 53 import org.columba.ristretto.auth.AuthenticationServer; 54 55 62 public class DigestMD5Mechanism implements 63 AuthenticationMechanism, 64 CallbackHandler { 65 66 67 private static final Logger LOG = Logger.getLogger("org.columba.ristretto.auth.mechanism"); 68 69 private String username; 70 private char[] password; 71 72 SaslWrapper sasl; 73 74 75 78 public void authenticate(AuthenticationServer server, String user, 79 char[] password) throws IOException , AuthenticationException { 80 81 this.username = user; 82 this.password = password; 83 try { 84 sasl = new SaslWrapper(); 85 sasl.createClient(new String []{"DIGEST-MD5"}, 86 user, server.getService(), server.getHostName(), 87 new Hashtable (), this); 88 byte[] serverChallenge = server.authReceive(); 89 LOG.fine(new String (serverChallenge)); 90 91 byte[] response = sasl.evaluateChallenge(serverChallenge); 92 LOG.fine(new String (response)); 93 server.authSend(response); 94 95 serverChallenge = server.authReceive(); 96 LOG.fine(new String (serverChallenge)); 97 response = sasl.evaluateChallenge(serverChallenge); 98 if (!sasl.isComplete()) { 99 throw new AuthenticationException("Authentication failed"); 100 } 101 server.authSend(new byte[0]); 102 } catch (Exception e) { 103 if( e.getCause() instanceof IOException ) throw (IOException )e.getCause(); 104 else throw new AuthenticationException(e.getCause()); 105 } 106 } 107 108 111 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 112 for (int i = 0; i < callbacks.length; i++) { 113 if (callbacks[i] instanceof TextOutputCallback ) { 114 TextOutputCallback toc = (TextOutputCallback ) callbacks[i]; 116 switch (toc.getMessageType()) { 117 case TextOutputCallback.INFORMATION : 118 LOG.info(toc.getMessage()); 119 break; 120 case TextOutputCallback.ERROR : 121 LOG.severe(toc.getMessage()); 122 break; 123 case TextOutputCallback.WARNING : 124 LOG.warning(toc.getMessage()); 125 break; 126 default : 127 LOG.fine(toc.getMessage()); 128 } 129 } else if (callbacks[i] instanceof NameCallback ) { 130 NameCallback nc = (NameCallback ) callbacks[i]; 131 nc.setName(username); 132 } else if (callbacks[i] instanceof PasswordCallback ) { 133 PasswordCallback pc = (PasswordCallback ) callbacks[i]; 134 pc.setPassword(password); 135 } else if (sasl.handle(callbacks[i]) ) { 136 } else { 137 throw new UnsupportedCallbackException (callbacks[i], 138 "Unrecognized Callback"); 139 } 140 } 141 } 142 } 143 | Popular Tags |