use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class OAuth2UserApplications method query.
/**
* Allows users to query OAuth2 applications that they have given their consent access to and that have active
* access and/or refresh tokens.
*
* <p>Applications consist of an id, a name (the client id), a set of scopes and an expiry time. The scopes field
* is the union of the scopes of the individual access/refresh tokens. The expiry time is the time when the last
* access/refresh token will expire, or null if the server is configured to allow tokens to be refreshed
* indefinitely.</p>
*
* @param context The request context.
* @param queryHandler The query handler.
* @param request Unused but necessary for used of the {@link @Query} annotation.
* @return A promise of a query response.
*/
@Query
public Promise<QueryResponse, ResourceException> query(Context context, QueryResourceHandler queryHandler, QueryRequest request) {
String userId = contextHelper.getUserId(context);
String realm = contextHelper.getRealm(context);
try {
QueryFilter<CoreTokenField> queryFilter = getQueryFilter(userId, realm);
JsonValue tokens = tokenStore.query(queryFilter);
Map<String, Set<JsonValue>> applicationTokensMap = new HashMap<>();
for (JsonValue token : tokens) {
String clientId = getAttributeValue(token, CLIENT_ID.getOAuthField());
Set<JsonValue> applicationTokens = applicationTokensMap.get(clientId);
if (applicationTokens == null) {
applicationTokens = new HashSet<>();
applicationTokensMap.put(clientId, applicationTokens);
}
applicationTokens.add(token);
}
for (Map.Entry<String, Set<JsonValue>> applicationTokens : applicationTokensMap.entrySet()) {
ResourceResponse resource = getResourceResponse(context, applicationTokens.getKey(), applicationTokens.getValue());
queryHandler.handleResource(resource);
}
return Promises.newResultPromise(Responses.newQueryResponse());
} catch (CoreTokenException | ServerException | InvalidClientException | NotFoundException e) {
debug.message("Failed to query OAuth2 clients for user {}", userId, e);
return new InternalServerErrorException(e).asPromise();
} catch (InternalServerErrorException e) {
debug.message("Failed to query OAuth2 clients for user {}", userId, e);
return e.asPromise();
}
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class JavaBeanAdapter method toTokenQuery.
/**
* Use the bean mappings that have been parsed to turn a query keyed by bean property names into a query keyed by
* token property names.
* @param filter The query keyed by bean property names.
* @return The transformed query keyed by token field names.
*/
public TokenFilter toTokenQuery(QueryFilter<String> filter) {
TokenFilterBuilder builder = new TokenFilterBuilder();
List<QueryFilter<CoreTokenField>> tokenFilter = new ArrayList<QueryFilter<CoreTokenField>>();
tokenFilter.add(filter.accept(TOKEN_QUERY_TRANSLATOR, null));
tokenFilter.add(QueryFilter.equalTo(CoreTokenField.TOKEN_TYPE, tokenType));
return builder.withQuery(QueryFilter.and(tokenFilter)).build();
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class JavaBeanAdapter method initialise.
/**
* Process the annotations on the bean class, and throw exceptions for invalid configuration.
*/
private void initialise() {
this.tokenType = beanClass.getAnnotation(Type.class).value();
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(beanClass);
} catch (IntrospectionException e) {
throw new IllegalStateException("Could not introspect type " + beanClass.getName(), e);
}
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
Method readMethod = pd.getReadMethod();
Field f = readMethod.getAnnotation(Field.class);
Method writeMethod = pd.getWriteMethod();
if (f == null) {
f = writeMethod.getAnnotation(Field.class);
}
if (f == null) {
try {
java.lang.reflect.Field field = beanClass.getDeclaredField(pd.getName());
f = field.getAnnotation(Field.class);
} catch (NoSuchFieldException e) {
// fine - field isn't for storage in CTS.
}
}
if (f != null) {
CoreTokenField tokenField = f.field();
Class<?> attributeType = tokenField.getAttributeType();
Class<?> beanFieldType = readMethod.getReturnType();
Class<? extends Converter> converterType = f.converter();
if (converterType.equals(Converter.IdentityConverter.class) && !beanFieldType.equals(attributeType)) {
throw new IllegalStateException("Field " + pd.getDisplayName() + " does not have a compatible type" + "and does not declare a converter");
}
validateConverterType(attributeType, beanFieldType, converterType);
Converter converter = InjectorHolder.getInstance(converterType);
boolean generated = f.generated();
FieldDetails field = new FieldDetails(tokenField, readMethod, writeMethod, converter, generated);
if (tokenField == CoreTokenField.TOKEN_ID) {
idField = field;
} else {
if (generated) {
throw new IllegalStateException("Non-id values cannot be generated: " + f.toString());
}
fields.add(field);
}
fieldsMap.put(pd.getName(), field);
}
}
}
if (idField == null) {
throw new IllegalStateException("The bean class does not declare an ID field");
}
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class TokenFilter method toString.
/**
* A multi-line representation of the filter.
* @return Non null.
*/
public String toString() {
StringBuilder a = new StringBuilder();
String separator = ",";
for (CoreTokenField field : getReturnFields()) {
a.append(field.toString()).append(separator);
}
return MessageFormat.format("TokenFilter: Filter: [{0}] Attributes: {1}", query, a);
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class QueryBuilder method returnTheseAttributes.
/**
* Limit the search to return only the named attributes.
*
* @param returnFields Array of CoreTokenField which are required in the results.
* @return The QueryBuilder instance.
* @throws IllegalArgumentException If array was null or empty.
*/
public QueryBuilder<C, F> returnTheseAttributes(CoreTokenField... returnFields) {
Reject.ifTrue(returnFields == null || returnFields.length == 0);
Set<String> attributes = new HashSet<String>();
for (CoreTokenField field : returnFields) {
attributes.add(field.toString());
}
return setReturnAttributes(attributes);
}
Aggregations