Search in sources :

Example 61 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class AMTokenParserImpl method getSessionFromAuthNResponse.

@Override
public String getSessionFromAuthNResponse(String authNResponse) throws TokenValidationException {
    JsonValue responseJson;
    try {
        responseJson = JsonValueBuilder.toJsonValue(authNResponse);
    } catch (JsonException e) {
        String message = "Exception caught getting the text of the json authN response: " + e;
        throw new TokenValidationException(ResourceException.INTERNAL_ERROR, message, e);
    }
    JsonValue sessionIdJsonValue = responseJson.get(TOKEN_ID);
    if (!sessionIdJsonValue.isString()) {
        String message = "REST authN response does not contain " + TOKEN_ID + " string entry. The obtained entry: " + sessionIdJsonValue.toString() + "; The response: " + responseJson.toString();
        throw new TokenValidationException(ResourceException.INTERNAL_ERROR, message);
    }
    return sessionIdJsonValue.asString();
}
Also used : JsonException(org.forgerock.json.JsonException) JsonValue(org.forgerock.json.JsonValue) TokenValidationException(org.forgerock.openam.sts.TokenValidationException)

Example 62 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class DecisionCombinersResource method queryCollection.

/**
     * {@inheritDoc}
     */
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
    final Set<String> combinerTypeNames = new TreeSet<>();
    List<ResourceResponse> combinerTypes = new ArrayList<>();
    final String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
    combinerTypeNames.addAll(entitlementRegistry.getCombinersShortNames());
    for (String combinerTypeName : combinerTypeNames) {
        final Class<? extends EntitlementCombiner> conditionClass = entitlementRegistry.getCombinerType(combinerTypeName);
        if (conditionClass == null) {
            if (debug.warningEnabled()) {
                debug.warning("DecisionCombinersResource :: QUERY by " + principalName + ": Listed combiner short name not found: " + combinerTypeName);
            }
            continue;
        }
        final JsonValue json = jsonify(combinerTypeName);
        if (json != null) {
            if (json != null) {
                String id = json.get(JSON_OBJ_TITLE).asString();
                combinerTypes.add(newResourceResponse(id, null, json));
            }
        }
    }
    QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
    return QueryResponsePresentation.perform(handler, request, combinerTypes);
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue)

Example 63 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class SubjectTypesResource method readInstance.

/**
     * {@inheritDoc}
     *
     * Uses the {@link EntitlementRegistry} to locate the {@link EntitlementSubject} to return.
     */
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
    final Class<? extends EntitlementSubject> subjectClass = entitlementRegistry.getSubjectType(resourceId);
    final String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
    if (subjectClass == null) {
        if (debug.errorEnabled()) {
            debug.error("SubjectTypesResource :: READ by " + principalName + "Requested subject short name not found: " + resourceId);
        }
        return new NotFoundException().asPromise();
    }
    final JsonValue json = jsonify(subjectClass, resourceId, LogicalSubject.class.isAssignableFrom(subjectClass));
    final ResourceResponse resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), json);
    return newResultPromise(resource);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException)

Example 64 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class ApplicationV1Filter method filterUpdate.

/**
     * Update expects the application json to contain both actions and resources; these attributes are part of the old
     * json definition for an application. It also expects that the mentioned application exists with exactly one
     * resource type - no resource types or many resource types is not acceptable, else it is impossible to determine
     * which resource type applies to the set of actions and resources being passed as part of the application json.
     * <p/>
     * Changes to the actions and/or resources will be reflected in the applications associated resource type.
     *
     * @param context
     *         the filter chain context
     * @param request
     *         the update request
     * @param next
     *         a request handler representing the remainder of the filter chain
     */
