use of org.forgerock.openam.cts.api.fields.OAuthTokenField 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]));
}
use of org.forgerock.openam.cts.api.fields.OAuthTokenField in project OpenAM by OpenRock.
the class OAuthAdapterTest method shouldSerialiseACollectionOfTimestamps.
@Test
public void shouldSerialiseACollectionOfTimestamps() {
// Given
OAuthAdapter adapter = generateOAuthAdapter();
String timestamp = "1370425721197";
OAuthTokenField field = OAuthTokenField.EXPIRY_TIME;
Map<String, Object> values = new HashMap<String, Object>();
values.put(field.getOAuthField(), Arrays.asList(new String[] { timestamp }));
JsonValue jsonValue = makeDefaultJsonValue(values);
// When
Calendar result = adapter.toToken(jsonValue).getExpiryTimestamp();
// Then
// The result timezone is set to local time.
// Convert this timestamp to a known timezone.
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
c.setTimeInMillis(result.getTimeInMillis());
// Wed, 05 Jun 2013 10:48:41 BST
assertEquals(c.get(Calendar.MONTH), 5);
assertEquals(c.get(Calendar.YEAR), 2013);
assertEquals(c.get(Calendar.HOUR_OF_DAY), 10);
assertEquals(c.get(Calendar.MINUTE), 48);
}
use of org.forgerock.openam.cts.api.fields.OAuthTokenField in project OpenAM by OpenRock.
the class OAuthAdapterTest method shouldSerialiseSimpleString.
@Test
public void shouldSerialiseSimpleString() {
// Given
OAuthAdapter adapter = generateOAuthAdapter();
Set<String> text = new HashSet<String>();
text.add("badger");
OAuthTokenField field = OAuthTokenField.PARENT;
Map<String, Object> values = new HashMap<String, Object>();
values.put(field.getOAuthField(), text);
JsonValue jsonValue = makeDefaultJsonValue(values);
// When
Token result = adapter.toToken(jsonValue);
// Then
assert (result.getValue(field.getField()).toString().contains("badger"));
}
use of org.forgerock.openam.cts.api.fields.OAuthTokenField 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.fields.OAuthTokenField in project OpenAM by OpenRock.
the class OAuthAdapterTest method shouldSerialiseCollectionOfStrings.
@Test
public void shouldSerialiseCollectionOfStrings() {
// Given
OAuthAdapter adapter = generateOAuthAdapter();
String one = "badger";
String two = "weasel";
String three = "ferret";
OAuthTokenField field = OAuthTokenField.SCOPE;
Map<String, Object> values = new HashMap<String, Object>();
values.put(field.getOAuthField(), Arrays.asList(new String[] { one, two, three }));
JsonValue jsonValue = makeDefaultJsonValue(values);
// When
String result = adapter.toToken(jsonValue).getValue(field.getField());
// Then
assertTrue(result.contains(one));
assertTrue(result.contains(two));
assertTrue(result.contains(three));
}
Aggregations