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;
}
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;
}
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);
}
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());
}
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);
}
Aggregations