use of org.forgerock.json.resource.ForbiddenException in project OpenAM by OpenRock.
the class IdentityResourceV1 method deleteInstance.
/**
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> deleteInstance(final Context context, final String resourceId, final DeleteRequest request) {
RealmContext realmContext = context.asContext(RealmContext.class);
final String realm = realmContext.getResolvedRealm();
JsonValue result = new JsonValue(new LinkedHashMap<String, Object>(1));
ResourceResponse resource;
IdentityDetails dtls;
try {
SSOToken admin = getSSOToken(getCookieFromServerContext(context));
// read to see if resource is available to user
dtls = identityServices.read(resourceId, getIdentityServicesAttributes(realm), admin);
// delete the resource
identityServices.delete(dtls, admin);
String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
debug.message("IdentityResource.deleteInstance :: DELETE of resourceId={} in realm={} performed by " + "principalName={}", resourceId, realm, principalName);
result.put("success", "true");
resource = newResourceResponse(resourceId, "0", result);
return newResultPromise(resource);
} catch (final NeedMoreCredentials ex) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : User does not have enough" + " privileges.", resourceId, ex);
return new ForbiddenException(resourceId, ex).asPromise();
} catch (final ObjectNotFound notFound) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE {} : Resource cannot be found.", resourceId, notFound);
return new NotFoundException("Resource cannot be found.", notFound).asPromise();
} catch (final TokenExpired tokenExpired) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : Unauthorized", resourceId, tokenExpired);
return new PermanentException(401, "Unauthorized", null).asPromise();
} catch (final AccessDenied accessDenied) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : Access denied", resourceId, accessDenied);
return new ForbiddenException(accessDenied).asPromise();
} catch (final GeneralFailure generalFailure) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : general failure", resourceId, generalFailure);
return new BadRequestException(generalFailure.getMessage(), generalFailure).asPromise();
} catch (ForbiddenException ex) {
debug.warning("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={}: User does not have " + "enough privileges.", resourceId, ex);
return new ForbiddenException(resourceId, ex).asPromise();
} catch (NotFoundException notFound) {
debug.warning("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : Resource cannot be found", resourceId, notFound);
return new NotFoundException("Resource cannot be found.", notFound).asPromise();
} catch (ResourceException re) {
debug.warning("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={} : resource failure", resourceId, re);
result.put("success", "false");
resource = newResourceResponse(resourceId, "0", result);
return newResultPromise(resource);
} catch (Exception e) {
debug.error("IdentityResource.deleteInstance() :: Cannot DELETE resourceId={}", resourceId, e);
result.put("success", "false");
resource = newResourceResponse(resourceId, "0", result);
return newResultPromise(resource);
}
}
use of org.forgerock.json.resource.ForbiddenException in project OpenAM by OpenRock.
the class IdentityResourceV1 method idFromSession.
/**
* Gets the user id from the session provided in the server context
*
* @param context Current Server Context
* @param request Request from client to retrieve id
*/
private Promise<ActionResponse, ResourceException> idFromSession(final Context context, final ActionRequest request) {
JsonValue result = new JsonValue(new LinkedHashMap<String, Object>(1));
SSOToken ssotok;
AMIdentity amIdentity;
try {
SSOTokenManager mgr = SSOTokenManager.getInstance();
ssotok = mgr.createSSOToken(getCookieFromServerContext(context));
amIdentity = new AMIdentity(ssotok);
// build resource
result.put("id", amIdentity.getName());
result.put("realm", com.sun.identity.sm.DNMapper.orgNameToRealmName(amIdentity.getRealm()));
result.put("dn", amIdentity.getUniversalId());
result.put("successURL", ssotok.getProperty(ISAuthConstants.SUCCESS_URL, false));
result.put("fullLoginURL", ssotok.getProperty(ISAuthConstants.FULL_LOGIN_URL, false));
if (debug.messageEnabled()) {
debug.message("IdentityResource.idFromSession() :: Retrieved ID for user={}", amIdentity.getName());
}
return newResultPromise(newActionResponse(result));
} catch (SSOException e) {
debug.error("IdentityResource.idFromSession() :: Cannot retrieve SSO Token", e);
return new ForbiddenException("SSO Token cannot be retrieved.", e).asPromise();
} catch (IdRepoException ex) {
debug.error("IdentityResource.idFromSession() :: Cannot retrieve user from IdRepo", ex);
return new ForbiddenException("Cannot retrieve id from session.", ex).asPromise();
}
}
use of org.forgerock.json.resource.ForbiddenException in project OpenAM by OpenRock.
the class RealmResource method createInstance.
/**
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request) {
RealmContext realmContext = context.asContext(RealmContext.class);
String realmPath = realmContext.getResolvedRealm();
ResourceResponse resource;
String parentRealm;
String childRealm;
String realm = null;
try {
hasPermission(context);
final JsonValue jVal = request.getContent();
// get the realm
realm = jVal.get("realm").asString();
if (StringUtils.isBlank(realm)) {
realm = request.getNewResourceId();
}
realm = checkForTopLevelRealm(realm);
if (StringUtils.isBlank(realm)) {
throw new BadRequestException("No realm name provided.");
} else if (!realm.startsWith("/")) {
realm = "/" + realm;
}
if (!realmPath.equalsIgnoreCase("/")) {
// build realm to comply with format if not top level
realm = realmPath + realm;
}
parentRealm = RealmUtils.getParentRealm(realm);
childRealm = RealmUtils.getChildRealm(realm);
OrganizationConfigManager ocm = new OrganizationConfigManager(getSSOToken(), parentRealm);
Map defaultValues = createServicesMap(jVal);
ocm.createSubOrganization(childRealm, defaultValues);
String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
debug.message("RealmResource.createInstance :: CREATE of realm " + childRealm + " in realm " + parentRealm + " performed by " + principalName);
// create a resource for handler to return
OrganizationConfigManager realmCreated = new OrganizationConfigManager(getSSOToken(), realm);
resource = newResourceResponse(childRealm, String.valueOf(System.currentTimeMillis()), createJsonMessage("realmCreated", realmCreated.getOrganizationName()));
return newResultPromise(resource);
} catch (SMSException smse) {
debug.error("RealmResource.createInstance() : Cannot find " + realm, smse);
try {
configureErrorMessage(smse);
return new BadRequestException(smse.getMessage(), smse).asPromise();
} catch (NotFoundException nf) {
debug.error("RealmResource.createInstance() : Cannot find " + realm, nf);
return nf.asPromise();
} catch (ForbiddenException fe) {
// User does not have authorization
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, fe);
return fe.asPromise();
} catch (PermanentException pe) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, pe);
// Cannot recover from this exception
return pe.asPromise();
} catch (ConflictException ce) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, ce);
return ce.asPromise();
} catch (BadRequestException be) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, be);
return be.asPromise();
} catch (Exception e) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, e);
return new BadRequestException(e.getMessage(), e).asPromise();
}
} catch (SSOException sso) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, sso);
return new PermanentException(401, "Access Denied", null).asPromise();
} catch (ForbiddenException fe) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, fe);
return fe.asPromise();
} catch (BadRequestException be) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, be);
return be.asPromise();
} catch (PermanentException pe) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realm, pe);
// Cannot recover from this exception
return pe.asPromise();
} catch (Exception e) {
debug.error("RealmResource.createInstance()" + realm + ":" + e);
return new BadRequestException(e.getMessage(), e).asPromise();
}
}
use of org.forgerock.json.resource.ForbiddenException in project OpenAM by OpenRock.
the class IdentityResourceV2 method attemptResourceCreation.
private Promise<IdentityDetails, ResourceException> attemptResourceCreation(String realm, SSOToken admin, IdentityDetails identity, String resourceId) {
IdentityDetails dtls = null;
try {
// Create the resource
identityServices.create(identity, admin);
// Read created resource
dtls = identityServices.read(resourceId, getIdentityServicesAttributes(realm, objectType), admin);
if (debug.messageEnabled()) {
debug.message("IdentityResource.createInstance() :: Created resourceId={} in realm={} by AdminID={}", resourceId, realm, admin.getTokenID());
}
} catch (final ObjectNotFound notFound) {
debug.error("IdentityResource.createInstance() :: Cannot READ resourceId={} : Resource cannot be found.", resourceId, notFound);
return new NotFoundException("Resource not found.", notFound).asPromise();
} catch (final TokenExpired tokenExpired) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE resourceId={} : Unauthorized", resourceId, tokenExpired);
return new PermanentException(401, "Unauthorized", null).asPromise();
} catch (final NeedMoreCredentials needMoreCredentials) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE resourceId={} : Token is not authorized", resourceId, needMoreCredentials);
return new ForbiddenException("Token is not authorized", needMoreCredentials).asPromise();
} catch (final GeneralAccessDeniedError accessDenied) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE " + accessDenied);
return new ForbiddenException().asPromise();
} catch (GeneralFailure generalFailure) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE " + generalFailure);
return new BadRequestException("Resource cannot be created: " + generalFailure.getMessage(), generalFailure).asPromise();
} catch (AccessDenied accessDenied) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE " + accessDenied);
return new ForbiddenException("Token is not authorized: " + accessDenied.getMessage(), accessDenied).asPromise();
} catch (ResourceException re) {
debug.warning("IdentityResource.createInstance() :: Cannot CREATE resourceId={}", resourceId, re);
return re.asPromise();
} catch (final Exception e) {
debug.error("IdentityResource.createInstance() :: Cannot CREATE resourceId={}", resourceId, e);
return new NotFoundException(e.getMessage(), e).asPromise();
}
return newResultPromise(dtls);
}
use of org.forgerock.json.resource.ForbiddenException 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();
}
}
Aggregations