Search in sources :

Example 61 with Connection

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));
}
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) Test(org.testng.annotations.Test)

Example 62 with Connection

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");
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Token(org.forgerock.openam.cts.api.tokens.Token) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) QueryBuilder(org.forgerock.openam.sm.datalayer.api.query.QueryBuilder) Filter(org.forgerock.opendj.ldap.Filter) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) QueryFilter(org.forgerock.util.query.QueryFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 63 with Connection

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);
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Connection(org.forgerock.opendj.ldap.Connection) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) QueryBuilder(org.forgerock.openam.sm.datalayer.api.query.QueryBuilder) Filter(org.forgerock.opendj.ldap.Filter) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) QueryFilter(org.forgerock.util.query.QueryFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 64 with Connection

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;
}
Also used : CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) Attribute(org.forgerock.opendj.ldap.Attribute) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) LdapException(org.forgerock.opendj.ldap.LdapException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 65 with Connection

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);
    }
}
Also used : IdRepoUnsupportedOpException(com.sun.identity.idm.IdRepoUnsupportedOpException) PasswordPolicyException(com.sun.identity.idm.PasswordPolicyException) BindRequest(org.forgerock.opendj.ldap.requests.BindRequest) Connection(org.forgerock.opendj.ldap.Connection) IdRepoException(com.sun.identity.idm.IdRepoException) ByteString(org.forgerock.opendj.ldap.ByteString) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LdapException(org.forgerock.opendj.ldap.LdapException)

Aggregations

Connection (org.forgerock.opendj.ldap.Connection)88 LdapException (org.forgerock.opendj.ldap.LdapException)70 ByteString (org.forgerock.opendj.ldap.ByteString)45 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)42 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)35 ResultCode (org.forgerock.opendj.ldap.ResultCode)29 Attribute (org.forgerock.opendj.ldap.Attribute)25 HashSet (java.util.HashSet)23 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)20 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)17 IOException (java.io.IOException)16 SSOException (com.iplanet.sso.SSOException)15 PolicyException (com.sun.identity.policy.PolicyException)14 SMSException (com.sun.identity.sm.SMSException)13 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)12 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)11 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)10 InvalidNameException (com.sun.identity.policy.InvalidNameException)10 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)10 LinkedHashSet (java.util.LinkedHashSet)10