2021-08-06 21:14:27 +00:00
|
|
|
defmodule MatrixServerWeb.Federation.KeyController do
|
2021-08-06 13:52:03 +00:00
|
|
|
use MatrixServerWeb, :controller
|
|
|
|
|
2021-08-14 13:20:42 +00:00
|
|
|
import MatrixServerWeb.Error
|
2021-08-06 13:52:03 +00:00
|
|
|
|
2021-08-12 22:45:07 +00:00
|
|
|
alias MatrixServer.KeyServer
|
2021-08-06 13:52:03 +00:00
|
|
|
|
|
|
|
def get_signing_keys(conn, _params) do
|
|
|
|
keys =
|
2021-08-12 22:45:07 +00:00
|
|
|
KeyServer.get_own_signing_keys()
|
2021-08-06 13:52:03 +00:00
|
|
|
|> Enum.into(%{}, fn {key_id, key} ->
|
|
|
|
{key_id, %{"key" => key}}
|
|
|
|
end)
|
|
|
|
|
2021-08-16 17:30:47 +00:00
|
|
|
# TODO: Consider using TimeX.
|
|
|
|
# Valid for one month.
|
|
|
|
valid_until = DateTime.utc_now() |> DateTime.add(60 * 60 * 24 * 30, :second)
|
|
|
|
|
2021-08-06 13:52:03 +00:00
|
|
|
data = %{
|
|
|
|
server_name: MatrixServer.server_name(),
|
|
|
|
verify_keys: keys,
|
|
|
|
old_verify_keys: %{},
|
2021-08-16 17:30:47 +00:00
|
|
|
valid_until_ts: valid_until
|
2021-08-06 13:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 22:45:07 +00:00
|
|
|
case KeyServer.sign_object(data) do
|
|
|
|
{:ok, sig, key_id} ->
|
|
|
|
signed_data = MatrixServer.add_signature(data, key_id, sig)
|
|
|
|
|
2021-08-06 13:52:03 +00:00
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(signed_data)
|
|
|
|
|
2021-08-12 22:45:07 +00:00
|
|
|
:error ->
|
2021-08-06 13:52:03 +00:00
|
|
|
put_error(conn, :unknown, "Error signing object.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|