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