Search in sources :

Example 1 with Result

use of org.forgerock.opendj.ldap.responses.Result in project OpenAM by OpenRock.

the class LDAPAuthUtils method changePassword.

/**
     * Updates to new password by using the parameters passed by the user.
     *
     * @param oldPwd Current password entered.
     * @param password New password entered.
     * @param confirmPassword Confirm password.
     * @throws LDAPUtilException
     */
public void changePassword(String oldPwd, String password, String confirmPassword) throws LDAPUtilException {
    if (password.equals(oldPwd)) {
        setState(ModuleState.WRONG_PASSWORD_ENTERED);
        return;
    }
    if (!(password.equals(confirmPassword))) {
        setState(ModuleState.PASSWORD_MISMATCH);
        return;
    }
    if (password.equals(userId)) {
        setState(ModuleState.USER_PASSWORD_SAME);
        return;
    }
    Connection modConn = null;
    List<Control> controls;
    try {
        ModifyRequest mods = LDAPRequests.newModifyRequest(userDN);
        if (beheraEnabled) {
            mods.addControl(PasswordPolicyRequestControl.newControl(false));
        }
        if (!isAd) {
            mods.addModification(ModificationType.DELETE, LDAP_PASSWD_ATTR, oldPwd);
            mods.addModification(ModificationType.ADD, LDAP_PASSWD_ATTR, password);
            modConn = getConnection();
            modConn.bind(LDAPRequests.newSimpleBindRequest(userDN, oldPwd.toCharArray()));
        } else {
            mods.addModification(ModificationType.DELETE, AD_PASSWD_ATTR, updateADPassword(oldPwd));
            mods.addModification(ModificationType.ADD, AD_PASSWD_ATTR, updateADPassword(password));
            modConn = getAdminConnection();
        }
        Result modResult = modConn.modify(mods);
        controls = processControls(modResult);
        // Were there any password policy controls returned?
        PasswordPolicyResult result = checkControls(controls);
        if (result == null) {
            if (debug.messageEnabled()) {
                debug.message("No controls returned");
            }
            setState(ModuleState.PASSWORD_UPDATED_SUCCESSFULLY);
        } else {
            processPasswordPolicyControls(result);
        }
    } catch (LdapException ere) {
        if (ere.getResult().getResultCode().equals(ResultCode.CONSTRAINT_VIOLATION)) {
            PasswordPolicyResult result = checkControls(processControls(ere.getResult()));
            if (result != null) {
                processPasswordPolicyControls(result);
            } else {
                if (isAd) {
                    setState(ModuleState.PASSWORD_NOT_UPDATE);
                } else {
                    setState(ModuleState.INSUFFICIENT_PASSWORD_QUALITY);
                }
            }
        } else if (ere.getResult().getResultCode().equals(ResultCode.CLIENT_SIDE_CONNECT_ERROR) || ere.getResult().getResultCode().equals(ResultCode.CLIENT_SIDE_SERVER_DOWN) || ere.getResult().getResultCode().equals(ResultCode.UNAVAILABLE) || ere.getResult().getResultCode().equals(ResultCode.CLIENT_SIDE_TIMEOUT)) {
            if (debug.messageEnabled()) {
                debug.message("changepassword:Cannot connect to " + servers + ": ", ere);
            }
            setState(ModuleState.SERVER_DOWN);
            return;
        } else if (ere.getResult().getResultCode().equals(ResultCode.UNWILLING_TO_PERFORM)) {
            // Were there any password policy controls returned?
            PasswordPolicyResult result = checkControls(processControls(ere.getResult()));
            if (result != null) {
                processPasswordPolicyControls(result);
            } else {
                setState(ModuleState.INSUFFICIENT_PASSWORD_QUALITY);
            }
        } else if (ere.getResult().getResultCode().equals(ResultCode.INVALID_CREDENTIALS)) {
            Result r = ere.getResult();
            if (r != null) {
                // Were there any password policy controls returned?
                PasswordPolicyResult result = checkControls(processControls(r));
                if (result != null) {
                    processPasswordPolicyControls(result);
                }
            }
            setState(ModuleState.PASSWORD_NOT_UPDATE);
        } else {
            setState(ModuleState.PASSWORD_NOT_UPDATE);
        }
        if (debug.warningEnabled()) {
            debug.warning("Cannot update : ", ere);
        }
    } finally {
        if (modConn != null) {
            modConn.close();
        }
    }
}
Also used : PasswordExpiringResponseControl(org.forgerock.opendj.ldap.controls.PasswordExpiringResponseControl) PasswordExpiredResponseControl(org.forgerock.opendj.ldap.controls.PasswordExpiredResponseControl) PasswordPolicyRequestControl(org.forgerock.opendj.ldap.controls.PasswordPolicyRequestControl) Control(org.forgerock.opendj.ldap.controls.Control) PasswordPolicyResponseControl(org.forgerock.opendj.ldap.controls.PasswordPolicyResponseControl) Connection(org.forgerock.opendj.ldap.Connection) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LdapException(org.forgerock.opendj.ldap.LdapException) BindResult(org.forgerock.opendj.ldap.responses.BindResult) Result(org.forgerock.opendj.ldap.responses.Result)

Example 2 with Result

use of org.forgerock.opendj.ldap.responses.Result in project OpenAM by OpenRock.

the class LdapAdapterTest method shouldPerformUpdate.

@Test
public void shouldPerformUpdate() throws Exception {
    // Given
    Token first = new Token("weasel", TokenType.OAUTH);
    Token second = new Token("badger", TokenType.OAUTH);
    Connection mockConnection = mock(Connection.class);
    Result successResult = mockSuccessfulResult();
    given(mockConnection.modify(any(ModifyRequest.class))).willReturn(successResult);
    LdapDataLayerConfiguration config = mock(LdapDataLayerConfiguration.class);
    when(config.getTokenStoreRootSuffix()).thenReturn(DN.valueOf("ou=unit-test"));
    LDAPDataConversion dataConversion = new LDAPDataConversion();
    LdapTokenAttributeConversion conversion = new LdapTokenAttributeConversion(dataConversion, config);
    LdapAdapter adapter = new LdapAdapter(conversion, null, null);
    // When
    adapter.update(mockConnection, first, second);
    // Then
    verify(mockConnection).modify(any(ModifyRequest.class));
}
Also used : LdapTokenAttributeConversion(org.forgerock.openam.cts.utils.LdapTokenAttributeConversion) LdapDataLayerConfiguration(org.forgerock.openam.sm.datalayer.impl.ldap.LdapDataLayerConfiguration) Connection(org.forgerock.opendj.ldap.Connection) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Token(org.forgerock.openam.cts.api.tokens.Token) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LDAPDataConversion(org.forgerock.openam.cts.utils.LDAPDataConversion) Result(org.forgerock.opendj.ldap.responses.Result) Test(org.testng.annotations.Test)

Example 3 with Result

use of org.forgerock.opendj.ldap.responses.Result in project OpenAM by OpenRock.

the class LdapAdapterTest method mockSuccessfulResult.

private static Result mockSuccessfulResult() {
    Result result = mock(Result.class);
    ResultCode resultCode = ResultCode.SUCCESS;
    given(result.getResultCode()).willReturn(resultCode);
    return result;
}
Also used : ResultCode(org.forgerock.opendj.ldap.ResultCode) Result(org.forgerock.opendj.ldap.responses.Result)

Example 4 with Result

use of org.forgerock.opendj.ldap.responses.Result in project OpenAM by OpenRock.

the class LdapQueryBuilder method getEntries.

