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();
}
}
}
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));
}
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;
}
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;
}
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());
}
Aggregations