/**
   * Create a new authentication flow
   *
   * @param flow Authentication flow representation
   * @return
   */
  @Path("/flows")
  @POST
  @NoCache
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createFlow(AuthenticationFlowRepresentation flow) {
    auth.requireManage();

    if (flow.getAlias() == null || flow.getAlias().isEmpty()) {
      return ErrorResponse.exists("Failed to create flow with empty alias name");
    }

    if (realm.getFlowByAlias(flow.getAlias()) != null) {
      return ErrorResponse.exists("Flow " + flow.getAlias() + " already exists");
    }

    AuthenticationFlowModel createdModel =
        realm.addAuthenticationFlow(RepresentationToModel.toModel(flow));

    flow.setId(createdModel.getId());
    adminEvent
        .operation(OperationType.CREATE)
        .resourcePath(uriInfo, createdModel.getId())
        .representation(flow)
        .success();
    return Response.status(201).build();
  }
  /**
   * Create new authenticator configuration
   *
   * @param rep JSON describing new authenticator configuration
   * @deprecated Use {@link #newExecutionConfig(String, AuthenticatorConfigRepresentation)} instead
   */
  @Path("config")
  @POST
  @NoCache
  public Response createAuthenticatorConfig(AuthenticatorConfigRepresentation rep) {
    auth.requireManage();

    AuthenticatorConfigModel config =
        realm.addAuthenticatorConfig(RepresentationToModel.toModel(rep));
    adminEvent
        .operation(OperationType.CREATE)
        .resourcePath(uriInfo, config.getId())
        .representation(rep)
        .success();
    return Response.created(uriInfo.getAbsolutePathBuilder().path(config.getId()).build()).build();
  }
  /**
   * Update execution with new configuration
   *
   * @param execution Execution id
   * @param json JSON with new configuration
   * @return
   */
  @Path("/executions/{executionId}/config")
  @POST
  @NoCache
  @Consumes(MediaType.APPLICATION_JSON)
  public Response newExecutionConfig(
      @PathParam("executionId") String execution, AuthenticatorConfigRepresentation json) {
    auth.requireManage();

    AuthenticationExecutionModel model = realm.getAuthenticationExecutionById(execution);
    if (model == null) {
      session.getTransaction().setRollbackOnly();
      throw new NotFoundException("Illegal execution");
    }
    AuthenticatorConfigModel config = RepresentationToModel.toModel(json);
    config = realm.addAuthenticatorConfig(config);
    model.setAuthenticatorConfig(config.getId());
    realm.updateAuthenticatorExecution(model);

    json.setId(config.getId());
    adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo).representation(json).success();
    return Response.created(uriInfo.getAbsolutePathBuilder().path(config.getId()).build()).build();
  }
  /**
   * Add new authentication execution
   *
   * @param execution JSON model describing authentication execution
   */
  @Path("/executions")
  @POST
  @NoCache
  @Consumes(MediaType.APPLICATION_JSON)
  public Response addExecution(AuthenticationExecutionRepresentation execution) {
    auth.requireManage();

    AuthenticationExecutionModel model = RepresentationToModel.toModel(realm, execution);
    AuthenticationFlowModel parentFlow = getParentFlow(model);
    if (parentFlow.isBuiltIn()) {
      throw new BadRequestException("It is illegal to add execution to a built in flow");
    }
    model.setPriority(getNextPriority(parentFlow));
    model = realm.addAuthenticatorExecution(model);

    adminEvent
        .operation(OperationType.CREATE)
        .resourcePath(uriInfo, model.getId())
        .representation(execution)
        .success();
    return Response.created(uriInfo.getAbsolutePathBuilder().path(model.getId()).build()).build();
  }