Create tesla middleware for adding signature to federation requests
Change repo structure
This commit is contained in:
parent
214293323c
commit
be9860f7d0
23 changed files with 83 additions and 58 deletions
35
lib/matrix_server_web/client/authenticate_client_plug.ex
Normal file
35
lib/matrix_server_web/client/authenticate_client_plug.ex
Normal file
|
@ -0,0 +1,35 @@
|
|||
defmodule MatrixServerWeb.Client.AuthenticateClientPlug do
|
||||
import MatrixServerWeb.Error
|
||||
import Plug.Conn
|
||||
|
||||
alias MatrixServer.Account
|
||||
alias Plug.Conn
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(%Conn{params: %{"access_token" => access_token}} = conn, _opts) do
|
||||
authenticate(conn, access_token)
|
||||
end
|
||||
|
||||
def call(%Conn{req_headers: headers} = conn, _opts) do
|
||||
case List.keyfind(headers, "authorization", 0) do
|
||||
{_, "Bearer " <> access_token} ->
|
||||
authenticate(conn, access_token)
|
||||
|
||||
_ ->
|
||||
put_error(conn, :missing_token)
|
||||
end
|
||||
end
|
||||
|
||||
defp authenticate(conn, access_token) do
|
||||
case Account.by_access_token(access_token) do
|
||||
{account, device} ->
|
||||
conn
|
||||
|> assign(:account, account)
|
||||
|> assign(:device, device)
|
||||
|
||||
nil ->
|
||||
put_error(conn, :unknown_token)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,7 +2,7 @@ defmodule MatrixServerWeb.Client.AccountController do
|
|||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServer
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
|
||||
alias MatrixServer.{Account, Repo}
|
||||
alias Plug.Conn
|
|
@ -1,7 +1,7 @@
|
|||
defmodule MatrixServerWeb.Client.AliasesController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
|
||||
alias MatrixServer.Alias
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
defmodule MatrixServerWeb.Client.InfoController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
|
||||
@supported_versions ["r0.6.1"]
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
defmodule MatrixServerWeb.Client.LoginController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MatrixServer.{Repo, Account}
|
||||
alias MatrixServerWeb.Request.Login
|
||||
alias MatrixServerWeb.Client.Request.Login
|
||||
alias Ecto.Changeset
|
||||
|
||||
@login_type "m.login.password"
|
|
@ -1,11 +1,11 @@
|
|||
defmodule MatrixServerWeb.Client.RegisterController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MatrixServer.{Repo, Account}
|
||||
alias MatrixServerWeb.Request.Register
|
||||
alias MatrixServerWeb.Client.Request.Register
|
||||
alias Ecto.Changeset
|
||||
|
||||
@register_type "m.login.dummy"
|
|
@ -1,11 +1,11 @@
|
|||
defmodule MatrixServerWeb.Client.RoomController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import MatrixServerWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MatrixServer.Room
|
||||
alias MatrixServerWeb.Request.{CreateRoom}
|
||||
alias MatrixServerWeb.Client.Request.CreateRoom
|
||||
alias Ecto.Changeset
|
||||
alias Plug.Conn
|
||||
|
38
lib/matrix_server_web/client/request/create_room.ex
Normal file
38
lib/matrix_server_web/client/request/create_room.ex
Normal file
|
@ -0,0 +1,38 @@
|
|||
defmodule MatrixServerWeb.Client.Request.CreateRoom do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :visibility, :string
|
||||
field :room_alias_name, :string
|
||||
field :name, :string
|
||||
field :topic, :string
|
||||
field :invite, {:array, :string}
|
||||
field :room_version, :string
|
||||
field :preset, :string
|
||||
# TODO: unimplemented:
|
||||
# creation_content, initial_state, invite_3pid, initial_state,
|
||||
# is_direct, power_level_content_override
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [
|
||||
:visibility,
|
||||
:room_alias_name,
|
||||
:name,
|
||||
:topic,
|
||||
:invite,
|
||||
:room_version,
|
||||
:preset
|
||||
])
|
||||
|> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
31
lib/matrix_server_web/client/request/login.ex
Normal file
31
lib/matrix_server_web/client/request/login.ex
Normal file
|
@ -0,0 +1,31 @@
|
|||
defmodule MatrixServerWeb.Client.Request.Login do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :type, :string
|
||||
field :password, :string
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
|
||||
embeds_one :identifier, Identifier, primary_key: false do
|
||||
field :type, :string
|
||||
field :user, :string
|
||||
end
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [:type, :password, :device_id, :initial_device_display_name])
|
||||
|> cast_embed(:identifier, with: &identifier_changeset/2, required: true)
|
||||
|> validate_required([:type, :password])
|
||||
end
|
||||
|
||||
def identifier_changeset(identifier, params) do
|
||||
identifier
|
||||
|> cast(params, [:type, :user])
|
||||
|> validate_required([:type, :user])
|
||||
end
|
||||
end
|
33
lib/matrix_server_web/client/request/register.ex
Normal file
33
lib/matrix_server_web/client/request/register.ex
Normal file
|
@ -0,0 +1,33 @@
|
|||
defmodule MatrixServerWeb.Client.Request.Register do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
field :password, :string
|
||||
field :username, :string
|
||||
field :inhibit_login, :boolean, default: false
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [
|
||||
:device_id,
|
||||
:initial_device_display_name,
|
||||
:password,
|
||||
:username,
|
||||
:inhibit_login
|
||||
])
|
||||
|> validate_required([:password])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
|
||||
def get_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue