Search in sources :

Example 36 with ServerException

use of org.forgerock.oauth2.core.exceptions.ServerException in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method readResourceSet.

private Representation readResourceSet(String resourceSetId) throws NotFoundException, ServerException {
    ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
    ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId());
    Set<String> labels = new HashSet<String>();
    try {
        Set<ResourceSetLabel> labelSet = umaLabelsStore.forResourceSet(resourceSetDescription.getRealm(), resourceSetDescription.getResourceOwnerId(), resourceSetDescription.getId(), false);
        for (ResourceSetLabel label : labelSet) {
            labels.add(label.getName());
        }
    } catch (org.forgerock.json.resource.ResourceException e) {
        throw new ServerException(e);
    }
    resourceSetDescription.getDescription().put("labels", labels);
    return createJsonResponse(resourceSetDescription, true, true);
}
Also used : ServerException(org.forgerock.oauth2.core.exceptions.ServerException) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) HashSet(java.util.HashSet) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 37 with ServerException

use of org.forgerock.oauth2.core.exceptions.ServerException in project OpenAM by OpenRock.

the class ResourceSetService method getResourceSets.

/**
     * Queries resource sets across the resource set store and UMA policy store.
     *
     * @param context             The context.
     * @param realm               The realm.
     * @param query               The aggregated query.
     * @param resourceOwnerId     The resource owner id.
     * @param augmentWithPolicies {@code true} to pull in UMA policies into the resource set.
     * @return A Promise containing the Resource Sets or a ResourceException.
     */
Promise<Collection<ResourceSetDescription>, ResourceException> getResourceSets(final Context context, String realm, final ResourceSetWithPolicyQuery query, final String resourceOwnerId, final boolean augmentWithPolicies) {
    final Set<ResourceSetDescription> resourceSets;
    try {
        resourceSets = new ResourceSetSharedFilter(this, resourceOwnerId, realm).filter(resourceSetStoreFactory.create(realm).query(query.getResourceSetQuery()));
    } catch (ServerException e) {
        return new InternalServerErrorException(e).asPromise();
    }
    QueryRequest policyQuery = newQueryRequest("").setQueryId("searchAll");
    policyQuery.setQueryFilter(QueryFilter.<JsonPointer>alwaysTrue());
    return getSharedResourceSets(context, policyQuery, resourceOwnerId).thenAsync(new AsyncFunction<Set<ResourceSetDescription>, Collection<ResourceSetDescription>, ResourceException>() {

        @Override
        public Promise<Collection<ResourceSetDescription>, ResourceException> apply(final Set<ResourceSetDescription> sharedResourceSets) {
            //combine the owned ResourceSets with the shared ones, then filter based on the query
            sharedResourceSets.addAll(resourceSets);
            final Collection<ResourceSetDescription> filteredResourceSets = filterPolicies(resourceSets, query);
            Promise<Collection<ResourceSetDescription>, ResourceException> resourceSetsPromise;
            if (query.getPolicyQuery() != null) {
                QueryRequest policyQuery = newQueryRequest("").setQueryFilter(query.getPolicyQuery());
                resourceSetsPromise = policyService.queryPolicies(context, policyQuery).thenAsync(new AsyncFunction<Pair<QueryResponse, Collection<UmaPolicy>>, Collection<ResourceSetDescription>, ResourceException>() {

                    @Override
                    public Promise<Collection<ResourceSetDescription>, ResourceException> apply(Pair<QueryResponse, Collection<UmaPolicy>> result) throws ResourceException {
                        try {
                            return newResultPromise(combine(context, query, filteredResourceSets, result.getSecond(), augmentWithPolicies, resourceOwnerId));
                        } catch (org.forgerock.oauth2.core.exceptions.NotFoundException e) {
                            return new InternalServerErrorException(e).asPromise();
                        } catch (ServerException e) {
                            return new InternalServerErrorException(e).asPromise();
                        }
                    }
                });
            } else {
                if (augmentWithPolicies) {
                    List<Promise<ResourceSetDescription, ResourceException>> promises = new ArrayList<>();
                    PromiseImpl<ResourceSetDescription, ResourceException> kicker = PromiseImpl.create();
                    promises.add(kicker);
                    for (ResourceSetDescription resourceSet : filteredResourceSets) {
                        promises.add(augmentWithPolicy(context, resourceSet.getId(), resourceSet));
                    }
                    resourceSetsPromise = Promises.when(promises).thenAsync(new AsyncFunction<List<ResourceSetDescription>, Collection<ResourceSetDescription>, ResourceException>() {

                        @Override
                        public Promise<Collection<ResourceSetDescription>, ResourceException> apply(List<ResourceSetDescription> resourceSets) {
                            Collection<ResourceSetDescription> resourceSetDescriptions = new HashSet<>();
                            for (ResourceSetDescription rs : filteredResourceSets) {
                                if (rs != null) {
                                    resourceSetDescriptions.add(rs);
                                }
                            }
                            return newResultPromise(resourceSetDescriptions);
                        }
                    });
                    kicker.handleResult(null);
                } else {
                    resourceSetsPromise = newResultPromise(filteredResourceSets);
                }
            }
            return resourceSetsPromise;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) 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) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) Requests.newQueryRequest(org.forgerock.json.resource.Requests.newQueryRequest) QueryRequest(org.forgerock.json.resource.QueryRequest) PromiseImpl(org.forgerock.util.promise.PromiseImpl) ResourceSetSharedFilter(org.forgerock.openam.uma.ResourceSetSharedFilter) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) QueryResponse(org.forgerock.json.resource.QueryResponse) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) Collection(java.util.Collection)