private Collection<Entry> getEntries(Connection connection) throws CoreTokenException {
    // Prepare the search
    Filter ldapFilter = getLDAPFilter();
    SearchRequest searchRequest = LDAPRequests.newSearchRequest(dataLayerConfiguration.getTokenStoreRootSuffix(), SearchScope.WHOLE_SUBTREE, ldapFilter, requestedAttributes);
    searchRequest.setSizeLimit(sizeLimit);
    if (isPagingResults()) {
        searchRequest = searchRequest.addControl(SimplePagedResultsControl.newControl(true, pageSize, pagingCookie));
    }
    // Perform the search
    Collection<Entry> entries = createResultsList();
    final Result result = handler.performSearch(connection, searchRequest, entries);
    if (isPagingResults()) {
        try {
            SimplePagedResultsControl control = result.getControl(SimplePagedResultsControl.DECODER, new DecodeOptions());
            if (control == null) {
                if (debug.warningEnabled()) {
                    debug.warning("There was no paged result control in the search response, it is recommended to " + "set the CTS user's size-limit at least to " + (pageSize + 1));
                }
                pagingCookie = getEmptyPagingCookie();
            } else {
                pagingCookie = control.getCookie();
            }
        } catch (DecodeException e) {
            throw new CoreTokenException("Failed to decode Paging Cookie", e);
        }
    }
    if (debug.messageEnabled()) {
        debug.message(MessageFormat.format(CoreTokenConstants.DEBUG_HEADER + "Query: matched {0} results\n" + "Search Request: {1}\n" + "Filter: {2}\n" + "Result: {3}", entries.size(), searchRequest, ldapFilter.toString(), result));
    }
    return entries;
}
Also used : SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) Entry(org.forgerock.opendj.ldap.Entry) Filter(org.forgerock.opendj.ldap.Filter) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) SimplePagedResultsControl(org.forgerock.opendj.ldap.controls.SimplePagedResultsControl) DecodeException(org.forgerock.opendj.ldap.DecodeException) DecodeOptions(org.forgerock.opendj.ldap.DecodeOptions) Result(org.forgerock.opendj.ldap.responses.Result)

Example 5 with Result

use of org.forgerock.opendj.ldap.responses.Result in project OpenAM by OpenRock.

the class LdapAdapterTest method shouldUseConnectionForDelete.

@Test
public void shouldUseConnectionForDelete() throws Exception {
    // Given
    String tokenId = "badger";
    DN testDN = DN.rootDN();
    Result successResult = mockSuccessfulResult();
    given(mockConnection.delete(any(DeleteRequest.class))).willReturn(successResult);
    given(mockConversion.generateTokenDN(anyString())).willReturn(testDN);
    // When
    adapter.delete(mockConnection, tokenId);
    // Then
    ArgumentCaptor<DeleteRequest> captor = ArgumentCaptor.forClass(DeleteRequest.class);
    verify(mockConnection).delete(captor.capture());
    assertEquals(testDN, captor.getValue().getName());
}
Also used : DN(org.forgerock.opendj.ldap.DN) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) Result(org.forgerock.opendj.ldap.responses.Result) Test(org.testng.annotations.Test)

Aggregations

Result (org.forgerock.opendj.ldap.responses.Result)8 Test (org.testng.annotations.Test)4 Connection (org.forgerock.opendj.ldap.Connection)3 Token (org.forgerock.openam.cts.api.tokens.Token)2 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)2 Entry (org.forgerock.opendj.ldap.Entry)2 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)2 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)1 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)1 LDAPDataConversion (org.forgerock.openam.cts.utils.LDAPDataConversion)1 LdapTokenAttributeConversion (org.forgerock.openam.cts.utils.LdapTokenAttributeConversion)1 LdapDataLayerConfiguration (org.forgerock.openam.sm.datalayer.impl.ldap.LdapDataLayerConfiguration)1 DN (org.forgerock.opendj.ldap.DN)1 DecodeException (org.forgerock.opendj.ldap.DecodeException)1 DecodeOptions (org.forgerock.opendj.ldap.DecodeOptions)1 Filter (org.forgerock.opendj.ldap.Filter)1 LdapException (org.forgerock.opendj.ldap.LdapException)1