Skip to content

Commit

Permalink
Decode group_name by GroupNameDecoder
Browse files Browse the repository at this point in the history
- Use GroupNameDecoder (Plug) to decode group_name instead implementation inside controllers

FIX #904

Signed-off-by: Gabriele Ghio <[email protected]>
  • Loading branch information
shinnokdisengir committed Mar 18, 2024
1 parent c83724f commit 01e0fff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ defmodule Astarte.AppEngine.APIWeb.DeviceStatusByGroupController do
conn,
%{"realm_name" => realm_name, "group_name" => group_name, "details" => "true"} = params
) do
decoded_group_name = URI.decode(group_name)
group_name

with {:ok, %DevicesList{} = devices_list} <-
Groups.list_detailed_devices(realm_name, decoded_group_name, params) do
Groups.list_detailed_devices(realm_name, group_name, params) do
render(conn, "detailed_index.json", devices_list: devices_list, request: params)
end
end

def index(conn, %{"realm_name" => realm_name, "group_name" => group_name} = params) do
decoded_group_name = URI.decode(group_name)

with {:ok, %DevicesList{} = devices_list} <-
Groups.list_devices(realm_name, decoded_group_name, params) do
Groups.list_devices(realm_name, group_name, params) do
render(conn, "index.json", devices_list: devices_list, request: params)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,13 @@ defmodule Astarte.AppEngine.APIWeb.GroupsController do
end

def show(conn, %{"realm_name" => realm_name, "group_name" => group_name}) do
# group_name is urlencoded to allow characters like / to be used in the
# group name
decoded_group_name = URI.decode(group_name)

with {:ok, group} <- Groups.get_group(realm_name, decoded_group_name) do
with {:ok, group} <- Groups.get_group(realm_name, group_name) do
render(conn, "show.json", group: group)
end
end

def add_device(conn, %{"realm_name" => realm_name, "group_name" => group_name, "data" => params}) do
decoded_group_name = URI.decode(group_name)

with :ok <- Groups.add_device(realm_name, decoded_group_name, params) do
with :ok <- Groups.add_device(realm_name, group_name, params) do
send_resp(conn, :created, "")
end
end
Expand All @@ -61,9 +55,7 @@ defmodule Astarte.AppEngine.APIWeb.GroupsController do
"group_name" => group_name,
"device_id" => device_id
}) do
decoded_group_name = URI.decode(group_name)

with :ok <- Groups.remove_device(realm_name, decoded_group_name, device_id) do
with :ok <- Groups.remove_device(realm_name, group_name, device_id) do
send_resp(conn, :no_content, "")
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Astarte.AppEngine.APIWeb.Plug.GroupNameDecoder do
@moduledoc """
This plug decodes a group name, which may have been coded
to remove the forward slash
"""
def init(default), do: default

def call(%Plug.Conn{path_params: %{"group_name" => group_name}} = conn, _) do
%Plug.Conn{conn | path_params: %{conn.path_params | "group_name" => URI.decode(group_name)}}
end

def call(conn, _), do: conn
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ defmodule Astarte.AppEngine.APIWeb.Router do
plug Astarte.AppEngine.APIWeb.Plug.JoinPath
plug Astarte.AppEngine.APIWeb.Plug.LogInterface
plug Astarte.AppEngine.APIWeb.Plug.LogPath
plug Astarte.AppEngine.APIWeb.Plug.Telemetry.CallsCount
plug Astarte.AppEngine.APIWeb.Plug.GroupNameDecoder
end

pipeline :swagger do
Expand Down

0 comments on commit 01e0fff

Please sign in to comment.