Restructure code base

This commit is contained in:
Pim Kunis 2021-08-06 23:14:27 +02:00
parent 1e53776a8d
commit f50f08061c
23 changed files with 245 additions and 213 deletions

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.AccountController do
defmodule MatrixServerWeb.Client.AccountController do
use MatrixServerWeb, :controller
import MatrixServer

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.AliasesController do
defmodule MatrixServerWeb.Client.AliasesController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.InfoController do
defmodule MatrixServerWeb.Client.InfoController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error

View file

@ -0,0 +1,57 @@
defmodule MatrixServerWeb.Client.LoginController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error
import Ecto.Changeset
alias MatrixServer.{Repo, Account}
alias MatrixServerWeb.Request.Login
alias Ecto.Changeset
@login_type "m.login.password"
def login_types(conn, _params) do
data = %{flows: [%{type: @login_type}]}
conn
|> put_status(200)
|> json(data)
end
def login(
conn,
%{"type" => @login_type, "identifier" => %{"type" => "m.id.user"}} = params
) do
case Login.changeset(params) do
%Changeset{valid?: true} = cs ->
input = apply_changes(cs)
case Account.login(input) |> Repo.transaction() do
{:ok, device} ->
data = %{
user_id: MatrixServer.get_mxid(device.localpart),
access_token: device.access_token,
device_id: device.device_id
}
conn
|> put_status(200)
|> json(data)
{:error, error} when is_atom(error) ->
put_error(conn, error)
{:error, _} ->
put_error(conn, :unknown)
end
_ ->
put_error(conn, :bad_json)
end
end
def login(conn, _params) do
# Other login types and identifiers are unsupported for now.
put_error(conn, :unrecognized, "Only m.login.password is supported currently.")
end
end

View file

@ -1,15 +1,14 @@
defmodule MatrixServerWeb.AuthController do
defmodule MatrixServerWeb.Client.RegisterController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error
import Ecto.Changeset
alias MatrixServer.{Repo, Account}
alias MatrixServerWeb.API.{Register, Login}
alias MatrixServerWeb.Request.Register
alias Ecto.Changeset
@register_type "m.login.dummy"
@login_type "m.login.password"
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
case Register.changeset(params) do
@ -58,49 +57,4 @@ defmodule MatrixServerWeb.AuthController do
|> put_status(401)
|> json(data)
end
def login_types(conn, _params) do
data = %{flows: [%{type: @login_type}]}
conn
|> put_status(200)
|> json(data)
end
def login(
conn,
%{"type" => @login_type, "identifier" => %{"type" => "m.id.user"}} = params
) do
case Login.changeset(params) do
%Changeset{valid?: true} = cs ->
input = apply_changes(cs)
case Account.login(input) |> Repo.transaction() do
{:ok, device} ->
data = %{
user_id: MatrixServer.get_mxid(device.localpart),
access_token: device.access_token,
device_id: device.device_id
}
conn
|> put_status(200)
|> json(data)
{:error, error} when is_atom(error) ->
put_error(conn, error)
{:error, _} ->
put_error(conn, :unknown)
end
_ ->
put_error(conn, :bad_json)
end
end
def login(conn, _params) do
# Other login types and identifiers are unsupported for now.
put_error(conn, :unrecognized, "Only m.login.password is supported currently.")
end
end

View file

@ -1,11 +1,11 @@
defmodule MatrixServerWeb.RoomController do
defmodule MatrixServerWeb.Client.RoomController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error
import Ecto.Changeset
alias MatrixServer.Room
alias MatrixServerWeb.API.{CreateRoom}
alias MatrixServerWeb.Request.{CreateRoom}
alias Ecto.Changeset
alias Plug.Conn

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.KeyController do
defmodule MatrixServerWeb.Federation.KeyController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.API.CreateRoom do
defmodule MatrixServerWeb.Request.CreateRoom do
use Ecto.Schema
import Ecto.Changeset

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.API.Login do
defmodule MatrixServerWeb.Request.Login do
use Ecto.Schema
import Ecto.Changeset

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.API.Register do
defmodule MatrixServerWeb.Request.Register do
use Ecto.Schema
import Ecto.Changeset

View file

@ -7,30 +7,37 @@ defmodule MatrixServerWeb.Router do
plug :accepts, ["json"]
end
pipeline :authenticated do
pipeline :authenticate_client do
plug :accepts, ["json"]
plug Authenticate
end
pipeline :authenticate_server do
plug :accepts, ["json"]
# TODO: Add plug to verify peer.
end
scope "/_matrix", MatrixServerWeb do
pipe_through :public
scope "/client/r0" do
post "/register", AuthController, :register
get "/register/available", AccountController, :available
get "/login", AuthController, :login_types
post "/login", AuthController, :login
scope "/client", Client do
scope "/r0" do
post "/register", RegisterController, :register
get "/register/available", AccountController, :available
get "/login", LoginController, :login_types
post "/login", LoginController, :login
end
get "/versions", InfoController, :versions
end
scope "/key/v2" do
scope "/key/v2", Federation do
get "/server", KeyController, :get_signing_keys
end
get "/client/versions", InfoController, :versions
end
scope "/_matrix", MatrixServerWeb do
pipe_through :authenticated
scope "/_matrix", MatrixServerWeb.Client do
pipe_through :authenticate_client
scope "/client/r0" do
get "/account/whoami", AccountController, :whoami
@ -44,7 +51,11 @@ defmodule MatrixServerWeb.Router do
end
end
scope "/", MatrixServerWeb do
scope "/_matrix", MatrixServerWeb.Federation do
end
scope "/", MatrixServerWeb.Client do
match :*, "/*path", InfoController, :unrecognized
end
end