Example 38 with ServerException

use of org.forgerock.oauth2.core.exceptions.ServerException in project OpenAM by OpenRock.

the class UmaResourceSetRegistrationHook method resourceSetDeleted.

/**
     * Removes the ResourceType from the Resource Server's policy application, deletes all related policies,
     * then deletes the ResourceSet.
     *
     * @param realm {@inheritDoc}
     * @param resourceSet {@inheritDoc}
     */
@Override
public void resourceSetDeleted(String realm, ResourceSetDescription resourceSet) throws ServerException {
    Subject adminSubject = SubjectUtils.createSuperAdminSubject();
    String resourceTypeUUID = resourceSet.getId();
    try {
        Application application = applicationManager.getApplication(adminSubject, realm, resourceSet.getClientId().toLowerCase());
        application.removeResourceTypeUuid(resourceTypeUUID);
        applicationManager.saveApplication(adminSubject, realm, application);
    } catch (EntitlementException e) {
        logger.error("Failed to remove Resource Type, " + resourceTypeUUID + " from application, " + resourceSet.getClientId(), e);
        throw new ServerException(e);
    }
    policyService.deletePolicy(createAdminContext(realm, resourceSet.getResourceOwnerId()), resourceSet.getId());
    try {
        resourceTypeService.deleteResourceType(adminSubject, realm, resourceTypeUUID);
    } catch (EntitlementException e) {
        logger.error("Failed to delete Resource Type " + resourceTypeUUID, e);
        throw new ServerException(e);
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject)

Example 39 with ServerException

use of org.forgerock.oauth2.core.exceptions.ServerException in project OpenAM by OpenRock.

the class UmaPolicyApplicationListener method deleteResourceSets.

private void deleteResourceSets(String realm, String resourceServerId) throws NotFoundException, ServerException {
    ResourceSetStore resourceSetStore = resourceSetStoreFactory.create(DNMapper.orgNameToRealmName(realm));
    QueryFilter<String> queryFilter = QueryFilter.equalTo(ResourceSetTokenField.CLIENT_ID, resourceServerId);
    Set<ResourceSetDescription> results = resourceSetStore.query(queryFilter);
    for (ResourceSetDescription resourceSet : results) {
        resourceSetStore.delete(resourceSet.getId(), resourceSet.getResourceOwnerId());
    }
}
Also used : ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription)

Example 40 with ServerException

use of org.forgerock.oauth2.core.exceptions.ServerException in project OpenAM by OpenRock.

the class UmaTokenStore method createPermissionTicket.

PermissionTicket createPermissionTicket(String resourceSetId, Set<String> scopes, String clientId) throws ServerException, NotFoundException {
    UmaProviderSettings settings = settingsFactory.get(realm);
    PermissionTicket permissionTicket = new PermissionTicket(null, resourceSetId, scopes, clientId);
    permissionTicket.setRealm(realm);
    permissionTicket.setExpiryTime(System.currentTimeMillis() + (settings.getPermissionTicketLifetime() * 1000));
    try {
        cts.create(permissionTicketAdapter.toToken(permissionTicket));
    } catch (CoreTokenException e) {
        throw new ServerException(e);
    }
    return permissionTicket;
}
Also used : ServerException(org.forgerock.oauth2.core.exceptions.ServerException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException)

Aggregations

ServerException (org.forgerock.oauth2.core.exceptions.ServerException)60 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)31 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)25 JsonValue (org.forgerock.json.JsonValue)18 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)18 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)18 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)14 HashMap (java.util.HashMap)13 AccessToken (org.forgerock.oauth2.core.AccessToken)13 HashSet (java.util.HashSet)12 InvalidGrantException (org.forgerock.oauth2.core.exceptions.InvalidGrantException)11 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)11 SSOException (com.iplanet.sso.SSOException)9 Request (org.restlet.Request)9 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)8 Map (java.util.Map)7 OAuth2Uris (org.forgerock.oauth2.core.OAuth2Uris)7 JSONObject (org.json.JSONObject)7 SMSException (com.sun.identity.sm.SMSException)6 ResourceOwner (org.forgerock.oauth2.core.ResourceOwner)6