Search in sources :

Example 26 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class TokenTest method shouldStoreDate.

@Test
public void shouldStoreDate() {
    // Given
    Calendar now = Calendar.getInstance();
    CoreTokenField key = CoreTokenField.EXPIRY_DATE;
    Token token = new Token("", TokenType.SESSION);
    // When
    token.setAttribute(key, now);
    // Then
    Calendar result = token.getValue(key);
    assertEquals(now.getTimeInMillis(), result.getTimeInMillis());
}
Also used : Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Test(org.testng.annotations.Test)

Example 27 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField 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 28 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class LdapTokenAttributeConversion method tokenFromEntry.

/**
     * Convert an Entry into a Token.
     *
     * The implication of function is that the Entry contains all the attributes of the
     * Token. If any required attributes are missing, this operation will fail.
     *
     * @param entry A non null Entry.
     *
     * @return A non null Token initialised with the contents of the Entry.
     *
     * @see #mapFromEntry(org.forgerock.opendj.ldap.Entry)
     */
public Token tokenFromEntry(Entry entry) {
    Map<CoreTokenField, Object> map = mapFromEntry(entry);
    String tokenId = (String) map.get(CoreTokenField.TOKEN_ID);
    TokenType type = (TokenType) map.get(CoreTokenField.TOKEN_TYPE);
    Token token = new Token(tokenId, type);
    for (Map.Entry<CoreTokenField, Object> e : map.entrySet()) {
        CoreTokenField key = e.getKey();
        Object value = e.getValue();
        if (Token.isFieldReadOnly(key)) {
            continue;
        }
        token.setAttribute(key, value);
    }
    return token;
}
Also used : TokenType(org.forgerock.openam.tokens.TokenType) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Token(org.forgerock.openam.cts.api.tokens.Token) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 29 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class LdapTokenAttributeConversion method mapFromEntry.

/**
     * Convert an Entry into a more convenient Mapping of CoreTokenField to Object.
     *
     * This function is important because no every operation with LDAP needs to return a
     * fully initialised Token. Instead users may be interested in only certain
     * attributes of the Token, and choose to query just those as a performance enhancement.
     *
     * @param entry Non null entry to convert.
     *
     * @return A mapping of zero or more CoreTokenFields to Objects.
     */
public Map<CoreTokenField, Object> mapFromEntry(Entry entry) {
    stripObjectClass(entry);
    Map<CoreTokenField, Object> r = new LinkedHashMap<>();
    for (Attribute a : entry.getAllAttributes()) {
        AttributeDescription description = a.getAttributeDescription();
        CoreTokenField field = CoreTokenField.fromLDAPAttribute(description.toString());
        // Special case for Token Type
        if (CoreTokenField.TOKEN_TYPE.equals(field)) {
            String value = entry.parseAttribute(description).asString();
            r.put(field, TokenType.valueOf(value));
            continue;
        }
        if (CoreTokenFieldTypes.isCalendar(field)) {
            String dateString = entry.parseAttribute(description).asString();
            Calendar calendar = conversion.fromLDAPDate(dateString);
            r.put(field, calendar);
        } else if (CoreTokenFieldTypes.isString(field)) {
            String value = entry.parseAttribute(description).asString();
            if (EMPTY.equals(value)) {
                value = "";
            }
            r.put(field, value);
        } else if (CoreTokenFieldTypes.isInteger(field)) {
            Integer value = entry.parseAttribute(description).asInteger();
            r.put(field, value);
        } else if (CoreTokenFieldTypes.isByteArray(field)) {
            byte[] data = entry.parseAttribute(description).asByteString().toByteArray();
            r.put(field, data);
        } else {
            throw new IllegalStateException();
        }
    }
    return r;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) LinkedHashMap(java.util.LinkedHashMap) AttributeDescription(org.forgerock.opendj.ldap.AttributeDescription)

Example 30 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class Token method toString.

/**
     * Returns a formatted version fo the Token which is intended to be human readable.
     * Some of the field values have been formatted to ensure that their contents are more
     * meaningful.
     *
     * Note: This is not a machine readable/parsable format. This function is only intended
     * for debugging use only as its conversion logic will not be performant.
     *
     * @return Non null string which describes the Token.
     */
@Override
public String toString() {
    String r = this.getClass().getName() + "\n";
    String format = "\t{0} = {1}\n";
    for (CoreTokenField field : getAttributeNames()) {
        Object value;
        if (field.equals(CoreTokenField.EXPIRY_DATE)) {
            DateFormat dateFormat = DateFormat.getInstance();
            Date date = getExpiryTimestamp().getTime();
            value = dateFormat.format(date);
        } else if (field.equals(CoreTokenField.BLOB)) {
            value = Long.toString(getBlob().length) + " bytes";
        } else {
            value = getValue(field);
        }
        if (value == null) {
            value = "[null]";
        }
        r += MessageFormat.format(format, field.toString(), value);
    }
    return r;
}
Also used : DateFormat(java.text.DateFormat) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Date(java.util.Date)

Aggregations

CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)33 Test (org.testng.annotations.Test)17 Calendar (java.util.Calendar)5 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)4 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)4 QueryFilter (org.forgerock.util.query.QueryFilter)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonValue (org.forgerock.json.JsonValue)3 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)3 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)3 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)3 Token (org.forgerock.openam.cts.api.tokens.Token)3 BadRequestException (org.forgerock.json.resource.BadRequestException)2 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)2 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2