Java Code Examples for org.apache.directory.api.ldap.model.message.ResultCodeEnum#SUCCESS

The following examples show how to use org.apache.directory.api.ldap.model.message.ResultCodeEnum#SUCCESS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Process the ExtendedResponse received from the server
 * 
 * @param extendedResponse The ExtendedResponse to process
 * @param extendedFuture The ExtendedFuture to feed
 * @param responseId The associated request message ID
 * @throws InterruptedException If the Future is interrupted
 * @throws DecoderException If the response cannot be decoded
 */
private void extendedReceived( ExtendedResponse extendedResponse, ExtendedFuture extendedFuture, int responseId ) 
    throws InterruptedException, DecoderException
{
    if ( LOG.isDebugEnabled() )
    {
        if ( extendedResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            // Everything is fine, return the response
            LOG.debug( I18n.msg( I18n.MSG_04118_EXTENDED_SUCCESSFUL, extendedResponse ) );
        }
        else
        {
            // We have had an error
            LOG.debug( I18n.msg( I18n.MSG_04117_EXTENDED_FAILED, extendedResponse ) );
        }
    }
    
    extendedResponse = handleOpaqueResponse( extendedResponse, extendedFuture );

    // Store the response into the future
    extendedFuture.set( extendedResponse );

    // Remove the future from the map
    removeFromFutureMaps( responseId );
}
 
Example 2
Source File: AbstractPasswordPolicyResponder.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final PasswordWarning process( PasswordPolicyOperation operation )
    throws PasswordException
{
    try
    {
        ResultResponse response = operation.process();
        PasswordPolicyResponse passwordPolicyResponse = getPasswordPolicy( response );
        ResultCodeEnum resultCode = response.getLdapResult().getResultCode();
        
        if ( resultCode == ResultCodeEnum.SUCCESS )
        {
            return success( passwordPolicyResponse );
        }
        else
        {
            throw fail( response, passwordPolicyResponse, resultCode );
        }
    }
    catch ( LdapException e )
    {
        throw new PasswordException().setLdapException( e );
    }
}
 
Example 3
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Process the DeleteResponse received from the server
 * 
 * @param deleteResponse The DeleteResponse to process
 * @param deleteFuture The DeleteFuture to feed
 * @param responseId The associated request message ID
 * @throws InterruptedException If the Future is interrupted
 */
private void deleteReceived( DeleteResponse deleteResponse, DeleteFuture deleteFuture, int responseId ) 
    throws InterruptedException
{
    if ( LOG.isDebugEnabled() )
    {
        if ( deleteResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            // Everything is fine, return the response
            LOG.debug( I18n.msg( I18n.MSG_04116_DELETE_SUCCESSFUL, deleteResponse ) );
        }
        else
        {
            // We have had an error
            LOG.debug( I18n.msg( I18n.MSG_04115_DELETE_FAILED, deleteResponse ) );
        }
    }

    // Store the response into the future
    deleteFuture.set( deleteResponse );

    // Remove the future from the map
    removeFromFutureMaps( responseId );
}
 
Example 4
Source File: Dsmlv2Engine.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Binds to the ldap server
 * 
 * @param messageId the message Id
 * @throws LdapException If we had an issue while binding
 * @throws IOException If we had an issue while transmitting the request or re ceiving the response
 */
