Search in sources :

Example 1 with LinkedHashMapEntry

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

the class LdapTokenAttributeConversionTest method shouldUnderstandEmptyStrings.

@Test
public void shouldUnderstandEmptyStrings() {
    // Given
    Entry entry = new LinkedHashMapEntry();
    entry.addAttribute(CoreTokenField.TOKEN_ID.toString(), "id");
    entry.addAttribute(CoreTokenField.TOKEN_TYPE.toString(), TokenType.OAUTH.toString());
    entry.addAttribute(CoreTokenField.STRING_ONE.toString(), LdapTokenAttributeConversion.EMPTY);
    LdapTokenAttributeConversion conversion = generateTokenAttributeConversion();
    // When
    Token result = conversion.tokenFromEntry(entry);
    // Then
    String string = result.getValue(CoreTokenField.STRING_ONE);
    assertTrue(string.isEmpty());
}
Also used : LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Token(org.forgerock.openam.cts.api.tokens.Token) Test(org.testng.annotations.Test)

Example 2 with LinkedHashMapEntry

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

the class DJLDAPv3Repo method create.

/**
     * Creates a new identity using the passed in attributes. The following steps will be performed with the passed in
     * data:
     * <ul>
     *  <li>The password will be encoded in case we are dealing with AD.</li>
     *  <li>If the attribute map contains the default status attribute, then it will be converted to the status values
     *      specified in the configuration.</li>
     *  <li>Performing creation attribute mapping, so certain attributes can have default values (coming from other
     *      attributes, or from the identity name if there is no mapping for the attribute).</li>
     *  <li>Removes all attributes that are not defined in the configuration.</li>
     * </ul>
     * If the default group member setting is being used and a new group identity is being created, the newly created
     * group will also have the default group member assigned.
     *
     * @param token Not used.
     * @param type The type of the identity.
     * @param name The name of the identity.
     * @param attrMap The attributes of the new identity, that needs to be stored.
     * @return The DN of the newly created identity
     * @throws IdRepoException If there is an error while creating the new identity, or if it's a group and there is a
     * problem while adding the default group member.
     */
@Override
public String create(SSOToken token, IdType type, String name, Map<String, Set<String>> attrMap) throws IdRepoException {
    if (DEBUG.messageEnabled()) {
        DEBUG.message("Create invoked on " + type + ": " + name + " attrMap = " + IdRepoUtils.getAttrMapWithoutPasswordAttrs(attrMap, null));
    }
    String dn = generateDN(type, name);
    Set<String> objectClasses = getObjectClasses(type);
    //First we should make sure that we wrap the attributes with a case insensitive hashmap.
    attrMap = new CaseInsensitiveHashMap(attrMap);
    byte[] encodedPwd = helper.encodePassword(type, attrMap.get(AD_UNICODE_PWD_ATTR));
    //Let's set the userstatus as it is configured in the datastore.
    mapUserStatus(type, attrMap);
    //In case some attributes are missing use the create attribute mapping to get those values.
    mapCreationAttributes(type, name, attrMap);
    //and lastly we should make sure that we get rid of the attributes that are not known by the datastore.
    attrMap = removeUndefinedAttributes(type, attrMap);
    Set<String> ocs = attrMap.get(OBJECT_CLASS_ATTR);
    if (ocs != null) {
        ocs.addAll(objectClasses);
    } else {
        attrMap.put(OBJECT_CLASS_ATTR, objectClasses);
    }
    attrMap.put(getSearchAttribute(type), asSet(name));
    Entry entry = new LinkedHashMapEntry(dn);
    Set<String> attributeValue;
    for (Map.Entry<String, Set<String>> attr : attrMap.entrySet()) {
        // Add only attributes whose values are not empty or null
        attributeValue = attr.getValue();
        if (attributeValue != null && !attributeValue.isEmpty()) {
            entry.addAttribute(attr.getKey(), attributeValue.toArray());
        }
    }
    if (type.equals(IdType.GROUP) && defaultGroupMember != null) {
        entry.addAttribute(uniqueMemberAttr, defaultGroupMember);
    }
    if (encodedPwd != null) {
        entry.replaceAttribute(AD_UNICODE_PWD_ATTR, encodedPwd);
    }
    Connection conn = null;
    try {
        conn = connectionFactory.getConnection();
        conn.add(LDAPRequests.newAddRequest(entry));
        if (type.equals(IdType.GROUP) && defaultGroupMember != null) {
            if (memberOfAttr != null) {
                ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(defaultGroupMember);
                modifyRequest.addModification(ModificationType.ADD, memberOfAttr, dn);
                conn.modify(modifyRequest);
            }
        }
    } catch (LdapException ere) {
        DEBUG.error("Unable to add a new entry: " + name + " attrMap: " + IdRepoUtils.getAttrMapWithoutPasswordAttrs(attrMap, null), ere);
        if (ResultCode.ENTRY_ALREADY_EXISTS.equals(ere.getResult().getResultCode())) {
            throw IdRepoDuplicateObjectException.nameAlreadyExists(name);
        } else {
            handleErrorResult(ere);
        }
    } finally {
        IOUtils.closeIfNotNull(conn);
    }
    return dn;
}
Also used : SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) Set(java.util.Set) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CollectionUtils.asSet(org.forgerock.openam.utils.CollectionUtils.asSet) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) Map(java.util.Map) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap) LdapException(org.forgerock.opendj.ldap.LdapException) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap)

