use of io.gravitee.am.common.exception.oauth2.InvalidRequestException in project gravitee-access-management by gravitee-io.
the class AccountFactorsEndpointHandler method verifyFactor.
/**
* Verify a factor for the current user
*
* @param routingContext the routingContext holding the current user
*/
public void verifyFactor(RoutingContext routingContext) {
try {
if (routingContext.getBodyAsString() == null) {
routingContext.fail(new InvalidRequestException("Unable to parse body message"));
return;
}
final User user = routingContext.get(ConstantKeys.USER_CONTEXT_KEY);
final String factorId = routingContext.request().getParam("factorId");
final String code = routingContext.getBodyAsJson().getString("code");
// code is required
if (isEmpty(code)) {
routingContext.fail(new InvalidRequestException("Field [code] is required"));
return;
}
// find factor
findFactor(factorId, h -> {
if (h.failed()) {
routingContext.fail(h.cause());
return;
}
final FactorProvider factorProvider = factorManager.get(factorId);
if (factorProvider == null) {
routingContext.fail(new FactorNotFoundException(factorId));
return;
}
// get enrolled factor for the current user
Optional<EnrolledFactor> optionalEnrolledFactor = user.getFactors().stream().filter(enrolledFactor -> factorId.equals(enrolledFactor.getFactorId())).findFirst();
if (!optionalEnrolledFactor.isPresent()) {
routingContext.fail(new FactorNotFoundException(factorId));
return;
}
// if factor is already activated, continue
final EnrolledFactor enrolledFactor = optionalEnrolledFactor.get();
if (FactorStatus.ACTIVATED.equals(enrolledFactor.getStatus())) {
AccountResponseHandler.handleDefaultResponse(routingContext, enrolledFactor);
return;
}
// verify factor
verifyFactor(code, enrolledFactor, factorProvider, vh -> {
if (vh.failed()) {
routingContext.fail(vh.cause());
return;
}
// verify successful, change the EnrolledFactor Status
enrolledFactor.setStatus(FactorStatus.ACTIVATED);
accountService.upsertFactor(user.getId(), enrolledFactor, new DefaultUser(user)).subscribe(__ -> AccountResponseHandler.handleDefaultResponse(routingContext, enrolledFactor), error -> routingContext.fail(error));
});
});
} catch (DecodeException ex) {
routingContext.fail(new InvalidRequestException("Unable to parse body message"));
}
}
use of io.gravitee.am.common.exception.oauth2.InvalidRequestException in project gravitee-access-management by gravitee-io.
the class AccountEndpointHandler method changePassword.
public void changePassword(RoutingContext routingContext) {
try {
// update user password value from parameters
final JsonObject bodyAsJson = routingContext.getBodyAsJson();
if (isNull(bodyAsJson) || bodyAsJson.isEmpty()) {
routingContext.fail(new InvalidRequestException("Body is null or empty"));
return;
}
final Client client = routingContext.get(ConstantKeys.CLIENT_CONTEXT_KEY);
final User user = getUserFromContext(routingContext);
final DefaultUser principal = convertUserToPrincipal(routingContext, user);
final String password = bodyAsJson.getString(PASSWORD_KEY);
// user password is required
if (isEmpty(password)) {
routingContext.fail(new InvalidRequestException("Field [password] is required"));
return;
}
accountService.resetPassword(user, client, password, principal).subscribe(__ -> handleNoBodyResponse(routingContext), error -> {
if (error instanceof UserProviderNotFoundException) {
handleUpdateUserResponse(routingContext, "Action forbidden", FORBIDDEN_403);
} else {
routingContext.fail(error);
}
});
} catch (DecodeException ex) {
routingContext.fail(new InvalidRequestException("Unable to parse body message"));
}
}
use of io.gravitee.am.common.exception.oauth2.InvalidRequestException in project gravitee-access-management by gravitee-io.
the class PermissionEndpoint method extractRequest.
/**
* Specification state :
* Requesting multiple permissions might be appropriate, for example, in cases where the resource server expects the requesting party
* to need access to several related resources if they need access to any one of the resources
*
* Means : Body can either be a JsonArray or a JsonObject, we need to handle both case.
*
* See <a href="https://docs.kantarainitiative.org/uma/wg/rec-oauth-uma-federated-authz-2.0.html#permission-endpoint">here</a>
* @param context RoutingContext
* @return List of PermissionRequest
*/
private Single<List<PermissionTicketRequest>> extractRequest(RoutingContext context) {
List<PermissionTicketRequest> result;
Object json;
try {
json = context.getBody().toJson();
} catch (RuntimeException err) {
return Single.error(new InvalidRequestException("Unable to parse body permission request"));
}
if (json instanceof JsonArray) {
result = convert(((JsonArray) json).getList());
} else {
result = Arrays.asList(((JsonObject) json).mapTo(PermissionTicketRequest.class));
}
return Single.just(result);
}
use of io.gravitee.am.common.exception.oauth2.InvalidRequestException in project gravitee-access-management by gravitee-io.
the class ResourceAccessPoliciesEndpoint method extractRequest.
private AccessPolicy extractRequest(RoutingContext context) {
try {
// get body request
JsonObject body = context.getBodyAsJson();
// check missing values
Arrays.asList("name", "type", "description", "condition").forEach(key -> {
if (!body.containsKey(key)) {
throw new InvalidRequestException("[" + key + ": must not be null]");
}
});
// check type value
AccessPolicyType accessPolicyType = AccessPolicyType.fromString(body.getString("type"));
// check condition value
AccessPolicyCondition condition = body.getJsonObject("condition").mapTo(accessPolicyType.getConditionClazz());
// create access policy object
AccessPolicy accessPolicy = new AccessPolicy();
accessPolicy.setType(accessPolicyType);
accessPolicy.setName(body.getString("name"));
accessPolicy.setDescription(body.getString("description"));
accessPolicy.setCondition(condition.toString());
accessPolicy.setEnabled(body.getBoolean("enabled", true));
return accessPolicy;
} catch (DecodeException ex) {
throw new InvalidRequestException("Bad request payload");
} catch (Exception ex) {
throw new InvalidRequestException(ex.getMessage());
}
}
use of io.gravitee.am.common.exception.oauth2.InvalidRequestException in project gravitee-access-management by gravitee-io.
the class CibaTokenGranter method parseRequest.
@Override
protected Single<TokenRequest> parseRequest(TokenRequest tokenRequest, Client client) {
MultiValueMap<String, String> parameters = tokenRequest.parameters();
final String authReqId = parameters.getFirst(Parameters.AUTH_REQ_ID);
if (isEmpty(authReqId)) {
return Single.error(new InvalidRequestException("Missing parameter: auth_req_id"));
}
return super.parseRequest(tokenRequest, client).flatMap(tokenRequest1 -> authenticationRequestService.retrieve(domain, authReqId).map(cibaRequest -> {
if (!cibaRequest.getClientId().equals(client.getClientId())) {
logger.warn("client_id '{}' requests token using not owned authentication request '{}'", client.getId(), authReqId);
throw new AuthenticationRequestNotFoundException("Authentication request not found");
}
return cibaRequest;
}).map(cibaRequest -> {
// set resource owner
tokenRequest1.setSubject(cibaRequest.getSubject());
// set original scopes
tokenRequest1.setScopes(cibaRequest.getScopes());
// store only the AuthenticationFlowContext.data attributes in order to simplify EL templating
// and provide an up to date set of data if the enrichAuthFlow Policy ius used multiple time in a step
// {#context.attributes['authFlow']['entry']}
tokenRequest1.getContext().put(AUTH_FLOW_CONTEXT_ATTRIBUTES_KEY, emptyMap());
return tokenRequest1;
}));
}
Aggregations