Search in sources :

Example 1 with JSONSerialisation

use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.

the class SAMLAdapterTest method shouldSerialiseAndDeserialiseToken.

@Test
public void shouldSerialiseAndDeserialiseToken() {
    // Given
    // Need real delegates for this test.
    serialisation = new JSONSerialisation(new ObjectMapper());
    adapter = new SAMLAdapter(new TokenIdFactory(encoding), new JSONSerialisation(new ObjectMapper()), new TokenBlobUtils());
    String tokenId = encoding.encodeKey("badger");
    Token token = new Token(tokenId, TokenType.SAML2);
    // SAML tokens only store time to seconds resolution
    Calendar now = Calendar.getInstance();
    now.set(Calendar.MILLISECOND, 0);
    token.setExpiryTimestamp(now);
    // SAML implementation detail around stored object
    String blob = "woodland forrest";
    token.setBlob(serialisation.serialise(blob).getBytes());
    token.setAttribute(SAMLTokenField.OBJECT_CLASS.getField(), String.class.getName());
    // SAML detail for secondary key
    String secondaryKey = encoding.encodeKey("weasel");
    token.setAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey);
    // When
    Token result = adapter.toToken(adapter.fromToken(token));
    // Then
    TokenTestUtils.assertTokenEquals(result, token);
}
Also used : JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) Calendar(java.util.Calendar) TokenIdFactory(org.forgerock.openam.cts.api.tokens.TokenIdFactory) SAMLToken(org.forgerock.openam.cts.api.tokens.SAMLToken) Token(org.forgerock.openam.cts.api.tokens.Token) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TokenBlobUtils(org.forgerock.openam.cts.utils.blob.TokenBlobUtils) Test(org.testng.annotations.Test)

Example 2 with JSONSerialisation

use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.

the class OAuthAdapterTest method shouldDeserialiseSerialisedToken.

@Test
public void shouldDeserialiseSerialisedToken() {
    // Given
    String[] id = { "badger" };
    List<String> list = new ArrayList<String>(Arrays.asList(id));
    OAuthTokenField field = OAuthTokenField.ID;
    JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
    OAuthAdapter adapter = generateOAuthAdapter();
    // Populate a map for serialisation.
    Map<String, Object> values = new HashMap<String, Object>();
    values.put(field.getOAuthField(), list);
    //Map<String, Object> map = new HashMap<String, Object>();
    //map.put(OAuthAdapter.VALUE, values);
    String serialisedObject = serialisation.serialise(values);
    Token token = new Token(id[0], TokenType.OAUTH);
    // Set the serialised binary data
    token.setBlob(serialisedObject.getBytes());
    // When
    JsonValue result = adapter.fromToken(token);
    // Then
    assertNotNull(result);
    assert (result.asMap().get(field.getOAuthField()).toString().contains(id[0]));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) Token(org.forgerock.openam.cts.api.tokens.Token) OAuthTokenField(org.forgerock.openam.cts.api.fields.OAuthTokenField) JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with JSONSerialisation

use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.

the class OAuthAdapterTest method generateOAuthAdapter.

/**
     * @return Makes a standard OAuthAdapter with real dependencies.
     */
private OAuthAdapter generateOAuthAdapter() {
    JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
    KeyConversion keyConversion = new KeyConversion();
    OAuthValues oAuthValues = new OAuthValues();
    TokenBlobUtils blobUtils = new TokenBlobUtils();
    return new OAuthAdapter(new TokenIdFactory(keyConversion), serialisation, oAuthValues, blobUtils);
}
Also used : JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) KeyConversion(org.forgerock.openam.cts.utils.KeyConversion) TokenIdFactory(org.forgerock.openam.cts.api.tokens.TokenIdFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TokenBlobUtils(org.forgerock.openam.cts.utils.blob.TokenBlobUtils)

Example 4 with JSONSerialisation

use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.

the class SessionAdapterTest method shouldRestoreLatestAccessTimeFromAttribute.

@Test
public void shouldRestoreLatestAccessTimeFromAttribute() {
    // Given
    String latestAccessTime = "12345";
    Token token = new Token("badger", TokenType.SESSION);
    token.setAttribute(SessionTokenField.LATEST_ACCESS_TIME.getField(), latestAccessTime);
    // blob contents are missing the latestAccessTime value
    token.setBlob("{\"clientDomain\":null,\"creationTime\":1376307674,\"isISStored\":true,\"maxCachingTime\":3}".getBytes());
    // need a real JSONSerialisation for this test
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE).withIsGetterVisibility(JsonAutoDetect.Visibility.NONE).withSetterVisibility(JsonAutoDetect.Visibility.NONE).withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    JSONSerialisation serialisation = new JSONSerialisation(mapper);
    adapter = new SessionAdapter(tokenIdFactory, coreTokenConfig, serialisation, blobUtils);
    // When
    InternalSession session = adapter.fromToken(token);
    // Then
    // if latestAccessTime was zero, this would fail
    long epochedSeconds = System.currentTimeMillis() / 1000;
    long idleTime = session.getIdleTime();
    assertTrue(idleTime < epochedSeconds);
}
Also used : JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) InternalSession(com.iplanet.dpro.session.service.InternalSession) Token(org.forgerock.openam.cts.api.tokens.Token) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 5 with JSONSerialisation

use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.

the class OAuthAdapterTest method shouldNotDeserialiseATokenWhichDoesntContainAMap.

@Test
public void shouldNotDeserialiseATokenWhichDoesntContainAMap() {
    // Given
    JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
    OAuthAdapter adapter = generateOAuthAdapter();
    Token token = new Token("", TokenType.OAUTH);
    token.setBlob(serialisation.serialise("badger").getBytes());
    // When
    JsonValue fromToken = adapter.fromToken(token);
    // Then
    assertThat(fromToken).isNull();
}
Also used : JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) JsonValue(org.forgerock.json.JsonValue) Token(org.forgerock.openam.cts.api.tokens.Token) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

JSONSerialisation (org.forgerock.openam.cts.utils.JSONSerialisation)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Token (org.forgerock.openam.cts.api.tokens.Token)4 Test (org.testng.annotations.Test)4 TokenIdFactory (org.forgerock.openam.cts.api.tokens.TokenIdFactory)3 TokenBlobUtils (org.forgerock.openam.cts.utils.blob.TokenBlobUtils)3 JsonValue (org.forgerock.json.JsonValue)2 InternalSession (com.iplanet.dpro.session.service.InternalSession)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 CoreTokenConfig (org.forgerock.openam.cts.CoreTokenConfig)1 OAuthTokenField (org.forgerock.openam.cts.api.fields.OAuthTokenField)1 SAMLToken (org.forgerock.openam.cts.api.tokens.SAMLToken)1 KeyConversion (org.forgerock.openam.cts.utils.KeyConversion)1 BeforeMethod (org.testng.annotations.BeforeMethod)1