Example 3 with LinkedHashMapEntry

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

the class LdapTokenAttributeConversion method getEntry.

/**
     * Generate an Entry based on the given Token.
     *
     * @param token Non null Token to base the Entry on.
     *
     * @return An Entry suitable for LDAP operations. Includes the Object Class.
     */
public Entry getEntry(Token token) {
    Entry entry = new LinkedHashMapEntry(generateTokenDN(token));
    addObjectClass(entry);
    for (CoreTokenField field : token.getAttributeNames()) {
        String key = field.toString();
        // Token Type special case is an Enum
        if (CoreTokenField.TOKEN_TYPE.equals(field)) {
            TokenType type = token.getValue(field);
            entry.addAttribute(key, type.name());
            continue;
        }
        if (CoreTokenFieldTypes.isCalendar(field)) {
            Calendar calendar = token.getValue(field);
            String dateString = conversion.toLDAPDate(calendar);
            entry.addAttribute(key, dateString);
        } else if (CoreTokenFieldTypes.isByteArray(field)) {
            byte[] array = token.getValue(field);
            entry.addAttribute(key, array);
        } else if (CoreTokenFieldTypes.isInteger(field)) {
            Integer value = token.getValue(field);
            entry.addAttribute(key, value);
        } else if (CoreTokenFieldTypes.isString(field)) {
            String value = token.getValue(field);
            if (!value.isEmpty()) {
                entry.addAttribute(key, value);
            }
        } else {
            throw new IllegalStateException();
        }
    }
    return entry;
}
Also used : LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) TokenType(org.forgerock.openam.tokens.TokenType) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField)

Example 4 with LinkedHashMapEntry

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

the class LdapQueryBuilderTest method shouldReturnTokensFromSearch.

@Test
public void shouldReturnTokensFromSearch() throws CoreTokenException {
    // Given
    final Collection<Entry> entries = new LinkedList<Entry>();
    entries.add(new LinkedHashMapEntry());
    entries.add(new LinkedHashMapEntry());
    // Slightly more fiddly mocking to provide behaviour when the mock is called.
    given(searchHandler.performSearch(any(Connection.class), any(SearchRequest.class), any(Collection.class))).will(new Answer() {

        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Collection<Entry> list = (Collection<Entry>) invocationOnMock.getArguments()[2];
            list.addAll(entries);
            return null;
        }
    });
    // Ensure that the Token Conversion returns a Token
    given(tokenEntryConverter.convert(any(Entry.class), any(String[].class))).willReturn(new Token(Long.toString(System.currentTimeMillis()), TokenType.SESSION));
    // When
    Iterator<Collection<Token>> results = builder.execute(mockConnection);
    // Then
    verifyZeroInteractions(tokenEntryConverter);
    assertThat(results.next().size()).isEqualTo(entries.size());
    verify(tokenEntryConverter, times(2)).convert(any(Entry.class), any(String[].class));
}
Also used : SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) Connection(org.forgerock.opendj.ldap.Connection) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Token(org.forgerock.openam.cts.api.tokens.Token) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) Test(org.testng.annotations.Test)

Aggregations

Entry (org.forgerock.opendj.ldap.Entry)4 LinkedHashMapEntry (org.forgerock.opendj.ldap.LinkedHashMapEntry)4 Token (org.forgerock.openam.cts.api.tokens.Token)2 Connection (org.forgerock.opendj.ldap.Connection)2 Test (org.testng.annotations.Test)2 CaseInsensitiveHashMap (com.sun.identity.common.CaseInsensitiveHashMap)1 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)1 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)1 TokenType (org.forgerock.openam.tokens.TokenType)1 CollectionUtils.asSet (org.forgerock.openam.utils.CollectionUtils.asSet)1 ByteString (org.forgerock.opendj.ldap.ByteString)1