Search in sources :

Example 11 with CoreTokenField

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();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) HashMap(java.util.HashMap) Map(java.util.Map) Query(org.forgerock.json.resource.annotations.Query)

Example 12 with CoreTokenField

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();
}
Also used : QueryFilter(org.forgerock.util.query.QueryFilter) ArrayList(java.util.ArrayList) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField)

Example 13 with CoreTokenField

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");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Method(java.lang.reflect.Method) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Field(org.forgerock.openam.tokens.Field) Converter(org.forgerock.openam.tokens.Converter)

Example 14 with CoreTokenField

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);
}
Also used : CoreTokenField(org.forgerock.openam.tokens.CoreTokenField)

Example 15 with CoreTokenField

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);
}
Also used : CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) HashSet(java.util.HashSet)

Aggregations

CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)33 Test (org.testng.annotations.Test)17 Calendar (java.util.Calendar)5 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)4 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)4 QueryFilter (org.forgerock.util.query.QueryFilter)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonValue (org.forgerock.json.JsonValue)3 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)3 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)3 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)3 Token (org.forgerock.openam.cts.api.tokens.Token)3 BadRequestException (org.forgerock.json.resource.BadRequestException)2 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)2 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2