2021-09-01 12:43:55 +00:00
|
|
|
defmodule ArchitexWeb.Client.AccountController do
|
|
|
|
use ArchitexWeb, :controller
|
2021-06-25 22:29:33 +00:00
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
import ArchitexWeb.Error
|
2021-06-25 22:29:33 +00:00
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
alias Architex.{Account, Repo}
|
2021-06-25 22:29:33 +00:00
|
|
|
alias Plug.Conn
|
2021-06-22 21:04:37 +00:00
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Checks to see if a username is available, and valid, for the server.
|
|
|
|
|
|
|
|
Action for GET /_matrix/client/r0/register/available.
|
|
|
|
"""
|
2021-06-22 21:04:37 +00:00
|
|
|
def available(conn, params) do
|
|
|
|
localpart = Map.get(params, "username", "")
|
|
|
|
|
2021-06-25 23:14:09 +00:00
|
|
|
case Account.available?(localpart) do
|
|
|
|
:ok ->
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{available: true})
|
2021-06-22 21:04:37 +00:00
|
|
|
|
2021-06-25 23:14:09 +00:00
|
|
|
{:error, error} ->
|
|
|
|
put_error(conn, error)
|
|
|
|
end
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|
2021-06-25 22:29:33 +00:00
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Gets information about the owner of a given access token.
|
|
|
|
|
|
|
|
Action for GET /_matrix/client/r0/account/whoami.
|
|
|
|
"""
|
2021-06-25 22:29:33 +00:00
|
|
|
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do
|
2021-09-08 14:27:31 +00:00
|
|
|
data = %{user_id: Architex.get_mxid(localpart)}
|
2021-06-25 22:29:33 +00:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(data)
|
|
|
|
end
|
2021-06-27 20:24:54 +00:00
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Invalidates an existing access token, so that it can no longer be used for authorization.
|
|
|
|
|
|
|
|
Action for POST /_matrix/client/r0/logout.
|
|
|
|
"""
|
2021-06-27 20:24:54 +00:00
|
|
|
def logout(%Conn{assigns: %{device: device}} = conn, _params) do
|
|
|
|
case Repo.delete(device) do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{})
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
put_error(conn, :unknown)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Invalidates all access tokens for a user, so that they can no longer be used
|
|
|
|
for authorization.
|
|
|
|
|
|
|
|
Action for POST /_matrix/client/r0/logout/all.
|
|
|
|
"""
|
2021-06-27 20:24:54 +00:00
|
|
|
def logout_all(%Conn{assigns: %{account: account}} = conn, _params) do
|
|
|
|
Repo.delete_all(Ecto.assoc(account, :devices))
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|