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

@ -8,7 +8,7 @@ config :matrix_server, MatrixServer.Repo,
username: "matrix_server", username: "matrix_server",
password: "matrix_server", password: "matrix_server",
database: "matrix_server_dev", database: "matrix_server_dev",
hostname: "localhost", hostname: hostname,
show_sensitive_data_on_connection_error: true, show_sensitive_data_on_connection_error: true,
pool_size: 10 pool_size: 10

View file

@ -1,5 +1,8 @@
use Mix.Config use Mix.Config
hostname = "localhost"
port = System.get_env("PORT") || 4000
# Configure your database # Configure your database
# #
# The MIX_TEST_PARTITION environment variable can be used # The MIX_TEST_PARTITION environment variable can be used
@ -9,16 +12,17 @@ config :matrix_server, MatrixServer.Repo,
username: "matrix_server", username: "matrix_server",
password: "matrix_server", password: "matrix_server",
database: "matrix_server_test#{System.get_env("MIX_TEST_PARTITION")}", database: "matrix_server_test#{System.get_env("MIX_TEST_PARTITION")}",
hostname: "localhost", hostname: hostname,
pool: Ecto.Adapters.SQL.Sandbox pool: Ecto.Adapters.SQL.Sandbox
# We don't run a server during test. If one is required, # We don't run a server during test. If one is required,
# you can enable the server option below. # you can enable the server option below.
config :matrix_server, MatrixServerWeb.Endpoint, config :matrix_server, MatrixServerWeb.Endpoint,
http: [port: 4002], http: [port: port],
server: false server: false
# Print only warnings and errors during test # Print only warnings and errors during test
config :logger, level: :warn config :logger, level: :warn
config :matrix_server, :server_name, "localhost" config :matrix_server, server_name: "#{hostname}:#{port}"
config :matrix_server, private_key_file: "keys/id_ed25519"

View file

@ -2,7 +2,7 @@ defmodule MatrixServer.QuickCheck do
import Ecto.Query import Ecto.Query
alias MatrixServer.{Repo, Room, Account, RoomServer} alias MatrixServer.{Repo, Room, Account, RoomServer}
alias MatrixServerWeb.API.CreateRoom alias MatrixServerWeb.Request.CreateRoom
def create_room(name \\ nil, topic \\ nil) do def create_room(name \\ nil, topic \\ nil) do
account = Repo.one!(from a in Account, limit: 1) account = Repo.one!(from a in Account, limit: 1)

View file

@ -5,7 +5,7 @@ defmodule MatrixServer.RoomServer do
import Ecto.Changeset import Ecto.Changeset
alias MatrixServer.{Repo, Room, Event, StateResolution} alias MatrixServer.{Repo, Room, Event, StateResolution}
alias MatrixServerWeb.API.CreateRoom alias MatrixServerWeb.Request.CreateRoom
alias MatrixServer.StateResolution.Authorization alias MatrixServer.StateResolution.Authorization
@registry MatrixServer.RoomServer.Registry @registry MatrixServer.RoomServer.Registry

View file

@ -4,7 +4,7 @@ defmodule MatrixServer.Account do
import Ecto.{Changeset, Query} import Ecto.{Changeset, Query}
alias MatrixServer.{Repo, Account, Device} alias MatrixServer.{Repo, Account, Device}
alias MatrixServerWeb.API.{Register, Login} alias MatrixServerWeb.Request.{Register, Login}
alias Ecto.Multi alias Ecto.Multi
@max_mxid_length 255 @max_mxid_length 255

View file

@ -4,7 +4,7 @@ defmodule MatrixServer.Device do
import Ecto.{Changeset, Query} import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo} alias MatrixServer.{Account, Device, Repo}
alias MatrixServerWeb.API.Login alias MatrixServerWeb.Request.Login
@primary_key false @primary_key false
schema "devices" do schema "devices" do

View file

@ -5,7 +5,7 @@ defmodule MatrixServer.Room do
import Ecto.Query import Ecto.Query
alias MatrixServer.{Repo, Room, Event, Alias, RoomServer} alias MatrixServer.{Repo, Room, Event, Alias, RoomServer}
alias MatrixServerWeb.API.CreateRoom alias MatrixServerWeb.Request.CreateRoom
@primary_key {:id, :string, []} @primary_key {:id, :string, []}
schema "rooms" do schema "rooms" do

View file

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

View file

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

View file

