HashedUser.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.server.security.auth.database;
022 
023 import org.apache.commons.codec.EncoderException;
024 import org.apache.commons.codec.binary.Base64;
025 import org.apache.log4j.Logger;
026 
027 import java.io.UnsupportedEncodingException;
028 import java.security.NoSuchAlgorithmException;
029 import java.security.Principal;
030 
031 public class HashedUser implements Principal
032 {
033     private static final Logger _logger = Logger.getLogger(HashedUser.class);
034 
035     String _name;
036     char[] _password;
037     byte[] _encodedPassword = null;
038     private boolean _modified = false;
039     private boolean _deleted = false;
040 
041     HashedUser(String[] datathrows UnsupportedEncodingException
042     {
043         if (data.length != 2)
044         {
045             throw new IllegalArgumentException("User Data should be length 2, username, password");
046         }
047 
048         _name = data[0];
049 
050         byte[] encoded_password = data[1].getBytes(Base64MD5PasswordFilePrincipalDatabase.DEFAULT_ENCODING);
051 
052         Base64 b64 = new Base64();
053         byte[] decoded = b64.decode(encoded_password);
054 
055         _encodedPassword = encoded_password;
056 
057         _password = new char[decoded.length];
058 
059         int index = 0;
060         for (byte c : decoded)
061         {
062             _password[index++(charc;
063         }
064     }
065 
066     public HashedUser(String name, char[] password)
067     {
068         _name = name;
069         setPassword(password);
070     }
071 
072     public String getName()
073     {
074         return _name;
075     }
076 
077     public String toString()
078     {
079         return _name;
080     }
081 
082     char[] getPassword()
083     {
084         return _password;
085     }
086 
087     void setPassword(char[] password)
088     {
089         _password = password;
090         _modified = true;
091         _encodedPassword = null;
092     }
093 
094     byte[] getEncodedPassword() throws EncoderException, UnsupportedEncodingException, NoSuchAlgorithmException
095     {
096         if (_encodedPassword == null)
097         {
098             encodePassword();
099         }
100         return _encodedPassword;
101     }
102 
103     private void encodePassword() throws EncoderException, UnsupportedEncodingException, NoSuchAlgorithmException
104     {
105         byte[] byteArray = new byte[_password.length];
106         int index = 0;
107         for (char c : _password)
108         {
109             byteArray[index++(bytec;
110         }
111         _encodedPassword = (new Base64()).encode(byteArray);
112     }
113 
114     public boolean isModified()
115     {
116         return _modified;
117     }
118 
119     public boolean isDeleted()
120     {
121         return _deleted;
122     }
123 
124     public void delete()
125     {
126         _deleted = true;
127     }
128 
129     public void saved()
130     {
131         _modified = false;
132     }
133 
134 }