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);
}
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);
}
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);
}
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();
}
Aggregations