Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2019-2020 Moxley Stratton, Mike Buhot <https://github.com/open-api-spex/open_api_spex>, MPL-2.0
  3. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  4. # SPDX-License-Identifier: AGPL-3.0-only
  5. defmodule Pleroma.Web.ApiSpec.CastAndValidate do
  6. @moduledoc """
  7. This plug is based on [`OpenApiSpex.Plug.CastAndValidate`]
  8. (https://github.com/open-api-spex/open_api_spex/blob/master/lib/open_api_spex/plug/cast_and_validate.ex).
  9. The main difference is ignoring unexpected query params instead of throwing
  10. an error and a config option (`[Pleroma.Web.ApiSpec.CastAndValidate, :strict]`)
  11. to disable this behavior. Also, the default rendering error module
  12. is `Pleroma.Web.ApiSpec.RenderError`.
  13. """
  14. @behaviour Plug
  15. alias OpenApiSpex.Plug.PutApiSpec
  16. alias Plug.Conn
  17. @impl Plug
  18. def init(opts) do
  19. opts
  20. |> Map.new()
  21. |> Map.put_new(:render_error, Pleroma.Web.ApiSpec.RenderError)
  22. end
  23. @impl Plug
  24. def call(conn, %{operation_id: operation_id, render_error: render_error}) do
  25. {spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
  26. operation = operation_lookup[operation_id]
  27. content_type =
  28. case Conn.get_req_header(conn, "content-type") do
  29. [header_value | _] ->
  30. header_value
  31. |> String.split(";")
  32. |> List.first()
  33. _ ->
  34. "application/json"
  35. end
  36. conn = Conn.put_private(conn, :operation_id, operation_id)
  37. case cast_and_validate(spec, operation, conn, content_type, strict?()) do
  38. {:ok, conn} ->
  39. conn
  40. {:error, reason} ->
  41. opts = render_error.init(reason)
  42. conn
  43. |> render_error.call(opts)
  44. |> Plug.Conn.halt()
  45. end
  46. end
  47. def call(
  48. %{
  49. private: %{
  50. phoenix_controller: controller,
  51. phoenix_action: action,
  52. open_api_spex: %{spec_module: spec_module}
  53. }
  54. } = conn,
  55. opts
  56. ) do
  57. {spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
  58. operation =
  59. case operation_lookup[{controller, action}] do
  60. nil ->
  61. operation_id = controller.open_api_operation(action).operationId
  62. operation = operation_lookup[operation_id]
  63. operation_lookup = Map.put(operation_lookup, {controller, action}, operation)
  64. OpenApiSpex.Plug.Cache.adapter().put(spec_module, {spec, operation_lookup})
  65. operation
  66. operation ->
  67. operation
  68. end
  69. if operation.operationId do
  70. call(conn, Map.put(opts, :operation_id, operation.operationId))
  71. else
  72. raise "operationId was not found in action API spec"
  73. end
  74. end
  75. def call(conn, opts), do: OpenApiSpex.Plug.CastAndValidate.call(conn, opts)
  76. defp cast_and_validate(spec, operation, conn, content_type, true = _strict) do
  77. OpenApiSpex.cast_and_validate(spec, operation, conn, content_type)
  78. end
  79. defp cast_and_validate(spec, operation, conn, content_type, false = _strict) do
  80. case OpenApiSpex.cast_and_validate(spec, operation, conn, content_type) do
  81. {:ok, conn} ->
  82. {:ok, conn}
  83. # Remove unexpected query params and cast/validate again
  84. {:error, errors} ->
  85. query_params =
  86. Enum.reduce(errors, conn.query_params, fn
  87. %{reason: :unexpected_field, name: name, path: [name]}, params ->
  88. Map.delete(params, name)
  89. # Filter out empty params
  90. %{reason: :invalid_type, path: [name_atom], value: ""}, params ->
  91. Map.delete(params, to_string(name_atom))
  92. %{reason: :invalid_enum, name: nil, path: path, value: value}, params ->
  93. path = path |> Enum.reverse() |> tl() |> Enum.reverse() |> list_items_to_string()
  94. update_in(params, path, &List.delete(&1, value))
  95. _, params ->
  96. params
  97. end)
  98. conn = %Conn{conn | query_params: query_params}
  99. OpenApiSpex.cast_and_validate(spec, operation, conn, content_type)
  100. end
  101. end
  102. defp list_items_to_string(list) do
  103. Enum.map(list, fn
  104. i when is_atom(i) -> to_string(i)
  105. i -> i
  106. end)
  107. end
  108. defp strict?, do: Pleroma.Config.get([__MODULE__, :strict], false)
  109. end