Add plug to generate Matrix error

This commit is contained in:
Pim Kunis 2021-06-26 01:14:09 +02:00
parent dac1a429b9
commit 1ec18163c1
6 changed files with 47 additions and 50 deletions

View file

@ -30,17 +30,6 @@ defmodule MatrixServer do
|> validate_required(required) |> validate_required(required)
end end
def generate_error(errcode) do
{get_errcode_status(errcode), %{errcode: errcode, error: get_errcode_error(errcode)}}
end
# TODO: make a plug for this?
def get_errcode_error("M_BAD_JSON"), do: "Bad request."
def get_errcode_error("M_USER_IN_USE"), do: "Username is already taken."
def get_errcode_error("M_INVALID_USERNAME"), do: "Invalid username."
def get_errcode_status(_), do: 400
def get_mxid(localpart) when is_binary(localpart) do def get_mxid(localpart) when is_binary(localpart) do
"@#{localpart}:#{server_name()}" "@#{localpart}:#{server_name()}"
end end

View file

@ -1,7 +1,8 @@
defmodule MatrixServerWeb.AccountController do defmodule MatrixServerWeb.AccountController do
use MatrixServerWeb, :controller use MatrixServerWeb, :controller
import MatrixServer, only: [get_mxid: 1] import MatrixServer
import MatrixServerWeb.Plug.Error
alias MatrixServer.Account alias MatrixServer.Account
alias Plug.Conn alias Plug.Conn
@ -9,21 +10,15 @@ defmodule MatrixServerWeb.AccountController do
def available(conn, params) do def available(conn, params) do
localpart = Map.get(params, "username", "") localpart = Map.get(params, "username", "")
{status, data} =
case Account.available?(localpart) do case Account.available?(localpart) do
:ok -> :ok ->
{200, %{available: true}}
{:error, :user_in_use} ->
{400, %{errcode: "M_USER_IN_USE", error: "Desired user ID is already taken."}}
{:error, :invalid_username} ->
{400, %{errocode: "M_INVALID_USERNAME", error: "Desired user ID is invalid."}}
end
conn conn
|> put_status(status) |> put_status(200)
|> json(data) |> json(%{available: true})
{:error, error} ->
put_error(conn, error)
end
end end
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do

View file

@ -2,6 +2,7 @@ defmodule MatrixServerWeb.AuthController do
use MatrixServerWeb, :controller use MatrixServerWeb, :controller
import MatrixServer import MatrixServer
import MatrixServerWeb.Plug.Error
alias MatrixServer.{Repo, Account} alias MatrixServer.{Repo, Account}
alias Ecto.Changeset alias Ecto.Changeset
@ -38,7 +39,7 @@ defmodule MatrixServerWeb.AuthController do
{200, data} {200, data}
{:error, error} -> {:error, error} ->
generate_error(error) put_error(conn, error)
end end
conn conn
@ -48,11 +49,7 @@ defmodule MatrixServerWeb.AuthController do
def register(conn, %{"auth" => _}) do def register(conn, %{"auth" => _}) do
# Other login types are unsupported for now. # Other login types are unsupported for now.
data = %{errcode: "M_FORBIDDEN", error: "Login type not supported"} put_error(conn, :forbidden)
conn
|> put_status(400)
|> json(data)
end end
def register(conn, _params) do def register(conn, _params) do
@ -80,9 +77,9 @@ defmodule MatrixServerWeb.AuthController do
end end
defp get_register_error(%Changeset{errors: [error | _]}), do: get_register_error(error) defp get_register_error(%Changeset{errors: [error | _]}), do: get_register_error(error)
defp get_register_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: "M_USER_IN_USE" defp get_register_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
defp get_register_error({:localpart, {_, [{:validation, _} | _]}}), do: "M_INVALID_USERNAME" defp get_register_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
defp get_register_error(_), do: "M_BAD_JSON" defp get_register_error(_), do: :bad_json
defp register_schema do defp register_schema do
types = %{ types = %{

View file

@ -1,11 +1,12 @@
defmodule MatrixServerWeb.Authenticate do defmodule MatrixServerWeb.Plug.Authenticate do
import MatrixServerWeb.Plug.Error
import Plug.Conn import Plug.Conn
import Phoenix.Controller, only: [json: 2] import Phoenix.Controller, only: [json: 2]
alias MatrixServer.Account alias MatrixServer.Account
alias Plug.Conn alias Plug.Conn
def init(options), do: options def init(opts), do: opts
def call(%Conn{params: %{"access_token" => access_token}} = conn, _opts) do def call(%Conn{params: %{"access_token" => access_token}} = conn, _opts) do
authenticate(conn, access_token) authenticate(conn, access_token)
@ -17,12 +18,7 @@ defmodule MatrixServerWeb.Authenticate do
authenticate(conn, access_token) authenticate(conn, access_token)
_ -> _ ->
data = %{errcode: "M_MISSING_TOKEN", error: "Access token missing."} put_error(conn, :missing_token)
conn
|> put_status(401)
|> json(data)
|> halt()
end end
end end
@ -34,12 +30,7 @@ defmodule MatrixServerWeb.Authenticate do
|> assign(:device, device) |> assign(:device, device)
nil -> nil ->
data = %{errcode: "M_UNKNOWN_TOKEN", error: "Invalid access token."} put_error(conn, :unknown_token)
conn
|> put_status(401)
|> json(data)
|> halt()
end end
end end
end end

View file

@ -0,0 +1,23 @@
defmodule MatrixServerWeb.Plug.Error do
import Plug.Conn
import Phoenix.Controller, only: [json: 2]
@error_code_and_message %{
bad_json: {400, "M_BAD_JSON", "Bad request."},
user_in_use: {400, "M_USE_IN_USE", "Username is already taken."},
invalid_username: {400, "M_INVALID_USERNAME", "Invalid username."},
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
missing_token: {401, "M_MISSING_TOKEN", "Access token required."}
}
def put_error(conn, error) do
{status, errcode, errmsg} = @error_code_and_message[error]
data = %{errcode: errcode, error: errmsg}
conn
|> put_status(status)
|> json(data)
|> halt()
end
end

View file

@ -1,13 +1,15 @@
defmodule MatrixServerWeb.Router do defmodule MatrixServerWeb.Router do
use MatrixServerWeb, :router use MatrixServerWeb, :router
alias MatrixServerWeb.Plug.Authenticate
pipeline :public do pipeline :public do
plug :accepts, ["json"] plug :accepts, ["json"]
end end
pipeline :authenticated do pipeline :authenticated do
plug :accepts, ["json"] plug :accepts, ["json"]
plug MatrixServerWeb.Authenticate plug Authenticate
end end
scope "/_matrix", MatrixServerWeb do scope "/_matrix", MatrixServerWeb do