@ -1,4 +1,4 @@
defmodule MatrixServerWeb.InfoController do defmodule MatrixServerWeb.Client.InfoController do
use MatrixServerWeb, :controller use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error 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 use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error import MatrixServerWeb.Plug.Error
import Ecto.Changeset import Ecto.Changeset
alias MatrixServer.{Repo, Account} alias MatrixServer.{Repo, Account}
alias MatrixServerWeb.API.{Register, Login} alias MatrixServerWeb.Request.Register
alias Ecto.Changeset alias Ecto.Changeset
@register_type "m.login.dummy" @register_type "m.login.dummy"
@login_type "m.login.password"
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
case Register.changeset(params) do case Register.changeset(params) do
@ -58,49 +57,4 @@ defmodule MatrixServerWeb.AuthController do
|> put_status(401) |> put_status(401)
|> json(data) |> json(data)
end 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 end

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,135 +0,0 @@
defmodule MatrixServerWeb.AuthControllerTest do
use MatrixServerWeb.ConnCase
import Ecto.Query
alias MatrixServer.{Repo, Device, Factory}
alias MatrixServerWeb.Endpoint
@basic_params %{
"username" => "user",
"password" => "lemmein",
"auth" => %{"type" => "m.login.dummy"}
}
describe "register endpoint" do
test "renders the auth flow when no auth parameter is given", %{conn: conn} do
conn = post(conn, Routes.auth_path(conn, :register))
assert %{"flows" => flows, "params" => _} = json_response(conn, 401)
assert is_list(flows)
end
test "registers account with minimal information", %{conn: conn} do
conn = post_json(conn, Routes.auth_path(Endpoint, :register), @basic_params)
user_id = MatrixServer.get_mxid("user")
assert %{"access_token" => _, "device_id" => _, "user_id" => ^user_id} =
json_response(conn, 200)
end
test "registers and sets device id", %{conn: conn} do
params = Map.put(@basic_params, :device_id, "android")
conn = post_json(conn, Routes.auth_path(Endpoint, :register), params)
assert %{"device_id" => "android"} = json_response(conn, 200)
end
test "registers and sets display name", %{conn: conn} do
params = Map.put(@basic_params, :initial_device_display_name, "My Android")
conn = post_json(conn, Routes.auth_path(Endpoint, :register), params)
assert json_response(conn, 200)
assert Repo.one!(from d in Device, select: d.display_name) == "My Android"
end
test "rejects account if localpart is already in use", %{conn: conn} do
Factory.insert(:account, localpart: "sneed")
conn =
post_json(conn, Routes.auth_path(Endpoint, :register), %{
@basic_params
| "username" => "sneed"
})
assert %{"errcode" => "M_USER_IN_USE"} = json_response(conn, 400)
end
test "obeys inhibit_login parameter", %{conn: conn} do
params = Map.put(@basic_params, :inhibit_login, "true")
conn = post_json(conn, Routes.auth_path(Endpoint, :register), params)
assert response = json_response(conn, 200)
refute Map.has_key?(response, "access_token")
refute Map.has_key?(response, "device_id")
end
test "generates localpart if omitted", %{conn: conn} do
params = Map.delete(@basic_params, "username")
conn = post_json(conn, Routes.auth_path(Endpoint, :register), params)
assert %{"user_id" => _} = json_response(conn, 200)
end
test "rejects invalid usernames", %{conn: conn} do
conn =
post_json(conn, Routes.auth_path(Endpoint, :register), %{
@basic_params
| "username" => "User1"
})
assert %{"errcode" => "M_INVALID_USERNAME"} = json_response(conn, 400)
end
end
@basic_params %{
"type" => "m.login.password",
"identifier" => %{
"type" => "m.id.user",
"user" => "sneed"
},
"password" => "lemmein"
}
describe "login endpoint" do
test "renders the list of login types", %{conn: conn} do
conn = get(conn, Routes.auth_path(Endpoint, :login))
assert %{"flows" => flows} = json_response(conn, 200)
assert is_list(flows)
end
test "logs a user in with password and matrix user id", %{conn: conn} do
Factory.insert(:account, localpart: "sneed", password_hash: Bcrypt.hash_pwd_salt("lemmein"))
conn = post_json(conn, Routes.auth_path(Endpoint, :login), @basic_params)
assert %{"user_id" => _, "access_token" => _, "device_id" => _} = json_response(conn, 200)
conn =
recycle(conn)
|> post_json(Routes.auth_path(Endpoint, :login), %{
@basic_params
| "identifier" => %{"type" => "m.id.user", "user" => MatrixServer.get_mxid("sneed")}
})
assert %{"user_id" => _, "access_token" => _, "device_id" => _} = json_response(conn, 200)
end
test "handles unknown matrix user id", %{conn: conn} do
conn = post_json(conn, Routes.auth_path(Endpoint, :login), @basic_params)
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
end
test "handles wrong password", %{conn: conn} do
Factory.insert(:account, localpart: "sneed", password_hash: Bcrypt.hash_pwd_salt("surprise"))
conn = post_json(conn, Routes.auth_path(Endpoint, :login), @basic_params)
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
end
# TODO: Test display name
# TODO: Test device recycling
end
end