protected void bind( int messageId ) throws LdapException, IOException
{
    if ( ( connection != null ) && connection.isAuthenticated() )
    {
        return;
    }

    if ( connection == null )
    {
        throw new IOException( I18n.err( I18n.ERR_02002_MISSING_CONNECTION_TO_BIND ) );
    }

    BindRequest bindRequest = new BindRequestImpl();
    bindRequest.setSimple( true );
    bindRequest.setCredentials( Strings.getBytesUtf8( password ) );
    bindRequest.setName( user );
    bindRequest.setVersion3( true );
    bindRequest.setMessageId( messageId );

    BindResponse bindResponse = connection.bind( bindRequest );

    if ( bindResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
    {
        if ( LOG.isWarnEnabled() )
        {
            LOG.warn( I18n.msg( I18n.MSG_02003_ERROR, bindResponse.getLdapResult().getDiagnosticMessage() ) );
        }
    }
}
 
Example 5
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Handles search requests containing the persistent search decorator but
 * delegates to doSimpleSearch() if the changesOnly parameter of the
 * decorator is set to false.
 *
 * @param session the LdapSession for which this search is conducted
 * @param req the search request containing the persistent search decorator
 * @param psearchDecorator the persistent search decorator extracted
 * @throws Exception if failures are encountered while searching
 */
private void handlePersistentSearch( LdapSession session, SearchRequest req,
    PersistentSearch psearch ) throws Exception
{
    /*
     * We want the search to complete first before we start listening to
     * events when the decorator does NOT specify changes ONLY mode.
     */
    if ( !psearch.isChangesOnly() )
    {
        SearchResultDone done = doSimpleSearch( session, req );

        // ok if normal search beforehand failed somehow quickly abandon psearch
        if ( done.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
        {
            session.getIoSession().write( done );
            return;
        }
    }

    if ( req.isAbandoned() )
    {
        return;
    }

    // now we process entries forever as they change
    PersistentSearchListener persistentSearchListener = new PersistentSearchListener( session, req );

    // compose notification criteria and add the listener to the event
    // service using that notification criteria to determine which events
    // are to be delivered to the persistent search issuing client
    NotificationCriteria criteria = new NotificationCriteria();
    criteria.setAliasDerefMode( req.getDerefAliases() );
    criteria.setBase( req.getBase() );
    criteria.setFilter( req.getFilter() );
    criteria.setScope( req.getScope() );
    criteria.setEventMask( EventType.getEventTypes( psearch.getChangeTypes() ) );
    getLdapServer().getDirectoryService().getEventService().addListener( persistentSearchListener, criteria );
    req.addAbandonListener( new SearchAbandonListener( ldapServer, persistentSearchListener ) );
}
 
Example 6
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T extends ResultResponse> T responseOrException( T response )
{
    if ( ResultCodeEnum.SUCCESS != response.getLdapResult().getResultCode() )
    {
        throw new LdapRequestUnsuccessfulException( response );
    }
    return response;
}
 
Example 7
Source File: AcceleratorDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Deactivate user role from impl session
 * This function follows the pattern from: {@link org.apache.directory.fortress.core.AccessMgr#dropActiveRole(org.apache.directory.fortress.core.model.Session, org.apache.directory.fortress.core.model.UserRole)}.
 * Success will result in impl session state to be modified inside server-side cache.
 * It uses the {@link RbacDropRoleRequest} and {@link RbacDropRoleResponse} accelerator APIs.
 *
 * @param session contains a valid sessionId captured from accelerator createSession method.
 * @param userRole both the {@link org.apache.directory.fortress.core.model.UserRole#userId} and {@link UserRole#name} fields must be set before invoking.
 * @throws SecurityException rethrows {@code LdapException} with {@code GlobalErrIds.ACEL_DROP_ROLE_ERR}.
 */
void dropActiveRole( Session session, UserRole userRole ) throws SecurityException
{
    LdapConnection ld = null;

    try
    {
        ld = getAdminConnection();
        RbacDropRoleRequest dropRoleRequest = new RbacDropRoleRequestImpl();
        dropRoleRequest.setSessionId( session.getSessionId() );
        dropRoleRequest.setRole( userRole.getName() );
        dropRoleRequest.setUserIdentity( userRole.getUserId() );
        // Send the request
        RbacDropRoleResponse rbacDropRoleResponse = ( RbacDropRoleResponse ) ld.extended(
            dropRoleRequest );
        LOG.debug( "dropActiveRole result: {}", rbacDropRoleResponse.getLdapResult().getResultCode() );

        if ( rbacDropRoleResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
        {
            String info = "dropActiveRole Role [" + userRole.getName() + "] User ["
                + session.getUserId() + "], not previously activated.";
            throw new SecurityException( GlobalErrIds.URLE_NOT_ACTIVE, info );
        }
    }
    catch ( LdapException e )
    {
        String error = "dropActiveRole role name [" + userRole.getName() + "] caught LDAPException=" + " msg=" + e
            .getMessage();
        throw new SecurityException( GlobalErrIds.ACEL_DROP_ROLE_ERR, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example 8
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void processResponse( final ResultResponse response )
        throws ChaiOperationException
{
    final boolean success = response.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS;
    if ( !success )
    {
        final String msg = response.getLdapResult().getDiagnosticMessage();
        throw ChaiOperationException.forErrorMessage( msg );
    }
}
 
Example 9
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Process the BindResponse received from the server
 * 
 * @param bindResponse The BindResponse to process
 * @param bindFuture The BindFuture to feed
 * @param responseId The associated request message ID
 * @throws InterruptedException If the Future is interrupted
 */
private void bindReceived( BindResponse bindResponse, BindFuture bindFuture, int responseId ) 
    throws InterruptedException
{
    // remove the listener from the listener map
    if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
    {
        authenticated.set( true );

        // Everything is fine, return the response
        if ( LOG.isDebugEnabled() )
        { 
            LOG.debug( I18n.msg( I18n.MSG_04101_BIND_SUCCESSFUL, bindResponse ) );
        }
    }
    else
    {
        // We have had an error
        if ( LOG.isDebugEnabled() )
        { 
            LOG.debug( I18n.msg( I18n.MSG_04100_BIND_FAIL, bindResponse ) );
        }
    }

    // Store the response into the future
    bindFuture.set( bindResponse );

    // Remove the future from the map
    removeFromFutureMaps( responseId );
}
 
Example 10
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DeleteResponse delete( DeleteRequest deleteRequest ) throws LdapException
{
    if ( deleteRequest == null )
    {
        String msg = I18n.err( I18n.ERR_04149_CANNOT_PROCESS_NULL_DEL_REQ );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    DeleteFuture deleteFuture = deleteAsync( deleteRequest );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        DeleteResponse delResponse = deleteFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( delResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            {
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Delete" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( delResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04116_DELETE_SUCCESSFUL, delResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04115_DELETE_FAILED, delResponse ) );
            }
        }

        return delResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        // Send an abandon request
        if ( !deleteFuture.isCancelled() )
        {
            abandon( deleteRequest.getMessageId() );
        }

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 11
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ModifyDnResponse modifyDn( ModifyDnRequest modDnRequest ) throws LdapException
{
    if ( modDnRequest == null )
    {
        String msg = I18n.err( I18n.ERR_04145_ROOT_DSE_CANNOT_BE_TARGET );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    ModifyDnFuture modifyDnFuture = modifyDnAsync( modDnRequest );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        ModifyDnResponse modifyDnResponse = modifyDnFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( modifyDnResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            {
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "ModifyDn" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( modifyDnResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04125_MODIFYDN_SUCCESSFUL, modifyDnResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04124_MODIFYDN_FAILED, modifyDnResponse ) );
            }
        }

        return modifyDnResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        // Send an abandon request
        if ( !modifyDnFuture.isCancelled() )
        {
            abandon( modDnRequest.getMessageId() );
        }

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 12
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ExtendedResponse extended( ExtendedRequest extendedRequest ) throws LdapException
{
    if ( extendedRequest == null )
    {
        String msg = I18n.err( I18n.ERR_04154_CANNOT_PROCESS_NULL_EXT_REQ );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    ExtendedFuture extendedFuture = extendedAsync( extendedRequest );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        ExtendedResponse response = ( ExtendedResponse ) extendedFuture
            .get( timeout, TimeUnit.MILLISECONDS );

        if ( response == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            {
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Extended" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( response.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04118_EXTENDED_SUCCESSFUL, response ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04117_EXTENDED_FAILED, response ) );
            }
        }

        // Get back the response. It's still an opaque response
        if ( Strings.isEmpty( response.getResponseName() ) )
        {
            response.setResponseName( extendedRequest.getRequestName() );
        }

        // Decode the payload now
        return response;
    }
    catch ( Exception ie )
    {
        if ( ie instanceof LdapException )
        {
            throw ( LdapException ) ie;
        }

        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        // Send an abandon request
        if ( !extendedFuture.isCancelled() )
        {
            abandon( extendedRequest.getMessageId() );
        }

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 13
Source File: AcceleratorDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * Activate user role into impl session
 * This function follows the pattern from: {@link org.apache.directory.fortress.core.AccessMgr#addActiveRole(org.apache.directory.fortress.core.model.Session, org.apache.directory.fortress.core.model.UserRole)}.
 * Success will result in impl session state to be modified inside server-side cache.
 * It uses the {@link RbacAddRoleRequest} and {@link RbacAddRoleResponse} accelerator APIs.
 *
 * @param session contains a valid sessionId captured from accelerator createSession method.
 * @param userRole both the {@link org.apache.directory.fortress.core.model.UserRole#userId} and {@link UserRole#name} fields must be set before invoking.
 * @throws SecurityException rethrows {@code LdapException} with {@code GlobalErrIds.ACEL_ADD_ROLE_ERR}.
 */
void addActiveRole( Session session, UserRole userRole ) throws SecurityException
{
    LdapConnection ld = null;

    try
    {
        ld = getAdminConnection();
        RbacAddRoleRequest addRoleRequest = new RbacAddRoleRequestImpl();
        addRoleRequest.setSessionId( session.getSessionId() );
        addRoleRequest.setRole( userRole.getName() );
        addRoleRequest.setUserIdentity( userRole.getUserId() );
        // Send the request
        RbacAddRoleResponse rbacAddRoleResponse = ( RbacAddRoleResponse ) ld.extended(
            addRoleRequest );
        LOG.debug( "addActiveRole result: {}", rbacAddRoleResponse.getLdapResult().getResultCode() );

        if ( rbacAddRoleResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS )
        {
            String info;
            int rc;

            if ( rbacAddRoleResponse.getLdapResult().getResultCode() == ResultCodeEnum.ATTRIBUTE_OR_VALUE_EXISTS )
            {
                info = "addActiveRole Role [" + userRole.getName() + "] User ["
                    + session.getUserId() + "], already activated.";
                rc = GlobalErrIds.URLE_ALREADY_ACTIVE;
            }
            else
            {
                info = "addActiveRole Role [" + userRole.getName() + "] User ["
                    + session.getUserId() + "], not authorized for user.";
                rc = GlobalErrIds.URLE_ACTIVATE_FAILED;
            }

            throw new SecurityException( rc, info );
        }
    }
    catch ( LdapException e )
    {
        String error = "addActiveRole role name [" + userRole.getName() + "] caught LDAPException=" + " msg=" + e
            .getMessage();
        throw new SecurityException( GlobalErrIds.ACEL_ADD_ROLE_ERR, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example 14
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Bind to the server using a SaslExternalRequest object.
 *
 * @param request The SaslExternalRequest POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind( SaslExternalRequest request ) throws LdapException
{
    if ( request == null )
    {
        String msg = I18n.msg( I18n.MSG_04103_NULL_REQUEST );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    BindFuture bindFuture = bindAsync( request );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( bindResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            { 
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Bind" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            authenticated.set( true );

            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04101_BIND_SUCCESSFUL, bindResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04100_BIND_FAIL, bindResponse ) );
            }
        }

        return bindResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 15
Source File: StoreResultCode.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<Message> container ) throws DecoderException
{
    // The current TLV should be a integer
    // We get it and store it in MessageId
    TLV tlv = container.getCurrentTLV();

    BerValue value = tlv.getValue();
    ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;

    try
    {
        resultCode = ResultCodeEnum.getResultCode( IntegerDecoder.parse( value, 0,
            ResultCodeEnum.E_SYNC_REFRESH_REQUIRED
                .getResultCode() ) );
    }
    catch ( IntegerDecoderException ide )
    {
        LOG.error( I18n.err( I18n.ERR_05108_INVALID_RESULT_CODE, Strings.dumpBytes( value.getData() ), ide.getMessage() ) );

        throw new DecoderException( ide.getMessage(), ide );
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05109_RESULT_CODE_IS, resultCode ) );
    }

    ResultResponse response = ( ResultResponse ) container.getMessage();
    
    LdapResult ldapResult;
    
    if ( response == null ) 
    {
        ldapResult = new LdapResultImpl();
    }
    else
    {
        ldapResult = response.getLdapResult();
    }

    container.setLdapResult( ldapResult );
    ldapResult.setResultCode( resultCode );
}
 
Example 16
Source File: AcceleratorDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * Authenticate user and return sessionId inside {@link org.apache.directory.fortress.core.model.Session#sessionId}.
 * This function follows the pattern from: {@link org.apache.directory.fortress.core.AccessMgr#createSession(org.apache.directory.fortress.core.model.User, boolean)}
 * Success will result in impl session state, i.e. {@link org.apache.directory.fortress.core.model.Session}, to be stored on server-side.
 * Result may be stored inside RBAC server-side audit record and retrieved with {@link org.apache.directory.fortress.core.AuditMgr#searchBinds(org.apache.directory.fortress.core.model.UserAudit)}
 *
 * It uses the {@link RbacCreateSessionRequest} and {@link RbacCreateSessionResponse} accelerator APIs.
 *
 *
 * @param user
 * @return session contains a valid sessionId captured from accelerator createSession method.
 *
 * @throws SecurityException rethrows {@code LdapException} with {@code GlobalErrIds.ACEL_CREATE_SESSION_ERR}.
 *
 */
Session createSession( User user ) throws SecurityException
{
    Session session = null;
    LdapConnection ld = null;

    try
    {
        ld = getAdminConnection();
        ld.setTimeOut( 0 );
        // Create a new RBAC session
        RbacCreateSessionRequest rbacCreateSessionRequest = new RbacCreateSessionRequestImpl();
        //rbacCreateSessionRequest.setTenantId( "jts" );
        rbacCreateSessionRequest.setTenantId( user.getContextId() );
        rbacCreateSessionRequest.setUserIdentity( user.getUserId() );
        rbacCreateSessionRequest.setPassword( new String( user.getPassword() ) );

        if ( CollectionUtils.isNotEmpty( user.getRoles() ) )
        {
            for ( UserRole userRole : user.getRoles() )
            {
                rbacCreateSessionRequest.addRole( userRole.getName() );
            }
        }

        // Send the request
        RbacCreateSessionResponse rbacCreateSessionResponse = ( RbacCreateSessionResponse ) ld.extended(
            rbacCreateSessionRequest );
        LOG.debug( "createSession userId: {}, sessionId: {}, resultCode: {}",
            user.getUserId(), rbacCreateSessionResponse.getSessionId(),
            rbacCreateSessionResponse.getLdapResult().getResultCode() );
        session = new Session( user, rbacCreateSessionResponse.getSessionId() );

        if ( rbacCreateSessionResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            session.setAuthenticated( true );
        }
        else
        {
            session.setAuthenticated( false );
            String info = "createSession UserId [" + user.getUserId() + "] failed: "
                + rbacCreateSessionResponse.getLdapResult() + " , resultCode: "
                + rbacCreateSessionResponse.getLdapResult().getResultCode().getResultCode();
            throw new SecurityException( GlobalErrIds.USER_PW_INVLD, info );
        }
    }
    catch ( LdapException e )
    {
        String error = "createSession userId [" + user.getUserId() + "] caught LDAPException=" + " msg=" + e
            .getMessage();
        throw new SecurityException( GlobalErrIds.ACEL_CREATE_SESSION_ERR, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return session;
}
 
Example 17
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Bind to the server using a SaslRequest object.
 *
 * @param request The SaslRequest POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind( SaslRequest request ) throws LdapException
{
    if ( request == null )
    {
        String msg = I18n.msg( I18n.MSG_04103_NULL_REQUEST );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    BindFuture bindFuture = bindAsync( request );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( bindResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            { 
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Bind" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            authenticated.set( true );

            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04101_BIND_SUCCESSFUL, bindResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04100_BIND_FAIL, bindResponse ) );
            }
        }

        return bindResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 18
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * SASL PLAIN Bind on a server.
 *
 * @param authzid The Authorization identity
 * @param authcid The Authentication identity
 * @param credentials The password. It can't be null
 * @return The BindResponse LdapResponse
 * @throws LdapException if some error occurred
 */
public BindResponse bindSaslPlain( String authzid, String authcid, String credentials ) throws LdapException
{
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04127_SASL_PLAIN_BIND ) );
    }

    // Create the BindRequest
    SaslPlainRequest saslRequest = new SaslPlainRequest();
    saslRequest.setAuthorizationId( authzid );
    saslRequest.setUsername( authcid );
    saslRequest.setCredentials( credentials );

    BindFuture bindFuture = bindAsync( saslRequest );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( bindResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            { 
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Bind" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            authenticated.set( true );

            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04101_BIND_SUCCESSFUL, bindResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04100_BIND_FAIL, bindResponse ) );
            }
        }

        return bindResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );

        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 19
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public BindResponse bind( BindRequest bindRequest ) throws LdapException
{
    if ( bindRequest == null )
    {
        String msg = I18n.err( I18n.ERR_04128_CANNOT_PROCESS_NULL_BIND_REQ );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    BindFuture bindFuture = bindAsync( bindRequest );

    // Get the result from the future
    try
    {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get( timeout, TimeUnit.MILLISECONDS );

        if ( bindResponse == null )
        {
            // We didn't received anything : this is an error
            if ( LOG.isErrorEnabled() )
            { 
                LOG.error( I18n.err( I18n.ERR_04112_OP_FAILED_TIMEOUT, "Bind" ) );
            }
            
            throw new LdapException( TIME_OUT_ERROR );
        }

        if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
        {
            authenticated.set( true );

            // Everything is fine, return the response
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04101_BIND_SUCCESSFUL, bindResponse ) );
            }
        }
        else
        {
            // We have had an error
            if ( LOG.isDebugEnabled() )
            { 
                LOG.debug( I18n.msg( I18n.MSG_04100_BIND_FAIL, bindResponse ) );
            }
        }

        return bindResponse;
    }
    catch ( Exception ie )
    {
        // Catch all other exceptions
        LOG.error( NO_RESPONSE_ERROR, ie );
        
        throw new LdapException( NO_RESPONSE_ERROR, ie );
    }
}
 
Example 20
Source File: LdapResultImplTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Tests for equality when the lockable parent is the same.
 */
@Test
public void testEqualsDiffImpl()
{
    LdapResultImpl r0 = new LdapResultImpl();

    LdapResult r1 = new LdapResult()
    {
        public ResultCodeEnum getResultCode()
        {
            return ResultCodeEnum.SUCCESS;
        }


        public void setResultCode( ResultCodeEnum a_resultCode )
        {
        }


        public Dn getMatchedDn()
        {
            return null;
        }


        public void setMatchedDn( Dn dn )
        {
        }


        public String getDiagnosticMessage()
        {
            return null;
        }


        public void setDiagnosticMessage( String diagnosticMessage )
        {
        }


        public boolean isReferral()
        {
            return false;
        }


        public Referral getReferral()
        {
            return null;
        }


        public void setReferral( Referral referral )
        {
        }


        public boolean isDefaultSuccess()
        {
            return false;
        }
    };

    assertTrue( r0.equals( r1 ), "r0 equals should see other impl r1 as equal" );
    assertFalse( r1.equals( r0 ), "r1 impl uses Object.equals() so it should not see r0 as the same object" );
}