Search in sources :

Example 31 with JsonPointer

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

the class SoapSTSPublishServiceRequestHandler method handleQuery.

public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) {
    QueryFilter<JsonPointer> queryFilter = request.getQueryFilter();
    if (queryFilter == null) {
        return new BadRequestException(getQueryUsageString()).asPromise();
    }
    String realm;
    try {
        realm = getRealmFromQueryFilter(queryFilter);
    } catch (STSPublishException e) {
        return e.asPromise();
    }
    try {
        if (!realmValidator.isRealm(realm)) {
            return new BadRequestException("The specified realm does not exist.").asPromise();
        }
        final List<SoapSTSInstanceConfig> publishedInstances = publisher.getPublishedInstances(realm);
        for (SoapSTSInstanceConfig instanceConfig : publishedInstances) {
            /*
                Although instanceConfig.toJson() will yield the JsonValue which the handleResource invocation requires,
                the SoapSTSInstanceConfig is a complicated nesting of JsonValue objects, which should be 'homogenized'
                into a json format prior to inclusion in the response.
                 */
            handler.handleResource(newResourceResponse(instanceConfig.getDeploymentSubPath(), getInstanceConfigEtag(instanceConfig), new JsonValue(mapStringToJson(instanceConfig.toJson().toString()))));
        }
        return newResultPromise(newQueryResponse());
    } catch (STSPublishException e) {
        logger.error("Exception caught obtaining soap sts instances for realm " + (realm != null ? realm : "null realm") + "; Exception: " + e);
        return e.asPromise();
    }
}
Also used : SoapSTSInstanceConfig(org.forgerock.openam.sts.soap.config.user.SoapSTSInstanceConfig) STSPublishException(org.forgerock.openam.sts.STSPublishException) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) JsonPointer(org.forgerock.json.JsonPointer)

Example 32 with JsonPointer

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

the class TokenGenerationService method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(final Context serverContext, final QueryRequest queryRequest, final QueryResourceHandler queryResultHandler) {
    QueryFilter<JsonPointer> queryFilter = queryRequest.getQueryFilter();
    if (queryFilter == null) {
        return new BadRequestException(getUsageString()).asPromise();
    }
    try {
        final QueryFilter<CoreTokenField> coreTokenFieldQueryFilter = convertToCoreTokenFieldQueryFilter(queryFilter);
        final List<STSIssuedTokenState> issuedTokens = ctsTokenPersistence.listTokens(coreTokenFieldQueryFilter);
        for (STSIssuedTokenState tokenState : issuedTokens) {
            queryResultHandler.handleResource(newResourceResponse(tokenState.getTokenId(), EMPTY_STRING, tokenState.toJson()));
        }
        return newResultPromise(newQueryResponse());
    } catch (CTSTokenPersistenceException e) {
        logger.error("Exception caught obtaining list of sts-issued tokens: " + e, e);
        return e.asPromise();
    }
}
Also used : BadRequestException(org.forgerock.json.resource.BadRequestException) STSIssuedTokenState(org.forgerock.openam.sts.user.invocation.STSIssuedTokenState) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) JsonPointer(org.forgerock.json.JsonPointer) CTSTokenPersistenceException(org.forgerock.openam.sts.CTSTokenPersistenceException)

Example 33 with JsonPointer

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

the class UmaPolicyApplicationListener method deletePolicies.

private void deletePolicies(String realm, String resourceServerId) {
    RealmContext realmContext = new RealmContext(new RootContext());
    realmContext.setDnsAlias("/", realm);
    final Context context = new AdminSubjectContext(logger, sessionCache, realmContext);
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), resourceServerId));
    final List<ResourceResponse> resources = new ArrayList<>();
    policyResource.handleQuery(context, request, new QueryResourceHandler() {

        @Override
        public boolean handleResource(ResourceResponse resource) {
            resources.add(resource);
            return true;
        }
    }).thenAsync(new AsyncFunction<QueryResponse, List<ResourceResponse>, ResourceException>() {

        @Override
        public Promise<List<ResourceResponse>, ResourceException> apply(QueryResponse response) {
            List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
            for (ResourceResponse policy : resources) {
                DeleteRequest deleteRequest = Requests.newDeleteRequest("", policy.getId());
                promises.add(policyResource.handleDelete(context, deleteRequest));
            }
            Promise<List<ResourceResponse>, ResourceException> when = Promises.when(promises);
            return when;
        }
    }).thenOnException(new ExceptionHandler<ResourceException>() {

        @Override
        public void handleException(ResourceException error) {
            logger.error(error.getReason());
        }
    });
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) Context(org.forgerock.services.context.Context) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) ArrayList(java.util.ArrayList) JsonPointer(org.forgerock.json.JsonPointer) AsyncFunction(org.forgerock.util.AsyncFunction) RootContext(org.forgerock.services.context.RootContext) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) DenyOverride(com.sun.identity.entitlement.DenyOverride) DeleteRequest(org.forgerock.json.resource.DeleteRequest)

Example 34 with JsonPointer

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

the class UmaPolicyServiceImpl method internalReadPolicy.

/**
     * {@inheritDoc}
     */
