use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.
the class SmsRealmProvider method handleUpdate.
@Override
public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest request) {
RealmContext realmContext = context.asContext(RealmContext.class);
String realmPath = realmContext.getResolvedRealm();
try {
checkValues(request.getContent());
} catch (BadRequestException e) {
debug.error("RealmResource.updateInstance() : Cannot UPDATE " + realmPath, e);
return new BadRequestException("Invalid attribute values").asPromise();
}
// protect against attempts to change a realm that does not exist as this results in unexpected behaviour
try {
String requestPath = getExpectedPathFromRequestContext(request);
if (!realmPath.equals(requestPath)) {
return new BadRequestException(BAD_REQUEST_REALM_NAME_ERROR_MESSAGE).asPromise();
}
} catch (org.forgerock.oauth2.core.exceptions.NotFoundException e) {
return new BadRequestException(BAD_REQUEST_REALM_NAME_ERROR_MESSAGE).asPromise();
}
final JsonValue realmDetails = request.getContent();
try {
hasPermission(context);
OrganizationConfigManager realmManager = new OrganizationConfigManager(getSSOToken(), realmPath);
realmManager.setAttributes(IdConstants.REPO_SERVICE, getAttributeMap(realmDetails));
final List<Object> newServiceNames = realmDetails.get(SERVICE_NAMES).asList();
if (newServiceNames != null) {
assignServices(realmManager, newServiceNames);
}
debug.message("RealmResource.updateInstance :: UPDATE of realm " + realmPath + " performed by " + PrincipalRestUtils.getPrincipalNameFromServerContext(context));
return newResultPromise(getResource(getJsonValue(realmPath)));
} catch (SMSException e) {
debug.error("RealmResource.updateInstance() : Cannot UPDATE " + realmPath, e);
return configureErrorMessage(e).asPromise();
} catch (SSOException | ForbiddenException | IdRepoException e) {
debug.error("RealmResource.updateInstance() : Cannot UPDATE " + realmPath, e);
return new PermanentException(401, "Access Denied", null).asPromise();
}
}
use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.
the class PrivilegePolicyStoreProviderTest method shouldUseRealmAndSubjectFromContext.
@Test
public void shouldUseRealmAndSubjectFromContext() {
// Given
SubjectContext subjectContext = mock(SubjectContext.class);
Subject subject = new Subject();
String realm = "/test realm";
given(subjectContext.getCallerSubject()).willReturn(subject);
RealmContext context = new RealmContext(subjectContext);
context.setSubRealm(realm, realm);
PrivilegeManager manager = mock(PrivilegeManager.class);
given(mockFactory.get(realm, subject)).willReturn(manager);
// When
PolicyStore store = testProvider.getPolicyStore(context);
// Then
verify(mockFactory).get(realm, subject);
assertThat(store).isNotNull().isInstanceOf(PrivilegePolicyStore.class);
}
use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.
the class PolicyResourceWithCopyMoveSupport method copyPolicy.
private ActionResponse copyPolicy(Context context, String resourceId, ActionRequest request) throws ResourceException {
String sourceRealm = RealmContext.getRealm(context);
JsonValue payload = request.getContent().get("to");
if (payload.isNull()) {
throw new BadRequestException("to definition is missing");
}
String destinationRealm = payload.get("realm").defaultTo(sourceRealm).asString();
ReadRequest readRequest = Requests.newReadRequest("policies", resourceId);
JsonValue policy = router.handleRead(context, readRequest).getOrThrowUninterruptibly().getContent();
String sourceApplication = policy.get("applicationName").asString();
String sourceResourceType = policy.get("resourceTypeUuid").asString();
String destinationApplication = payload.get("application").defaultTo(sourceApplication).asString();
String destinationResourceTypeId = payload.get("resourceType").defaultTo(sourceResourceType).asString();
String copiedName = payload.get("name").defaultTo(resourceId).asString();
if (sourceRealm.equals(destinationRealm) && resourceId.equals(copiedName)) {
throw new BadRequestException("policy name already exists within the realm");
}
policy.put("name", copiedName);
policy.put("applicationName", destinationApplication);
policy.put("resourceTypeUuid", destinationResourceTypeId);
RealmContext updatedContext = new RealmContext(context);
updatedContext.setOverrideRealm(destinationRealm);
CreateRequest createRequest = Requests.newCreateRequest("policies", policy);
JsonValue copiedPolicy = router.handleCreate(updatedContext, createRequest).getOrThrowUninterruptibly().getContent();
return Responses.newActionResponse(copiedPolicy);
}
use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.
the class ApplicationTypesResourceTest method undefinedSubjectShouldFail.
@Test(expectedExceptions = InternalServerErrorException.class)
public void undefinedSubjectShouldFail() throws ResourceException {
//given
SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSubjectContext);
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
Subject subject = null;
given(mockSubjectContext.getCallerSubject()).willReturn(subject);
ReadRequest request = mock(ReadRequest.class);
//when
Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
result.getOrThrowUninterruptibly();
}
use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.
the class ApplicationTypesResourceTest method readShouldFailOnInvalidApplicationType.
@Test(expectedExceptions = NotFoundException.class)
public void readShouldFailOnInvalidApplicationType() throws ResourceException {
//given
SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSubjectContext);
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
Subject subject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(subject);
ReadRequest request = mock(ReadRequest.class);
//when
Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
//then
result.getOrThrowUninterruptibly();
}
Aggregations