Search in sources :

Example 11 with ResourceSetStore

use of org.forgerock.oauth2.resources.ResourceSetStore in project OpenAM by OpenRock.

the class ResourceSetServiceTest method setup.

@BeforeMethod
public void setup() throws Exception {
    ResourceSetStoreFactory resourceSetStoreFactory = mock(ResourceSetStoreFactory.class);
    resourceSetStore = mock(ResourceSetStore.class);
    policyService = mock(UmaPolicyService.class);
    coreWrapper = mock(CoreWrapper.class);
    UmaProviderSettingsFactory umaProviderSettingsFactory = mock(UmaProviderSettingsFactory.class);
    umaProviderSettings = mock(UmaProviderSettings.class);
    service = new ResourceSetService(resourceSetStoreFactory, policyService, coreWrapper, umaProviderSettingsFactory);
    given(resourceSetStoreFactory.create("REALM")).willReturn(resourceSetStore);
    given(umaProviderSettingsFactory.get("REALM")).willReturn(umaProviderSettings);
}
Also used : CoreWrapper(org.forgerock.openam.core.CoreWrapper) UmaPolicyService(org.forgerock.openam.uma.UmaPolicyService) UmaProviderSettings(org.forgerock.openam.uma.UmaProviderSettings) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) UmaProviderSettingsFactory(org.forgerock.openam.uma.UmaProviderSettingsFactory) ResourceSetStoreFactory(org.forgerock.openam.oauth2.resources.ResourceSetStoreFactory) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 12 with ResourceSetStore

use of org.forgerock.oauth2.resources.ResourceSetStore in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method setup.

