001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.codec.bind;
021
022
023 import java.nio.BufferOverflowException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.directory.shared.asn1.ber.tlv.TLV;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.ldap.codec.LdapConstants;
029 import org.apache.directory.shared.ldap.util.StringTools;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033
034 /**
035 * A ldapObject which stores the Simple authentication for a BindRequest.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $,
039 */
040 public class SimpleAuthentication extends LdapAuthentication
041 {
042 /** The logger */
043 private static Logger log = LoggerFactory.getLogger( SimpleAuthentication.class );
044
045 /** A speedup for logger */
046 private static final boolean IS_DEBUG = log.isDebugEnabled();
047
048 // ~ Instance fields
049 // ----------------------------------------------------------------------------
050
051 /** The simple authentication password */
052 private byte[] simple;
053
054
055 /**
056 * @see Asn1Object#Asn1Object
057 */
058 public SimpleAuthentication()
059 {
060 super();
061 }
062
063 // ~ Methods
064 // ------------------------------------------------------------------------------------
065
066 /**
067 * Get the simple password
068 *
069 * @return The password
070 */
071 public byte[] getSimple()
072 {
073 if ( simple == null )
074 {
075 return null;
076 }
077
078 final byte[] copy = new byte[ simple.length ];
079 System.arraycopy( simple, 0, copy, 0, simple.length );
080 return copy;
081 }
082
083
084 /**
085 * Set the simple password
086 *
087 * @param simple The simple password
088 */
089 public void setSimple( byte[] simple )
090 {
091 if ( simple != null )
092 {
093 this.simple = new byte[ simple.length ];
094 System.arraycopy( simple, 0, this.simple, 0, simple.length );
095 } else {
096 this.simple = null;
097 }
098 }
099
100
101 /**
102 * Compute the Simple authentication length
103 *
104 * Simple authentication : 0x80 L1 simple
105 *
106 * L1 = Length(simple)
107 * Length(Simple authentication) = Length(0x80) + Length(L1) + Length(simple)
108 */
109 public int computeLength()
110 {
111 int length = 1;
112
113 length += TLV.getNbBytes( simple.length ) + simple.length;
114
115 if ( IS_DEBUG )
116 {
117 log.debug( "Simple Authentication length : {}", Integer.valueOf( length ) );
118 }
119
120 return length;
121 }
122
123
124 /**
125 * Encode the simple authentication to a PDU.
126 *
127 * SimpleAuthentication : 0x80 LL simple
128 *
129 * @param buffer The buffer where to put the PDU
130 * @return The PDU.
131 */
132 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
133 {
134 if ( buffer == null )
135 {
136 log.error( "Cannot put a PDU in a null buffer !" );
137 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
138 }
139
140 try
141 {
142 // The simpleAuthentication Tag
143 buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SIMPLE_TAG );
144 buffer.put( TLV.getBytes( simple.length ) );
145
146 if ( simple.length != 0 )
147 {
148 buffer.put( simple );
149 }
150 }
151 catch ( BufferOverflowException boe )
152 {
153 log.error( "The PDU buffer size is too small !" );
154 throw new EncoderException( "The PDU buffer size is too small !" );
155 }
156
157 return buffer;
158 }
159
160
161 /**
162 * Return the simple authentication as a string
163 *
164 * @return The simple authentication string.
165 */
166 public String toString()
167 {
168 return ( ( simple == null ) ? "null" : StringTools.dumpBytes( simple) );
169 }
170 }