Search in sources :

Example 66 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class OAuthAdapter method toToken.

/**
     * Convert a JsonValue to a Token.
     *
     * The conversion assumes that the JsonValue contains a map which has an attribute called
     * 'value' which contains the OAuth Token values.
     *
     * The TokenIdFactory is responsible for resolving the primary Id of the Token.
     *
     * Note: OAuth tokens don't have an expiry or user concepts.
     *
     * @param request Non null.
     *
     * @return Non null Token.
     *
     * @throws IllegalArgumentException If the object wrapped inside the JsonValue
     * was not an instance of a Map.
     */
public Token toToken(JsonValue request) {
    assertObjectIsAMap(request);
    Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject();
    String id = null;
    if (idSet != null && !idSet.isEmpty()) {
        id = tokenIdFactory.generateTokenId(idSet.iterator().next());
    } else {
        id = tokenIdFactory.generateTokenId(null);
    }
    request.get(TokenIdFactory.ID).setObject(id);
    Token token = new Token(id, TokenType.OAUTH);
    // For each OAuth attribute, assign it to the token.
    Map<String, Object> values = request.asMap();
    if (values != null) {
        for (OAuthTokenField field : OAuthTokenField.values()) {
            String key = field.getOAuthField();
            if (values.containsKey(key)) {
                Object value = values.get(key);
                /**
                     * OAuthTokenField aware conversions.
                     *
                     * - Skip the ID as it is extracted by the TokenIdFactory.
                     * - Dates are formatted as milliseconds from epoch, and stored in Collections.
                     * - All other fields are stored in Collections which can be empty.
                     * - (just in case) If a field is not in a collection, assume it is the right type.
                     */
                if (OAuthTokenField.ID.getOAuthField().equals(key)) {
                    continue;
                }
                if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) {
                    if (!Collection.class.isAssignableFrom(value.getClass())) {
                        throw new IllegalStateException("Date must be in a collection");
                    }
                    if (isSetToNeverExpire((Collection<String>) value)) {
                        continue;
                    }
                    value = oAuthValues.getDateValue((Collection<String>) value);
                } else if (value instanceof Collection) {
                    value = oAuthValues.getSingleValue((Collection<String>) value);
                }
                token.setAttribute(field.getField(), value);
            }
        }
    }
    /**
         * Binary Data
         * The JsonValue class is unable to parse its own output, therefore we need
         * a suitable mechanism to work around this. In this case we will serialise
         * the object contained within the JsonValue which we know to be a map.
         */
    Object objectToStore = request.getObject();
    String serialisedObject = serialisation.serialise(objectToStore);
    blobUtils.setBlobFromString(token, serialisedObject);
    return token;
}
Also used : OAuthTokenField(org.forgerock.openam.cts.api.fields.OAuthTokenField) Set(java.util.Set) HashSet(java.util.HashSet) Collection(java.util.Collection) Token(org.forgerock.openam.cts.api.tokens.Token)

Example 67 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class TokenTestUtils method generateToken.

public static Token generateToken() {
    String id = RandomStringUtils.randomAlphabetic(20);
    Token token = new Token(id, TokenType.SESSION);
    // Set to expire now.
    token.setExpiryTimestamp(Calendar.getInstance());
    // Some extra data
    token.setAttribute(CoreTokenField.STRING_ONE, RandomStringUtils.randomAlphabetic(20));
    token.setAttribute(CoreTokenField.STRING_TWO, RandomStringUtils.randomAlphabetic(20));
    token.setAttribute(CoreTokenField.STRING_THREE, RandomStringUtils.randomAlphabetic(20));
    token.setAttribute(CoreTokenField.STRING_FOUR, RandomStringUtils.randomAlphabetic(20));
    token.setAttribute(CoreTokenField.STRING_FIVE, RandomStringUtils.randomAlphabetic(20));
    // Some binary data
    byte[] data = RandomStringUtils.randomAlphabetic(100).getBytes();
    token.setBlob(data);
    return token;
}
Also used : Token(org.forgerock.openam.cts.api.tokens.Token)

Example 68 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class TokenTestUtilsTest method shouldFailBecauseTokenTimestampsAreDifferentTimeZones.

@Test(expectedExceptions = AssertionError.class)
public void shouldFailBecauseTokenTimestampsAreDifferentTimeZones() {
    // Given
    Token expected = new Token("", TokenType.SESSION);
    Calendar expectedCal = Calendar.getInstance();
    expectedCal.setTimeZone(LDAPDataConversionTest.CHICAGO);
    expected.setExpiryTimestamp(expectedCal);
    Token result = new Token("", TokenType.SESSION);
    Calendar resultCal = Calendar.getInstance();
    resultCal.setTimeZone(LDAPDataConversionTest.BERLIN);
    result.setExpiryTimestamp(resultCal);
    // When / Then
    TokenTestUtils.assertTokenEquals(result, expected);
}
Also used : Calendar(java.util.Calendar) Token(org.forgerock.openam.cts.api.tokens.Token) LDAPDataConversionTest(org.forgerock.openam.cts.utils.LDAPDataConversionTest) Test(org.testng.annotations.Test)

Example 69 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class JavaBeanAdapterTest method testGenerateId.

@Test
public void testGenerateId() throws Exception {
    //Given
    DummyBean b = new DummyBean();
    //When
    Token t = adapter.toToken(b);
    //Then
    assertThat(b.getId()).matches("[0-9a-f-]{36}");
    assertThat(b.getId()).isEqualTo(t.getTokenId());
}
Also used : Token(org.forgerock.openam.cts.api.tokens.Token) Test(org.testng.annotations.Test)

Example 70 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class JavaBeanAdapterTest method guicedTokenBean.

@Test
public void guicedTokenBean() throws Exception {
    // Given
    JavaBeanAdapter<GuicedBean> guicedBeanAdapter = new JavaBeanAdapter<GuicedBean>(GuicedBean.class, null);
    Token token = new Token("abc123", TokenType.GENERIC);
    token.setAttribute(CoreTokenField.STRING_ONE, "fred");
    // When
    GuicedBean bean = guicedBeanAdapter.fromToken(token);
    // Then
    assertThat(bean.guicedValue).isEqualTo(GUICED_VALUE);
}
Also used : Token(org.forgerock.openam.cts.api.tokens.Token) Test(org.testng.annotations.Test)

Aggregations

Token (org.forgerock.openam.cts.api.tokens.Token)86 Test (org.testng.annotations.Test)58 Task (org.forgerock.openam.sm.datalayer.api.Task)17 ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)16 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)16 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)13 InternalSession (com.iplanet.dpro.session.service.InternalSession)8 DataLayerException (org.forgerock.openam.sm.datalayer.api.DataLayerException)8 Calendar (java.util.Calendar)7 JsonValue (org.forgerock.json.JsonValue)7 SessionID (com.iplanet.dpro.session.SessionID)6 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)6 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 Entry (org.forgerock.opendj.ldap.Entry)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 SAMLToken (org.forgerock.openam.cts.api.tokens.SAMLToken)4 Connection (org.forgerock.opendj.ldap.Connection)4 LinkedHashMapEntry (org.forgerock.opendj.ldap.LinkedHashMapEntry)4