2021-09-01 12:43:55 +00:00
|
|
|
defmodule ArchitexWeb.Federation.HTTPClient do
|
2021-09-09 14:00:58 +00:00
|
|
|
@moduledoc """
|
|
|
|
This module provides functions to interact with other homeservers
|
|
|
|
using the Matrix federation API.
|
|
|
|
"""
|
|
|
|
# TODO: Investigate request timeouts.
|
2021-08-06 20:03:34 +00:00
|
|
|
use Tesla
|
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
alias ArchitexWeb.Endpoint
|
2021-09-09 15:26:40 +00:00
|
|
|
alias ArchitexWeb.Federation.Response.GetSigningKeys
|
2021-09-01 12:43:55 +00:00
|
|
|
alias ArchitexWeb.Federation.Middleware.SignRequest
|
|
|
|
alias ArchitexWeb.Router.Helpers, as: RouteHelpers
|
2021-08-06 20:03:34 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
@type t :: schema_response() | map_response()
|
|
|
|
|
|
|
|
@type schema_response ::
|
|
|
|
{:ok, struct()}
|
|
|
|
| {:error, :status, Tesla.Env.t()}
|
|
|
|
| {:error, :validation, Ecto.Changeset.t()}
|
|
|
|
| {:error, :request, any()}
|
|
|
|
|
|
|
|
@type map_response ::
|
|
|
|
{:ok, map()}
|
|
|
|
| {:error, :status, Tesla.Env.t()}
|
|
|
|
| {:error, :validation, Ecto.Changeset.t()}
|
|
|
|
| {:error, :request, any()}
|
2021-08-06 20:03:34 +00:00
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
@adapter {Tesla.Adapter.Finch, name: ArchitexWeb.HTTPClient}
|
2021-08-06 20:03:34 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
@doc """
|
|
|
|
Get a Tesla client for the given server name, to be used for
|
|
|
|
interacting with other homeservers.
|
|
|
|
"""
|
|
|
|
@spec client(String.t()) :: Tesla.Client.t()
|
2021-08-06 20:03:34 +00:00
|
|
|
def client(server_name) do
|
2021-09-13 10:42:56 +00:00
|
|
|
# TODO: When implementing resolving homeservers, probably create
|
|
|
|
# a homeserver struct instead of using domain names directly.
|
2021-08-14 13:20:42 +00:00
|
|
|
Tesla.client(
|
|
|
|
[
|
|
|
|
{Tesla.Middleware.Opts, [server_name: server_name]},
|
|
|
|
SignRequest,
|
|
|
|
{Tesla.Middleware.BaseUrl, "http://" <> server_name},
|
|
|
|
Tesla.Middleware.JSON
|
|
|
|
],
|
|
|
|
@adapter
|
|
|
|
)
|
2021-08-06 20:03:34 +00:00
|
|
|
end
|
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
@doc """
|
|
|
|
Get the signing keys of a homeserver.
|
|
|
|
"""
|
|
|
|
@spec get_signing_keys(Tesla.Client.t()) :: {:ok, GetSigningKeys.t()} | :error
|
2021-08-06 20:03:34 +00:00
|
|
|
def get_signing_keys(client) do
|
2021-08-13 15:36:34 +00:00
|
|
|
path = RouteHelpers.key_path(Endpoint, :get_signing_keys)
|
|
|
|
|
2021-08-13 11:45:10 +00:00
|
|
|
with {:ok,
|
|
|
|
%GetSigningKeys{server_name: server_name, verify_keys: verify_keys, signatures: sigs} =
|
2021-08-13 15:36:34 +00:00
|
|
|
response} <- tesla_request(:get, client, path, GetSigningKeys),
|
2021-09-01 12:43:55 +00:00
|
|
|
serializable_response <- Architex.to_serializable_map(response),
|
2021-08-21 09:25:36 +00:00
|
|
|
serializable_response <- Map.drop(serializable_response, [:signatures]),
|
2021-09-01 12:43:55 +00:00
|
|
|
{:ok, encoded_body} <- Architex.encode_canonical_json(serializable_response),
|
2021-08-13 15:36:34 +00:00
|
|
|
server_sigs when not is_nil(server_sigs) <- sigs[server_name] do
|
2021-08-13 11:45:10 +00:00
|
|
|
# For each verify key, check if there is a matching signature.
|
|
|
|
# If not, invalidate the whole response.
|
2021-08-13 15:36:34 +00:00
|
|
|
Enum.all?(verify_keys, fn {key_id, %{"key" => key}} ->
|
|
|
|
with true <- Map.has_key?(server_sigs, key_id),
|
2021-09-01 12:43:55 +00:00
|
|
|
{:ok, decoded_key} <- Architex.decode_base64(key),
|
|
|
|
{:ok, decoded_sig} <- Architex.decode_base64(server_sigs[key_id]) do
|
|
|
|
Architex.sign_verify(decoded_sig, encoded_body, decoded_key)
|
2021-08-13 11:45:10 +00:00
|
|
|
else
|
2021-08-13 15:36:34 +00:00
|
|
|
_ -> false
|
2021-08-13 11:45:10 +00:00
|
|
|
end
|
2021-08-13 15:36:34 +00:00
|
|
|
end)
|
|
|
|
|> then(fn
|
|
|
|
true -> {:ok, response}
|
|
|
|
false -> :error
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
_ -> :error
|
2021-08-12 22:45:07 +00:00
|
|
|
end
|
2021-08-06 20:03:34 +00:00
|
|
|
end
|
2021-08-08 17:20:10 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
@doc """
|
|
|
|
Get the profile of a user.
|
|
|
|
"""
|
|
|
|
@spec query_profile(Tesla.Client.t(), String.t(), String.t() | nil) :: map_response()
|
2021-08-14 13:20:42 +00:00
|
|
|
def query_profile(client, user_id, field \\ nil) do
|
2021-08-10 16:02:53 +00:00
|
|
|
path = RouteHelpers.query_path(Endpoint, :profile) |> Tesla.build_url(user_id: user_id)
|
|
|
|
path = if field, do: Tesla.build_url(path, field: field), else: path
|
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
tesla_request(:get, client, path)
|
2021-08-08 17:20:10 +00:00
|
|
|
end
|
2021-08-13 11:45:10 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# def get_event(client, event_id) do
|
|
|
|
# path = RouteHelpers.event_path(Endpoint, :event, event_id)
|
2021-08-21 09:25:36 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# Tesla.get(client, path)
|
|
|
|
# end
|
2021-08-21 09:25:36 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# def get_state(client, room_id, event_id) do
|
|
|
|
# path =
|
|
|
|
# RouteHelpers.event_path(Endpoint, :state, room_id) |> Tesla.build_url(event_id: event_id)
|
2021-08-21 19:39:28 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# Tesla.get(client, path)
|
|
|
|
# end
|
2021-08-21 19:39:28 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# def get_state_ids(client, room_id, event_id) do
|
|
|
|
# path =
|
|
|
|
# RouteHelpers.event_path(Endpoint, :state_ids, room_id)
|
|
|
|
# |> Tesla.build_url(event_id: event_id)
|
2021-08-22 10:19:47 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# Tesla.get(client, path)
|
|
|
|
# end
|
2021-08-22 10:19:47 +00:00
|
|
|
|
2021-09-09 14:00:58 +00:00
|
|
|
# Perform a Tesla request and validate the response with the given
|
|
|
|
# Ecto schema struct.
|
|
|
|
@spec tesla_request(atom(), Tesla.Client.t(), String.t(), module()) :: t()
|
|
|
|
defp tesla_request(method, client, path, request_schema \\ nil) do
|
|
|
|
case Tesla.request(client, url: path, method: method) do
|
|
|
|
{:ok, %Tesla.Env{status: status} = env} when status != 200 ->
|
|
|
|
{:error, :status, env}
|
|
|
|
|
|
|
|
{:ok, %Tesla.Env{body: body}} ->
|
|
|
|
if request_schema do
|
|
|
|
case apply(request_schema, :parse, [body]) do
|
|
|
|
{:ok, response} ->
|
|
|
|
{:ok, response}
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
{:error, :validation, changeset}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:ok, body}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, tesla_error} ->
|
|
|
|
{:error, :request, tesla_error}
|
2021-08-13 11:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-06 20:03:34 +00:00
|
|
|
end
|