use of org.forgerock.oauth2.core.exceptions.BadRequestException in project OpenAM by OpenRock.
the class OpenIDConnectEndSession method endSession.
/**
* Ends an OpenId Connect session.
*
* @param idToken The OpenId Token.
* @throws BadRequestException If the request is malformed.
* @throws ServerException If any internal server error occurs.
*/
public void endSession(String idToken) throws BadRequestException, ServerException {
if (idToken == null || idToken.isEmpty()) {
logger.warn("No id_token_hint parameter supplied to the endSession endpoint");
throw new BadRequestException("The endSession endpoint requires an id_token_hint parameter");
}
JwtReconstruction jwtReconstruction = new JwtReconstruction();
SignedJwt jwt = jwtReconstruction.reconstructJwt(idToken, SignedJwt.class);
JwtClaimsSet claims = jwt.getClaimsSet();
String opsId = (String) claims.getClaim(OAuth2Constants.JWTTokenParams.OPS);
if (opsId == null) {
opsId = (String) claims.getClaim(OAuth2Constants.JWTTokenParams.LEGACY_OPS);
}
openIDConnectProvider.destroySession(opsId);
}
use of org.forgerock.oauth2.core.exceptions.BadRequestException in project OpenAM by OpenRock.
the class ClaimsParameterValidator method validateRequest.
@Override
public void validateRequest(OAuth2Request request) throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException, UnsupportedResponseTypeException, ServerException, BadRequestException, InvalidScopeException, NotFoundException {
final OAuth2ProviderSettings settings = providerSettingsFactory.get(request);
final String claims = request.getParameter(OAuth2Constants.Custom.CLAIMS);
//if we aren't supporting this no need to validate
if (!settings.getClaimsParameterSupported()) {
return;
}
//if we support, but it's not requested, no need to validate
if (claims == null) {
return;
}
final JSONObject claimsJson;
//convert claims into JSON object
try {
claimsJson = new JSONObject(claims);
} catch (JSONException e) {
throw new BadRequestException("Invalid JSON in supplied claims parameter.");
}
JSONObject userinfoClaims = null;
try {
userinfoClaims = claimsJson.getJSONObject(OAuth2Constants.UserinfoEndpoint.USERINFO);
} catch (Exception e) {
//fall through
}
//results in an Access Token being issued to the Client for use at the UserInfo Endpoint.
if (userinfoClaims != null) {
String responseType = request.getParameter(OAuth2Constants.Params.RESPONSE_TYPE);
if (responseType != null && responseType.trim().equals(OAuth2Constants.JWTTokenParams.ID_TOKEN)) {
throw new BadRequestException("Must request an access token when providing " + "userinfo in claims parameter.");
}
}
}
use of org.forgerock.oauth2.core.exceptions.BadRequestException in project OpenAM by OpenRock.
the class ResourceSetRegistrationExceptionFilterTest method shouldSetBadRequestExceptionResponse.
@Test
@SuppressWarnings("unchecked")
public void shouldSetBadRequestExceptionResponse() throws Exception {
//Given
Request request = mock(Request.class);
Response response = mock(Response.class);
Exception exception = new BadRequestException("MESSAGE");
Status status = new Status(444, exception);
given(response.getStatus()).willReturn(status);
//When
exceptionFilter.afterHandle(request, response);
//Then
ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
verify(response).setEntity(exceptionResponseCaptor.capture());
Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
assertThat(responseBody).containsOnly(entry("error", "bad_request"), entry("error_description", "MESSAGE"));
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(response).setStatus(statusCaptor.capture());
assertThat(statusCaptor.getValue().getCode()).isEqualTo(400);
assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
}
use of org.forgerock.oauth2.core.exceptions.BadRequestException in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpoint method updateResourceSet.
@Put
public Representation updateResourceSet(JsonRepresentation entity) throws NotFoundException, ServerException, BadRequestException {
if (!isConditionalRequest()) {
throw new ResourceException(512, "precondition_failed", "Require If-Match header to update Resource Set", null);
}
final Map<String, Object> resourceSetDescriptionAttributes = validator.validate(toMap(entity));
final String resourceSetId = getResourceSetId();
ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId()).update(resourceSetDescriptionAttributes);
JsonValue labels = resourceSetDescription.getDescription().get(OAuth2Constants.ResourceSets.LABELS);
resourceSetDescription.getDescription().remove(OAuth2Constants.ResourceSets.LABELS);
store.update(resourceSetDescription);
if (labels.isNotNull()) {
resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, labels.asSet());
} else {
resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, new HashSet<String>());
}
labelRegistration.updateLabelsForExistingResourceSet(resourceSetDescription);
return createJsonResponse(resourceSetDescription, false, true);
}
use of org.forgerock.oauth2.core.exceptions.BadRequestException in project OpenAM by OpenRock.
the class ResourceSetResource method updateInstance.
/**
* Update the none system labels on a resource set only
*
* @param context {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId, UpdateRequest request) {
final Map<String, Object> resourceSetDescriptionAttributes;
try {
resourceSetDescriptionAttributes = validator.validate(request.getContent().asMap());
final String realm = getRealm(context);
final String userId = getUserId(context);
//remove this resource set id from all labels
Set<ResourceSetLabel> labels = umaLabelsStore.forResourceSet(realm, userId, resourceId, true);
for (ResourceSetLabel label : labels) {
if (!isSystemLabel(label)) {
label.removeResourceSetId(resourceId);
umaLabelsStore.update(realm, userId, label);
}
}
//add resource set id to new labels
for (String labelId : (List<String>) resourceSetDescriptionAttributes.get("labels")) {
ResourceSetLabel label = umaLabelsStore.read(realm, userId, labelId);
label.addResourceSetId(resourceId);
umaLabelsStore.update(realm, userId, label);
}
return resourceSetService.getResourceSet(context, realm, resourceId, userId, augmentWithPolicies(request)).thenAsync(new AsyncFunction<ResourceSetDescription, ResourceResponse, ResourceException>() {
@Override
public Promise<ResourceResponse, ResourceException> apply(ResourceSetDescription result) {
try {
JsonValue content = null;
content = getResourceSetJson(result, userId);
return newResultPromise(newResource(result.getId(), content));
} catch (ResourceException e) {
return e.asPromise();
}
}
});
} catch (ResourceException e) {
return e.asPromise();
} catch (org.forgerock.oauth2.core.exceptions.BadRequestException e) {
return new BadRequestException("Error retrieving labels.", e).asPromise();
}
}
Aggregations