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.i18n.I18n;
27  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
28  import org.apache.directory.api.ldap.codec.api.ResponseCarryingException;
29  import org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator;
30  import org.apache.directory.api.ldap.model.message.BindRequest;
31  import org.apache.directory.api.ldap.model.message.BindResponseImpl;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * The action used to store the BindRequest version MessageID.
39   * <pre>
40   * BindRequest ::= [APPLICATION 0] SEQUENCE {
41   *     ....
42   *     authentication          AuthenticationChoice }
43   *
44   * AuthenticationChoice ::= CHOICE {
45   *     ...
46   *     sasl                  [3] SaslCredentials }
47   *     ...
48   *
49   * We have to create an Authentication Object to store the credentials.
50   * </pre>
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   */
54  public class InitSaslBind extends GrammarAction<LdapMessageContainer<BindRequestDecorator>>
55  {
56      /** The logger */
57      private static final Logger LOG = LoggerFactory.getLogger( InitSaslBind.class );
58  
59      /** Speedup for logs */
60      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
61  
62  
63      /**
64       * Instantiates a new action.
65       */
66      public InitSaslBind()
67      {
68          super( "Initialize Bind SASL Authentication" );
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void action( LdapMessageContainer<BindRequestDecorator> container ) throws DecoderException
76      {
77          BindRequest bindRequestMessage = container.getMessage();
78          TLV tlv = container.getCurrentTLV();
79  
80          // We will check that the sasl is not null
81          if ( tlv.getLength() == 0 )
82          {
83              String msg = I18n.err( I18n.ERR_04079 );
84              LOG.error( msg );
85  
86              BindResponseImpl response = new BindResponseImpl( bindRequestMessage.getMessageId() );
87  
88              throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_CREDENTIALS,
89                  bindRequestMessage.getDn(), null );
90          }
91  
92          bindRequestMessage.setSimple( false );
93  
94          if ( IS_DEBUG )
95          {
96              LOG.debug( "The SaslCredential has been created" );
97          }
98      }
99  }