2021-06-22 21:04:37 +00:00
|
|
|
defmodule MatrixServer.Account do
|
|
|
|
use Ecto.Schema
|
2021-06-25 15:43:12 +00:00
|
|
|
|
2021-06-22 21:04:37 +00:00
|
|
|
import Ecto.{Changeset, Query}
|
2021-06-25 15:43:12 +00:00
|
|
|
|
2021-08-23 10:59:12 +00:00
|
|
|
alias MatrixServer.{Repo, Account, Device, Room, JoinedRoom}
|
2021-08-14 13:20:42 +00:00
|
|
|
alias MatrixServerWeb.Client.Request.{Register, Login}
|
2021-08-26 09:01:19 +00:00
|
|
|
alias Ecto.{Multi, Changeset}
|
2021-06-22 21:04:37 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@type t :: %__MODULE__{
|
2021-08-19 14:31:03 +00:00
|
|
|
password_hash: String.t()
|
|
|
|
}
|
2021-08-18 21:22:04 +00:00
|
|
|
|
2021-06-22 21:04:37 +00:00
|
|
|
@max_mxid_length 255
|
|
|
|
|
|
|
|
@primary_key {:localpart, :string, []}
|
|
|
|
schema "accounts" do
|
|
|
|
field :password_hash, :string, redact: true
|
2021-06-25 15:43:12 +00:00
|
|
|
has_many :devices, Device, foreign_key: :localpart
|
2021-08-23 10:59:12 +00:00
|
|
|
|
|
|
|
many_to_many :joined_rooms, Room,
|
|
|
|
join_through: JoinedRoom,
|
|
|
|
join_keys: [localpart: :localpart, room_id: :id]
|
|
|
|
|
2021-06-22 21:04:37 +00:00
|
|
|
timestamps(updated_at: false)
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@doc """
|
|
|
|
Reports whether the given user localpart is available on this server.
|
|
|
|
"""
|
|
|
|
@spec available?(String.t()) :: :ok | {:error, :user_in_use | :invalid_username}
|
2021-06-22 21:04:37 +00:00
|
|
|
def available?(localpart) when is_binary(localpart) do
|
2021-07-10 21:16:00 +00:00
|
|
|
if Regex.match?(MatrixServer.localpart_regex(), localpart) and
|
2021-06-22 21:04:37 +00:00
|
|
|
String.length(localpart) <= localpart_length() do
|
|
|
|
if Repo.one!(
|
|
|
|
Account
|
|
|
|
|> where([a], a.localpart == ^localpart)
|
|
|
|
|> select([a], count(a))
|
|
|
|
) == 0 do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
{:error, :user_in_use}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, :invalid_username}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@doc """
|
|
|
|
Return an multi to register a new user.
|
|
|
|
"""
|
|
|
|
@spec register(Register.t()) :: Multi.t()
|
2021-07-17 15:38:20 +00:00
|
|
|
def register(%Register{} = input) do
|
2021-07-13 17:35:02 +00:00
|
|
|
account_params = %{
|
2021-07-17 15:38:20 +00:00
|
|
|
localpart: input.username || MatrixServer.random_string(10, ?a..?z),
|
|
|
|
password_hash: Bcrypt.hash_pwd_salt(input.password)
|
2021-07-13 17:35:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 15:43:12 +00:00
|
|
|
Multi.new()
|
2021-07-13 17:35:02 +00:00
|
|
|
|> Multi.insert(:account, changeset(%Account{}, account_params))
|
2021-06-25 15:43:12 +00:00
|
|
|
|> Multi.insert(:device, fn %{account: account} ->
|
2021-07-13 17:35:02 +00:00
|
|
|
device_params = %{
|
2021-07-17 15:38:20 +00:00
|
|
|
display_name: input.initial_device_display_name,
|
|
|
|
device_id: input.device_id || Device.generate_device_id(account.localpart)
|
2021-07-13 17:35:02 +00:00
|
|
|
}
|
2021-07-13 15:08:07 +00:00
|
|
|
|
2021-06-25 15:43:12 +00:00
|
|
|
Ecto.build_assoc(account, :devices)
|
2021-07-13 17:35:02 +00:00
|
|
|
|> Device.changeset(device_params)
|
2021-06-25 15:43:12 +00:00
|
|
|
end)
|
2021-06-27 15:28:28 +00:00
|
|
|
|> Multi.run(:device_with_access_token, &Device.insert_new_access_token/2)
|
2021-06-25 15:43:12 +00:00
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@doc """
|
|
|
|
Return a function to log a user in.
|
|
|
|
"""
|
|
|
|
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, Device.t()})
|
2021-07-17 15:38:20 +00:00
|
|
|
def login(%Login{} = input) do
|
|
|
|
localpart = try_get_localpart(input.identifier.user)
|
2021-07-13 21:16:56 +00:00
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
fn repo ->
|
|
|
|
case repo.one(from a in Account, where: a.localpart == ^localpart) do
|
|
|
|
%Account{password_hash: hash} = account ->
|
2021-07-17 15:38:20 +00:00
|
|
|
if Bcrypt.verify_pass(input.password, hash) do
|
|
|
|
case Device.login(input, account) do
|
2021-07-13 21:16:56 +00:00
|
|
|
{:ok, device} ->
|
|
|
|
device
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-13 21:16:56 +00:00
|
|
|
{:error, _cs} ->
|
|
|
|
repo.rollback(:forbidden)
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
repo.rollback(:forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
repo.rollback(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@doc """
|
|
|
|
Get a device and its associated account using the device's access token.
|
|
|
|
"""
|
|
|
|
@spec by_access_token(String.t()) :: {Account.t(), Device.t()} | nil
|
2021-06-27 20:24:54 +00:00
|
|
|
def by_access_token(access_token) do
|
|
|
|
Device
|
|
|
|
|> where([d], d.access_token == ^access_token)
|
|
|
|
|> join(:inner, [d], a in assoc(d, :account))
|
|
|
|
|> select([d, a], {a, d})
|
2021-06-25 22:29:33 +00:00
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@spec changeset(map(), map()) :: Changeset.t()
|
2021-06-25 15:43:12 +00:00
|
|
|
def changeset(account, params \\ %{}) do
|
2021-07-17 15:38:20 +00:00
|
|
|
# TODO: fix password_hash in params
|
2021-06-22 21:04:37 +00:00
|
|
|
account
|
2021-06-25 15:43:12 +00:00
|
|
|
|> cast(params, [:localpart, :password_hash])
|
2021-06-22 21:04:37 +00:00
|
|
|
|> validate_required([:localpart, :password_hash])
|
|
|
|
|> validate_length(:password_hash, max: 60)
|
2021-07-10 21:16:00 +00:00
|
|
|
|> validate_format(:localpart, MatrixServer.localpart_regex())
|
2021-06-22 21:04:37 +00:00
|
|
|
|> validate_length(:localpart, max: localpart_length())
|
|
|
|
|> unique_constraint(:localpart, name: :accounts_pkey)
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@spec localpart_length :: integer()
|
2021-06-22 21:04:37 +00:00
|
|
|
defp localpart_length do
|
|
|
|
# Subtract the "@" and ":" in the MXID.
|
2021-07-10 21:16:00 +00:00
|
|
|
@max_mxid_length - 2 - String.length(MatrixServer.server_name())
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|
2021-07-13 21:16:56 +00:00
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@spec try_get_localpart(String.t()) :: String.t()
|
2021-07-13 21:16:56 +00:00
|
|
|
defp try_get_localpart("@" <> rest = user_id) do
|
2021-08-06 20:03:34 +00:00
|
|
|
case String.split(rest, ":", parts: 2) do
|
2021-07-13 21:16:56 +00:00
|
|
|
[localpart, _] -> localpart
|
|
|
|
_ -> user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp try_get_localpart(localpart), do: localpart
|
2021-08-26 09:01:19 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get the matrix user ID of an account.
|
|
|
|
"""
|
|
|
|
@spec get_mxid(Account.t()) :: String.t()
|
|
|
|
def get_mxid(%Account{localpart: localpart}) do
|
|
|
|
"@" <> localpart <> ":" <> MatrixServer.server_name()
|
|
|
|
end
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|