View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.actions.bindRequest;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25  import org.apache.directory.api.asn1.ber.tlv.TLV;
26  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
27  import org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator;
28  import org.apache.directory.api.ldap.model.message.BindRequest;
29  import org.apache.directory.api.util.Strings;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * The action used to store the BindRequest simple authentication
36   * <pre>
37   * BindRequest ::= [APPLICATION 0] SEQUENCE {
38   *     ....
39   *     authentication          AuthenticationChoice }
40   *
41   * AuthenticationChoice ::= CHOICE {
42   *     simple                  [0] OCTET STRING,
43   *     ...
44   *
45   * We have to create an Authentication Object to store the credentials.
46   * </pre>
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoreSimpleAuth extends GrammarAction<LdapMessageContainer<BindRequestDecorator>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( StoreSimpleAuth.class );
53  
54      /** Speedup for logs */
55      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56  
57  
58      /**
59       * Instantiates a new action.
60       */
61      public StoreSimpleAuth()
62      {
63          super( "Store BindRequest Simple Authentication" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void action( LdapMessageContainer<BindRequestDecorator> container ) throws DecoderException
71      {
72          BindRequest bindRequestMessage = container.getMessage();
73          TLV tlv = container.getCurrentTLV();
74  
75          // Allocate the Authentication Object
76          bindRequestMessage.setSimple( true );
77  
78          // We have to handle the special case of a 0 length simple
79          if ( tlv.getLength() == 0 )
80          {
81              bindRequestMessage.setCredentials( Strings.EMPTY_BYTES );
82          }
83          else
84          {
85              bindRequestMessage.setCredentials( tlv.getValue().getData() );
86          }
87  
88          // We can have an END transition
89          container.setGrammarEndAllowed( true );
90  
91          if ( IS_DEBUG )
92          {
93              LOG.debug( "The simple authentication is : {}", Strings.dumpBytes( bindRequestMessage
94                  .getCredentials() ) );
95          }
96      }
97  }