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 *    https://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 */
020package org.apache.directory.api.ldap.codec.actions.request.bind;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
025import org.apache.directory.api.asn1.ber.tlv.TLV;
026import org.apache.directory.api.i18n.I18n;
027import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
028import org.apache.directory.api.ldap.codec.api.ResponseCarryingException;
029import org.apache.directory.api.ldap.model.message.BindRequest;
030import org.apache.directory.api.ldap.model.message.BindResponseImpl;
031import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * The action used to store the BindRequest version MessageID.
038 * <pre>
039 * BindRequest ::= [APPLICATION 0] SEQUENCE {
040 *     ....
041 *     authentication          AuthenticationChoice }
042 *
043 * AuthenticationChoice ::= CHOICE {
044 *     ...
045 *     sasl                  [3] SaslCredentials }
046 *     ...
047 *
048 * We have to create an Authentication Object to store the credentials.
049 * </pre>
050 *
051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
052 */
053public class InitSaslBind extends GrammarAction<LdapMessageContainer<BindRequest>>
054{
055    /** The logger */
056    private static final Logger LOG = LoggerFactory.getLogger( InitSaslBind.class );
057
058    /**
059     * Instantiates a new action.
060     */
061    public InitSaslBind()
062    {
063        super( "Initialize Bind SASL Authentication" );
064    }
065
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public void action( LdapMessageContainer<BindRequest> container ) throws DecoderException
072    {
073        BindRequest bindRequestMessage = container.getMessage();
074        TLV tlv = container.getCurrentTLV();
075
076        // We will check that the sasl is not null
077        if ( tlv.getLength() == 0 )
078        {
079            String msg = I18n.err( I18n.ERR_05116_SASL_CREDS_CANT_BE_NULL );
080            LOG.error( msg );
081
082            BindResponseImpl response = new BindResponseImpl( bindRequestMessage.getMessageId() );
083
084            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_CREDENTIALS,
085                bindRequestMessage.getDn(), null );
086        }
087
088        bindRequestMessage.setSimple( false );
089
090        if ( LOG.isDebugEnabled() )
091        {
092            LOG.debug( I18n.msg( I18n.MSG_05115_SASL_CREDS_CREATED ) );
093        }
094    }
095}