UsernameHashedPasswordCallbackHandler.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.client.security;
022 
023 import org.apache.qpid.client.protocol.AMQProtocolSession;
024 
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027 
028 import javax.security.auth.callback.Callback;
029 import javax.security.auth.callback.NameCallback;
030 import javax.security.auth.callback.PasswordCallback;
031 import javax.security.auth.callback.UnsupportedCallbackException;
032 
033 import java.io.IOException;
034 import java.io.UnsupportedEncodingException;
035 import java.security.MessageDigest;
036 import java.security.NoSuchAlgorithmException;
037 
038 public class UsernameHashedPasswordCallbackHandler implements AMQCallbackHandler
039 {
040     private static final Logger _logger = LoggerFactory.getLogger(UsernameHashedPasswordCallbackHandler.class);
041 
042     private AMQProtocolSession _protocolSession;
043 
044     public void initialise(AMQProtocolSession protocolSession)
045     {
046         _protocolSession = protocolSession;
047     }
048 
049     public void handle(Callback[] callbacksthrows IOException, UnsupportedCallbackException
050     {
051         for (int i = 0; i < callbacks.length; i++)
052         {
053             Callback cb = callbacks[i];
054             if (cb instanceof NameCallback)
055             {
056                 ((NameCallbackcb).setName(_protocolSession.getUsername());
057             }
058             else if (cb instanceof PasswordCallback)
059             {
060                 try
061                 {
062                     ((PasswordCallbackcb).setPassword(getHash(_protocolSession.getPassword()));
063                 }
064                 catch (NoSuchAlgorithmException e)
065                 {
066                     UnsupportedCallbackException uce = new UnsupportedCallbackException(cb);
067                     uce.initCause(e);
068                     throw uce;
069                 }
070             }
071             else
072             {
073                 throw new UnsupportedCallbackException(cb);
074             }
075         }
076     }
077 
078     private char[] getHash(String textthrows NoSuchAlgorithmException, UnsupportedEncodingException
079     {
080 
081         byte[] data = text.getBytes("utf-8");
082 
083         MessageDigest md = MessageDigest.getInstance("MD5");
084 
085         for (byte b : data)
086         {
087             md.update(b);
088         }
089 
090         byte[] digest = md.digest();
091 
092         char[] hash = new char[digest.length];
093 
094         int index = 0;
095         for (byte b : digest)
096         {
097             hash[index++(charb;
098         }
099 
100         return hash;
101     }
102 }