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.i18n.I18n;
029 import org.apache.directory.shared.ldap.codec.LdapConstants;
030 import org.apache.directory.shared.ldap.util.StringTools;
031 import org.slf4j.Logger;
032 import org.slf4j.LoggerFactory;
033
034
035 /**
036 * A ldapObject which stores the Simple authentication for a BindRequest.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @version $Rev: 912399 $, $Date: 2010-02-21 22:52:31 +0200 (Sun, 21 Feb 2010) $,
040 */
041 public class SimpleAuthentication extends LdapAuthentication
042 {
043 /** The logger */
044 private static Logger log = LoggerFactory.getLogger( SimpleAuthentication.class );
045
046 /** A speedup for logger */
047 private static final boolean IS_DEBUG = log.isDebugEnabled();
048
049 // ~ Instance fields
050 // ----------------------------------------------------------------------------
051
052 /** The simple authentication password */
053 private byte[] simple;
054
055
056 /**
057 * @see Asn1Object#Asn1Object
058 */
059 public SimpleAuthentication()
060 {
061 super();
062 }
063
064 // ~ Methods
065 // ------------------------------------------------------------------------------------
066
067 /**
068 * Get the simple password
069 *
070 * @return The password
071 */
072 public byte[] getSimple()
073 {
074 if ( simple == null )
075 {
076 return null;
077 }
078
079 final byte[] copy = new byte[ simple.length ];
080 System.arraycopy( simple, 0, copy, 0, simple.length );
081 return copy;
082 }
083
084
085 /**
086 * Set the simple password
087 *
088 * @param simple The simple password
089 */
090 public void setSimple( byte[] simple )
091 {
092 if ( simple != null )
093 {
094 this.simple = new byte[ simple.length ];
095 System.arraycopy( simple, 0, this.simple, 0, simple.length );
096 } else {
097 this.simple = null;
098 }
099 }
100
101
102 /**
103 * Compute the Simple authentication length
104 *
105 * Simple authentication : 0x80 L1 simple
106 *
107 * L1 = Length(simple)
108 * Length(Simple authentication) = Length(0x80) + Length(L1) + Length(simple)
109 */
110 public int computeLength()
111 {
112 int length = 1;
113
114 length += TLV.getNbBytes( simple.length ) + simple.length;
115
116 if ( IS_DEBUG )
117 {
118 log.debug( "Simple Authentication length : {}", Integer.valueOf( length ) );
119 }
120
121 return length;
122 }
123
124
125 /**
126 * Encode the simple authentication to a PDU.
127 *
128 * SimpleAuthentication : 0x80 LL simple
129 *
130 * @param buffer The buffer where to put the PDU
131 * @return The PDU.
132 */
133 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
134 {
135 if ( buffer == null )
136 {
137 log.error( I18n.err( I18n.ERR_04023 ) );
138 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
139 }
140
141 try
142 {
143 // The simpleAuthentication Tag
144 buffer.put( ( byte ) LdapConstants.BIND_REQUEST_SIMPLE_TAG );
145 buffer.put( TLV.getBytes( simple.length ) );
146
147 if ( simple.length != 0 )
148 {
149 buffer.put( simple );
150 }
151 }
152 catch ( BufferOverflowException boe )
153 {
154 log.error( I18n.err( I18n.ERR_04005 ) );
155 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
156 }
157
158 return buffer;
159 }
160
161
162 /**
163 * Return the simple authentication as a string
164 *
165 * @return The simple authentication string.
166 */
167 public String toString()
168 {
169 return ( ( simple == null ) ? "null" : StringTools.dumpBytes( simple) );
170 }
171 }