View file

@ -0,0 +1,57 @@
defmodule MatrixServerWeb.LoginControllerTest do
use MatrixServerWeb.ConnCase
alias MatrixServer.Factory
alias MatrixServerWeb.Endpoint
@basic_params %{
"type" => "m.login.password",
"identifier" => %{
"type" => "m.id.user",
"user" => "sneed"
},
"password" => "lemmein"
}
describe "login endpoint" do
test "renders the list of login types", %{conn: conn} do
conn = get(conn, Routes.login_path(Endpoint, :login))
assert %{"flows" => flows} = json_response(conn, 200)
assert is_list(flows)
end
test "logs a user in with password and matrix user id", %{conn: conn} do
Factory.insert(:account, localpart: "sneed", password_hash: Bcrypt.hash_pwd_salt("lemmein"))
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
assert %{"user_id" => _, "access_token" => _, "device_id" => _} = json_response(conn, 200)
conn =
recycle(conn)
|> post_json(Routes.login_path(Endpoint, :login), %{
@basic_params
| "identifier" => %{"type" => "m.id.user", "user" => MatrixServer.get_mxid("sneed")}
})
assert %{"user_id" => _, "access_token" => _, "device_id" => _} = json_response(conn, 200)
end
test "handles unknown matrix user id", %{conn: conn} do
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
end
test "handles wrong password", %{conn: conn} do
Factory.insert(:account, localpart: "sneed", password_hash: Bcrypt.hash_pwd_salt("surprise"))
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
end
# TODO: Test display name
# TODO: Test device recycling
end
end

View file

@ -0,0 +1,84 @@
defmodule MatrixServerWeb.RegisterControllerTest do
use MatrixServerWeb.ConnCase
import Ecto.Query
alias MatrixServer.{Repo, Device, Factory}
alias MatrixServerWeb.Endpoint
@basic_params %{
"username" => "user",
"password" => "lemmein",
"auth" => %{"type" => "m.login.dummy"}
}
describe "register endpoint" do
test "renders the auth flow when no auth parameter is given", %{conn: conn} do
conn = post(conn, Routes.register_path(conn, :register))
assert %{"flows" => flows, "params" => _} = json_response(conn, 401)
assert is_list(flows)
end
test "registers account with minimal information", %{conn: conn} do
conn = post_json(conn, Routes.register_path(Endpoint, :register), @basic_params)
user_id = MatrixServer.get_mxid("user")
assert %{"access_token" => _, "device_id" => _, "user_id" => ^user_id} =
json_response(conn, 200)
end
test "registers and sets device id", %{conn: conn} do
params = Map.put(@basic_params, :device_id, "android")
conn = post_json(conn, Routes.register_path(Endpoint, :register), params)
assert %{"device_id" => "android"} = json_response(conn, 200)
end
test "registers and sets display name", %{conn: conn} do
params = Map.put(@basic_params, :initial_device_display_name, "My Android")
conn = post_json(conn, Routes.register_path(Endpoint, :register), params)
assert json_response(conn, 200)
assert Repo.one!(from d in Device, select: d.display_name) == "My Android"
end
test "rejects account if localpart is already in use", %{conn: conn} do
Factory.insert(:account, localpart: "sneed")
conn =
post_json(conn, Routes.register_path(Endpoint, :register), %{
@basic_params
| "username" => "sneed"
})
assert %{"errcode" => "M_USER_IN_USE"} = json_response(conn, 400)
end
test "obeys inhibit_login parameter", %{conn: conn} do
params = Map.put(@basic_params, :inhibit_login, "true")
conn = post_json(conn, Routes.register_path(Endpoint, :register), params)
assert response = json_response(conn, 200)
refute Map.has_key?(response, "access_token")
refute Map.has_key?(response, "device_id")
end
test "generates localpart if omitted", %{conn: conn} do
params = Map.delete(@basic_params, "username")
conn = post_json(conn, Routes.register_path(Endpoint, :register), params)
assert %{"user_id" => _} = json_response(conn, 200)
end
test "rejects invalid usernames", %{conn: conn} do
conn =
post_json(conn, Routes.register_path(Endpoint, :register), %{
@basic_params
| "username" => "User1"
})
assert %{"errcode" => "M_INVALID_USERNAME"} = json_response(conn, 400)
end
end
end