@BeforeMethod
public void setup() throws Exception {
    policyResourceDelegate = mock(PolicyResourceDelegate.class);
    final ResourceSetStoreFactory resourceSetStoreFactory = mock(ResourceSetStoreFactory.class);
    Config<UmaAuditLogger> lazyAuditLogger = mock(Config.class);
    auditLogger = mock(UmaAuditLogger.class);
    contextHelper = mock(ContextHelper.class);
    UmaPolicyEvaluatorFactory policyEvaluatorFactory = mock(UmaPolicyEvaluatorFactory.class);
    policyEvaluator = mock(Evaluator.class);
    given(policyEvaluatorFactory.getEvaluator(any(Subject.class), anyString())).willReturn(policyEvaluator);
    coreServicesWrapper = mock(CoreServicesWrapper.class);
    Debug debug = mock(Debug.class);
    UmaSettingsFactory umaSettingsFactory = mock(UmaSettingsFactory.class);
    UmaSettings umaSettings = mock(UmaSettings.class);
    given(umaSettingsFactory.create(anyString())).willReturn(umaSettings);
    ExtensionFilterManager extensionFilterManager = mock(ExtensionFilterManager.class);
    resourceDelegationFilter = mock(ResourceDelegationFilter.class);
    given(extensionFilterManager.getFilters(ResourceDelegationFilter.class)).willReturn(Collections.singleton(resourceDelegationFilter));
    policyService = new UmaPolicyServiceImpl(policyResourceDelegate, resourceSetStoreFactory, lazyAuditLogger, contextHelper, policyEvaluatorFactory, coreServicesWrapper, debug, umaSettingsFactory, extensionFilterManager);
    given(contextHelper.getRealm(Matchers.<Context>anyObject())).willReturn("REALM");
    given(contextHelper.getUserId(Matchers.<Context>anyObject())).willReturn(RESOURCE_OWNER_ID);
    given(contextHelper.getUserUid(Matchers.<Context>anyObject())).willReturn("RESOURCE_OWNER_UID");
    resourceSetStore = mock(ResourceSetStore.class);
    resourceSet = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", RESOURCE_OWNER_ID, Collections.<String, Object>emptyMap());
    resourceSet.setDescription(json(object(field("name", "NAME"), field("scopes", array("SCOPE_A", "SCOPE_B", "SCOPE_C")))));
    given(resourceSetStoreFactory.create(anyString())).willReturn(resourceSetStore);
    given(resourceSetStore.read("RESOURCE_SET_ID", RESOURCE_OWNER_ID)).willReturn(resourceSet);
    given(resourceSetStore.read(eq("RESOURCE_SET_ID"), any(ResourceSetAcceptAllFilter.class))).willReturn(resourceSet);
    given(resourceSetStore.query(QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_SET_ID, "RESOURCE_SET_ID")))).willReturn(Collections.singleton(resourceSet));
    doThrow(org.forgerock.oauth2.core.exceptions.NotFoundException.class).when(resourceSetStore).read("OTHER_ID", RESOURCE_OWNER_ID);
    doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).read("FAILING_ID", RESOURCE_OWNER_ID);
    doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).query(QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_SET_ID, "FAILING_ID")));
    given(lazyAuditLogger.get()).willReturn(auditLogger);
    AMIdentity identity = mock(AMIdentity.class);
    given(identity.getUniversalId()).willReturn("uid=RESOURCE_OWNER_ID,ou=REALM,dc=forgerock,dc=org");
    given(coreServicesWrapper.getIdentity(RESOURCE_OWNER_ID, "REALM")).willReturn(identity);
}
Also used : CoreServicesWrapper(org.forgerock.openam.core.CoreServicesWrapper) ContextHelper(org.forgerock.openam.rest.resource.ContextHelper) UmaSettingsFactory(org.forgerock.openam.uma.UmaSettingsFactory) UmaAuditLogger(org.forgerock.openam.uma.audit.UmaAuditLogger) ResourceDelegationFilter(org.forgerock.openam.uma.extensions.ResourceDelegationFilter) Matchers.anyString(org.mockito.Matchers.anyString) Evaluator(com.sun.identity.entitlement.Evaluator) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Subject(javax.security.auth.Subject) ResourceSetAcceptAllFilter(org.forgerock.openam.uma.ResourceSetAcceptAllFilter) UmaSettings(org.forgerock.openam.uma.UmaSettings) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) AMIdentity(com.sun.identity.idm.AMIdentity) ResourceSetStoreFactory(org.forgerock.openam.oauth2.resources.ResourceSetStoreFactory) Debug(com.sun.identity.shared.debug.Debug) ExtensionFilterManager(org.forgerock.openam.oauth2.extensions.ExtensionFilterManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 13 with ResourceSetStore

use of org.forgerock.oauth2.resources.ResourceSetStore in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method createResourceSet.

/**
     * <p>Creates or updates a resource set description.</p>
     *
     * <p>If the request contains a If-Match header an update is performed, otherwise a create is performed.</p>
     *
     * <p>An update will replace the current description of the resource set with the contents of the request body.</p>
     *
     * @param entity The new resource set description.
     * @return A JSON object containing the authorization server's unique id for the resource set and, optionally,
     * a policy uri.
     * @throws NotFoundException If the requested resource set description does not exist.
     * @throws ServerException When an error occurs during creating or updating.
     * @throws BadRequestException If the request JSON is invalid.
     */
@Post
public Representation createResourceSet(JsonRepresentation entity) throws NotFoundException, ServerException, BadRequestException {
    ResourceSetDescription resourceSetDescription = new ResourceSetDescription(null, getClientId(), getResourceOwnerId(), validator.validate(toMap(entity)));
    OAuth2Request oAuth2Request = requestFactory.create(getRequest());
    ResourceSetStore store = providerSettingsFactory.get(oAuth2Request).getResourceSetStore();
    QueryFilter<String> query = QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.NAME, resourceSetDescription.getName()), QueryFilter.equalTo(ResourceSetTokenField.CLIENT_ID, getClientId()), QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_OWNER_ID, getResourceOwnerId()));
    if (!store.query(query).isEmpty()) {
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        Map<String, Object> response = new HashMap<String, Object>();
        response.put(OAuth2Constants.Params.ERROR, Status.CLIENT_ERROR_BAD_REQUEST.getReasonPhrase());
        response.put(OAuth2Constants.Params.ERROR_DESCRIPTION, "A shared item with the name '" + resourceSetDescription.getName() + "' already exists");
        return new JsonRepresentation(response);
    }
    JsonValue labels = resourceSetDescription.getDescription().get(OAuth2Constants.ResourceSets.LABELS);
    resourceSetDescription.getDescription().remove(OAuth2Constants.ResourceSets.LABELS);
    for (ResourceRegistrationFilter filter : extensionFilterManager.getFilters(ResourceRegistrationFilter.class)) {
        filter.beforeResourceRegistration(resourceSetDescription);
    }
    store.create(oAuth2Request, resourceSetDescription);
    if (labels.isNotNull()) {
        resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, labels.asSet());
    }
    labelRegistration.updateLabelsForNewResourceSet(resourceSetDescription);
    for (ResourceRegistrationFilter filter : extensionFilterManager.getFilters(ResourceRegistrationFilter.class)) {
        filter.afterResourceRegistration(resourceSetDescription);
    }
    for (ResourceSetRegistrationHook hook : hooks) {
        hook.resourceSetCreated(oAuth2Request.<String>getParameter("realm"), resourceSetDescription);
    }
    getResponse().setStatus(Status.SUCCESS_CREATED);
    return createJsonResponse(resourceSetDescription, false, true);
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) HashMap(java.util.HashMap) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetRegistrationHook(org.forgerock.oauth2.restlet.resources.ResourceSetRegistrationHook) JsonValue(org.forgerock.json.JsonValue) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceRegistrationFilter(org.forgerock.openam.oauth2.extensions.ResourceRegistrationFilter) Post(org.restlet.resource.Post)