@Override
public Promise<ResourceResponse, ResourceException> filterUpdate(final Context context, final UpdateRequest request, final RequestHandler next) {
    final JsonValue jsonValue = request.getContent();
    final Map<String, Boolean> actions = jsonValue.get(ACTIONS).asMap(Boolean.class);
    final Set<String> resources = jsonValue.get(RESOURCES).asSet(String.class);
    final String bodyRealm = jsonValue.get(REALM).asString();
    final String pathRealm = contextHelper.getRealm(context);
    if (actions == null) {
        return new BadRequestException("Invalid actions defined in request").asPromise();
    }
    if (resources == null) {
        return new BadRequestException("Invalid resources defined in request").asPromise();
    }
    if (!pathRealm.equals(bodyRealm)) {
        return resourceErrorHandler.handleError(context, request, new EntitlementException(EntitlementException.INVALID_APP_REALM, new String[] { bodyRealm, pathRealm })).asPromise();
    }
    final Subject callingSubject = contextHelper.getSubject(context);
    final String applicationName = request.getResourcePath();
    try {
        final ApplicationService applicationService = applicationServiceFactory.create(callingSubject, pathRealm);
        final Application application = applicationService.getApplication(applicationName);
        if (application == null) {
            return new NotFoundException("Unable to find application " + applicationName).asPromise();
        }
        if (application.getResourceTypeUuids().size() != 1) {
            return new BadRequestException("Cannot modify application with more than one " + "resource type using version 1.0 of this endpoint").asPromise();
        }
        // Retrieve the resource type from the applications single resource type.
        final String resourceTypeUuid = application.getResourceTypeUuids().iterator().next();
        ResourceType resourceType = resourceTypeService.getResourceType(callingSubject, pathRealm, resourceTypeUuid);
        boolean resourceTypeModified = false;
        if (!actions.equals(resourceType.getActions())) {
            resourceTypeModified = true;
            resourceType = resourceType.populatedBuilder().setActions(actions).build();
        }
        if (!resources.equals(resourceType.getPatterns())) {
            resourceTypeModified = true;
            resourceType = resourceType.populatedBuilder().setPatterns(resources).build();
        }
        if (resourceTypeModified) {
            resourceTypeService.updateResourceType(callingSubject, pathRealm, resourceType);
        }
        // Ensure the resource type UUID isn't lost.
        jsonValue.put(RESOURCE_TYPE_UUIDS, new HashSet<String>(Arrays.asList(resourceTypeUuid)));
    } catch (EntitlementException eE) {
        debug.error("Error filtering application update CREST request", eE);
        return resourceErrorHandler.handleError(context, request, eE).asPromise();
    }
    // Forward onto next handler.
    return applicationTransformer.transform(next.handleUpdate(context, request), context);
}
Also used : JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject) EntitlementException(com.sun.identity.entitlement.EntitlementException) BadRequestException(org.forgerock.json.resource.BadRequestException) Application(com.sun.identity.entitlement.Application) ApplicationService(org.forgerock.openam.entitlement.service.ApplicationService)

Example 65 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class PolicyV1Filter method filterCreate.

/**
     * The policy json will not have any resource type defined. Create retrieves the policy's associated application
     * and uses the applications associated resource type for the policy.
     *
     * @param context
     *         the filter chain context
     * @param request
     *         the create request
     * @param next
     *         a request handler representing the remainder of the filter chain
     */
@Override
public Promise<ResourceResponse, ResourceException> filterCreate(Context context, CreateRequest request, RequestHandler next) {
    try {
        final JsonValue jsonValue = request.getContent();
        final Subject callingSubject = contextHelper.getSubject(context);
        final String realm = contextHelper.getRealm(context);
        retrieveResourceType(jsonValue, callingSubject, realm);
    } catch (EntitlementException eE) {
        debug.error("Error filtering policy create CREST request", eE);
        return resourceErrorHandler.handleError(context, request, eE).asPromise();
    } catch (ResourceException rE) {
        debug.error("Error filtering policy create CREST request", rE);
        return rE.asPromise();
    }
    return transform(next.handleCreate(context, request));
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32