use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldNoNothingIfNoModificaitonsOnUpdate.
@Test
public void shouldNoNothingIfNoModificaitonsOnUpdate() throws Exception {
// Given
String tokenId = "badger";
Token first = new Token(tokenId, TokenType.OAUTH);
Token second = new Token(tokenId, TokenType.OAUTH);
Connection mockConnection = mock(Connection.class);
LdapDataLayerConfiguration config = mock(LdapDataLayerConfiguration.class);
when(config.getTokenStoreRootSuffix()).thenReturn(DN.valueOf("ou=unit-test"));
LDAPDataConversion dataConversion = new LDAPDataConversion();
LdapTokenAttributeConversion conversion = new LdapTokenAttributeConversion(dataConversion, config);
adapter = new LdapAdapter(conversion, mockQueryVisitor, mockQueryFactory);
// When
adapter.update(mockConnection, first, second);
// Then
verify(mockConnection, never()).modify(any(ModifyRequest.class));
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldQuery.
@Test
public void shouldQuery() throws Exception {
// Given
final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class);
given(mockBuilder.withFilter(any(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
given(mockBuilder.execute(any(Connection.class))).willReturn(Arrays.asList((Collection<Token>) Arrays.asList(new Token("weasel", TokenType.OAUTH))).iterator());
given(mockQueryFactory.createInstance()).willReturn(mockBuilder);
QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class);
given(mockQueryFactory.createFilterConverter()).willReturn(visitor);
given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue());
// When
TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).build();
Collection<Token> result = adapter.query(mockConnection, filter);
// Then
verify(mockBuilder).withFilter(any(Filter.class));
verify(mockBuilder).execute(mockConnection);
assertThat(result.size()).isEqualTo(1);
assertThat(result.iterator().next().getTokenId()).isEqualTo("weasel");
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldPartialQuery.
@Test
public void shouldPartialQuery() throws Exception {
// Given
final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class);
given(mockBuilder.withFilter(any(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
given(mockBuilder.returnTheseAttributes(anySetOf(CoreTokenField.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
PartialToken partialToken = new PartialToken(new HashMap<CoreTokenField, Object>());
given(mockBuilder.executeAttributeQuery(any(Connection.class))).willReturn(Arrays.asList((Collection<PartialToken>) Arrays.asList(partialToken)).iterator());
given(mockQueryFactory.createInstance()).willReturn(mockBuilder);
QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class);
given(mockQueryFactory.createFilterConverter()).willReturn(visitor);
given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue());
// When
TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).returnAttribute(CoreTokenField.STRING_ONE).build();
Collection<PartialToken> result = adapter.partialQuery(mockConnection, filter);
// Then
verify(mockBuilder).withFilter(any(Filter.class));
verify(mockBuilder).returnTheseAttributes(asSet(CoreTokenField.STRING_ONE));
verify(mockBuilder).executeAttributeQuery(mockConnection);
assertThat(result).containsOnly(partialToken);
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method getFilteredRoleMemberships.
/**
* Returns the filtered and non-filtered role memberships for this given user. This will execute a read on the user
* entry to retrieve the nsRole attribute. The values of the attribute will be returned along with the non-filtered
* role memberships.
*
* @param dn The DN of the user identity.
* @return The DNs of the filtered roles this user is member of.
* @throws IdRepoException If there was an error while retrieving the filtered or non-filtered role membership
* information.
*/
private Set<String> getFilteredRoleMemberships(String dn) throws IdRepoException {
Set<String> results = new CaseInsensitiveHashSet();
Connection conn = null;
try {
conn = connectionFactory.getConnection();
SearchResultEntry entry = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn, roleAttr));
Attribute attr = entry.getAttribute(roleAttr);
if (attr != null) {
results.addAll(LDAPUtils.getAttributeValuesAsStringSet(attr));
}
} catch (LdapException ere) {
DEBUG.error("An error occurred while trying to retrieve filtered role memberships for " + dn + " using " + roleAttr + " attribute", ere);
handleErrorResult(ere);
} finally {
IOUtils.closeIfNotNull(conn);
}
results.addAll(getRoleMemberships(dn));
return results;
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method changePassword.
/**
* Changes password for the given identity by binding as the user first (i.e. this is not password reset). In case
* of Active Directory the password will be encoded first. This will issue a DELETE for the old password and an ADD
* for the new password value.
*
* @param token Not used.
* @param type The type of the identity, this should be always USER.
* @param name The name of the identity.
* @param attrName The name of the password attribute, usually "userpassword" or "unicodepwd".
* @param oldPassword The current password of the identity.
* @param newPassword The new password of the idenity.
* @throws IdRepoException If the identity type is invalid, or the entry cannot be found, or some other LDAP error
* occurs while changing the password (like password policy related errors).
*/
@Override
public void changePassword(SSOToken token, IdType type, String name, String attrName, String oldPassword, String newPassword) throws IdRepoException {
if (DEBUG.messageEnabled()) {
DEBUG.message("changePassword invoked");
}
if (!type.equals(IdType.USER)) {
throw new IdRepoUnsupportedOpException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.CHANGE_PASSWORD_ONLY_FOR_USER, new Object[] { CLASS_NAME });
}
String dn = getDN(type, name);
BindRequest bindRequest = LDAPRequests.newSimpleBindRequest(dn, oldPassword.toCharArray());
ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(dn);
byte[] encodedOldPwd = helper.encodePassword(oldPassword);
byte[] encodedNewPwd = helper.encodePassword(newPassword);
modifyRequest.addModification(ModificationType.DELETE, attrName, encodedOldPwd);
modifyRequest.addModification(ModificationType.ADD, attrName, encodedNewPwd);
Connection conn = null;
try {
conn = bindConnectionFactory.getConnection();
conn.bind(bindRequest);
conn.modify(modifyRequest);
} catch (LdapException ere) {
DEBUG.error("An error occurred while trying to change password for identity: " + name, ere);
try {
handleErrorResult(ere);
} catch (IdRepoException e) {
throw new PasswordPolicyException(e);
}
} finally {
IOUtils.closeIfNotNull(conn);
}
}
Aggregations