Example 14 with ResourceSetStore

use of org.forgerock.oauth2.resources.ResourceSetStore in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method deleteResourceSet.

/**
     * <p>Deletes the resource set description for the request resource set id as long as the If-Match header matches
     * the current version of the resource set.</p>
     *
     * <p>If no If-Match header is present on the request a 512 Precondition Failed response will be returned.</p>
     *
     * @return An empty representation.
     * @throws NotFoundException If the requested resource set description does not exist.
     * @throws ServerException When an error occurs during removal.
     */
@Delete
public Representation deleteResourceSet() throws NotFoundException, ServerException {
    if (!isConditionalRequest()) {
        throw new ResourceException(512, "precondition_failed", "Require If-Match header to delete Resource Set", null);
    }
    ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
    ResourceSetDescription resourceSetDescription = store.read(getResourceSetId(), getResourceOwnerId());
    OAuth2Request oAuth2Request = requestFactory.create(getRequest());
    for (ResourceSetRegistrationHook hook : hooks) {
        hook.resourceSetDeleted(oAuth2Request.<String>getParameter("realm"), resourceSetDescription);
    }
    labelRegistration.updateLabelsForDeletedResourceSet(resourceSetDescription);
    store.delete(getResourceSetId(), getResourceOwnerId());
    return createEmptyResponse();
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetRegistrationHook(org.forgerock.oauth2.restlet.resources.ResourceSetRegistrationHook) ResourceException(org.restlet.resource.ResourceException) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Delete(org.restlet.resource.Delete)

Aggregations

ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)14 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)11 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 HashSet (java.util.HashSet)3 Subject (javax.security.auth.Subject)3 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)3 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)3 ExtensionFilterManager (org.forgerock.openam.oauth2.extensions.ExtensionFilterManager)3 Evaluator (com.sun.identity.entitlement.Evaluator)2 HashMap (java.util.HashMap)2 JsonValue (org.forgerock.json.JsonValue)2 AccessToken (org.forgerock.oauth2.core.AccessToken)2 OAuth2ProviderSettingsFactory (org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory)2 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)2 ResourceSetRegistrationHook (org.forgerock.oauth2.restlet.resources.ResourceSetRegistrationHook)2 ResourceSetStoreFactory (org.forgerock.openam.oauth2.resources.ResourceSetStoreFactory)2 UmaAuditLogger (org.forgerock.openam.uma.audit.UmaAuditLogger)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Request (org.restlet.Request)2