private Promise<UmaPolicy, ResourceException> internalReadPolicy(final Context context, final String resourceSetId) {
    String resourceOwnerUid = getResourceOwnerUid(context);
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("resourceTypeUuid"), resourceSetId), QueryFilter.equalTo(new JsonPointer("createdBy"), resourceOwnerUid)));
    return policyResourceDelegate.queryPolicies(context, request).thenAsync(new AsyncFunction<Pair<QueryResponse, List<ResourceResponse>>, UmaPolicy, ResourceException>() {

        @Override
        public Promise<UmaPolicy, ResourceException> apply(Pair<QueryResponse, List<ResourceResponse>> value) {
            try {
                if (value.getSecond().isEmpty()) {
                    return new NotFoundException("UMA Policy not found, " + resourceSetId).asPromise();
                } else {
                    ResourceSetDescription resourceSet = getResourceSet(getRealm(context), resourceSetId);
                    UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, value.getSecond());
                    return newResultPromise(umaPolicy);
                }
            } catch (ResourceException e) {
                return e.asPromise();
            }
        }
    });
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) NotFoundException(org.forgerock.json.resource.NotFoundException) JsonPointer(org.forgerock.json.JsonPointer) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair)

Example 35 with JsonPointer

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

the class UmaPolicyServiceImpl method updatePolicy.

/**
     * {@inheritDoc}
     */
@Override
public //TODO need to check if need to delete backend policies
Promise<UmaPolicy, ResourceException> updatePolicy(//TODO need to check if need to delete backend policies
final Context context, //TODO need to check if need to delete backend policies
final String resourceSetId, JsonValue policy) {
    final UmaPolicy updatedUmaPolicy;
    final ResourceSetDescription resourceSet;
    try {
        resourceSet = getResourceSet(getRealm(context), resourceSetId);
        updatedUmaPolicy = UmaPolicy.valueOf(resourceSet, resolveUsernameToUID(context, policy));
        boolean canShare = canUserShareResourceSet(resourceSet.getResourceOwnerId(), contextHelper.getUserId(context), resourceSet.getClientId(), getRealm(context), resourceSet.getId(), updatedUmaPolicy.getScopes());
        if (!canShare) {
            return new ForbiddenException().asPromise();
        }
        validateScopes(resourceSet, updatedUmaPolicy.getScopes());
    } catch (ResourceException e) {
        return e.asPromise();
    }
    return internalReadPolicy(context, resourceSetId).thenAsync(beforeResourceSharedModified(updatedUmaPolicy)).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            Set<String> modifiedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
            modifiedScopes.retainAll(currentUmaPolicy.getScopes());
            Set<String> removedScopes = new HashSet<>(currentUmaPolicy.getScopes());
            removedScopes.removeAll(modifiedScopes);
            for (JsonValue policy : currentUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
                for (String scope : removedScopes) {
                    if (policy.get("actionValues").isDefined(scope)) {
                        policyResourceDelegate.queryPolicies(context, Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("createdBy"), contextHelper.getUserUid(context)), QueryFilter.equalTo(new JsonPointer("name"), policy.get("name").asString())))).thenAsync(new DeleteOldPolicyFunction(context));
                    }
                }
            }
        }
    }).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            Set<String> modifiedScopes = new HashSet<>(currentUmaPolicy.getScopes());
            modifiedScopes.retainAll(updatedUmaPolicy.getScopes());
            Set<String> deletedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
            deletedScopes.removeAll(modifiedScopes);
            for (JsonValue policy : updatedUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
                for (String scope : deletedScopes) {
                    if (policy.get("actionValues").isDefined(scope)) {
                        policyResourceDelegate.createPolicies(context, singleton(policy));
                    }
                }
            }
        }
    }).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            String uid = contextHelper.getUserId(context);
            Set<String> underlyingPolicyIds = new HashSet<>(currentUmaPolicy.getUnderlyingPolicyIds());
            Set<JsonValue> newUnderlyingPolicies = updatedUmaPolicy.asUnderlyingPolicies(uid);
            for (JsonValue value : newUnderlyingPolicies) {
                underlyingPolicyIds.remove(value.get("name").asString());
            }
            policyResourceDelegate.deletePolicies(context, underlyingPolicyIds);
        }
    }).thenAsync(new UpdatePolicyGraphStatesFunction<UmaPolicy>(resourceSet, context)).thenAsync(new UpdateUmaPolicyFunction(context, updatedUmaPolicy, resourceSetId, resourceSet));
}
Also used : ForbiddenException(org.forgerock.json.resource.ForbiddenException) Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) ResultHandler(org.forgerock.util.promise.ResultHandler) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet)

Aggregations

JsonPointer (org.forgerock.json.JsonPointer)64 Test (org.testng.annotations.Test)40 QueryRequest (org.forgerock.json.resource.QueryRequest)34 JsonValue (org.forgerock.json.JsonValue)21 QueryResponse (org.forgerock.json.resource.QueryResponse)19 Context (org.forgerock.services.context.Context)18 RealmContext (org.forgerock.openam.rest.RealmContext)17 Collection (java.util.Collection)15 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)13 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)13 ClientContext (org.forgerock.services.context.ClientContext)13 Subject (javax.security.auth.Subject)10 ResourceException (org.forgerock.json.resource.ResourceException)10 ResourceResponse (org.forgerock.json.resource.ResourceResponse)10 ArrayList (java.util.ArrayList)9 BadRequestException (org.forgerock.json.resource.BadRequestException)9 SearchFilter (com.sun.identity.entitlement.util.SearchFilter)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 List (java.util.List)7