Add migration and schema for accounts
Add account name availability endpoint
This commit is contained in:
parent
e6e4472b94
commit
6e039524a4
10 changed files with 107 additions and 43 deletions
51
lib/matrix_server/account.ex
Normal file
51
lib/matrix_server/account.ex
Normal file
|
@ -0,0 +1,51 @@
|
|||
defmodule MatrixServer.Account do
|
||||
use Ecto.Schema
|
||||
import Ecto.{Changeset, Query}
|
||||
alias MatrixServer.{Repo, Account}
|
||||
|
||||
@max_mxid_length 255
|
||||
@localpart_regex ~r/^([a-z0-9\._=\/])+$/
|
||||
|
||||
@primary_key {:localpart, :string, []}
|
||||
schema "accounts" do
|
||||
field :password_hash, :string, redact: true
|
||||
|
||||
timestamps(updated_at: false)
|
||||
end
|
||||
|
||||
def available?(localpart) when is_binary(localpart) do
|
||||
if Regex.match?(@localpart_regex, localpart) and
|
||||
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
|
||||
|
||||
def changeset(%Account{} = account, attrs) do
|
||||
account
|
||||
|> cast(attrs, [:localpart, :password_hash])
|
||||
|> validate_required([:localpart, :password_hash])
|
||||
|> validate_length(:password_hash, max: 60)
|
||||
|> validate_format(:localpart, @localpart_regex)
|
||||
|> validate_length(:localpart, max: localpart_length())
|
||||
|> unique_constraint(:localpart, name: :accounts_pkey)
|
||||
end
|
||||
|
||||
defp localpart_length do
|
||||
# Subtract the "@" and ":" in the MXID.
|
||||
@max_mxid_length - 2 - String.length(server_name())
|
||||
end
|
||||
|
||||
defp server_name do
|
||||
Application.get_env(:matrix_server, :server_name)
|
||||
end
|
||||
end
|
|
@ -1,35 +0,0 @@
|
|||
defmodule MatrixServerWeb.UserSocket do
|
||||
use Phoenix.Socket
|
||||
|
||||
## Channels
|
||||
# channel "room:*", MatrixServerWeb.RoomChannel
|
||||
|
||||
# Socket params are passed from the client and can
|
||||
# be used to verify and authenticate a user. After
|
||||
# verification, you can put default assigns into
|
||||
# the socket that will be set for all channels, ie
|
||||
#
|
||||
# {:ok, assign(socket, :user_id, verified_user_id)}
|
||||
#
|
||||
# To deny connection, return `:error`.
|
||||
#
|
||||
# See `Phoenix.Token` documentation for examples in
|
||||
# performing token verification on connect.
|
||||
@impl true
|
||||
def connect(_params, socket, _connect_info) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||
#
|
||||
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
|
||||
#
|
||||
# Would allow you to broadcast a "disconnect" event and terminate
|
||||
# all active sockets and channels for a given user:
|
||||
#
|
||||
# MatrixServerWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
|
||||
#
|
||||
# Returning `nil` makes this socket anonymous.
|
||||
@impl true
|
||||
def id(_socket), do: nil
|
||||
end
|
28
lib/matrix_server_web/controllers/account_controller.ex
Normal file
28
lib/matrix_server_web/controllers/account_controller.ex
Normal file
|
@ -0,0 +1,28 @@
|
|||
defmodule MatrixServerWeb.AccountController do
|
||||
use MatrixServerWeb, :controller
|
||||
alias MatrixServer.Account
|
||||
|
||||
def register(conn, _params) do
|
||||
conn
|
||||
end
|
||||
|
||||
def available(conn, params) do
|
||||
localpart = Map.get(params, "username", "")
|
||||
|
||||
{status, data} =
|
||||
case Account.available?(localpart) do
|
||||
: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
|
||||
|> put_status(status)
|
||||
|> json(data)
|
||||
end
|
||||
end
|
|
@ -10,10 +10,6 @@ defmodule MatrixServerWeb.Endpoint do
|
|||
signing_salt: "IGPHtnAo"
|
||||
]
|
||||
|
||||
socket "/socket", MatrixServerWeb.UserSocket,
|
||||
websocket: true,
|
||||
longpoll: false
|
||||
|
||||
# Serve at "/" the static files from "priv/static" directory.
|
||||
#
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
|
|
|
@ -5,7 +5,12 @@ defmodule MatrixServerWeb.Router do
|
|||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
scope "/api", MatrixServerWeb do
|
||||
scope "/_matrix", MatrixServerWeb do
|
||||
pipe_through :api
|
||||
|
||||
scope "/client/r0", as: :client do
|
||||
post "/register", AccountController, :register
|
||||
get "/register/available", AccountController, :available
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue