architex/lib/architex_web/federation/http_client.ex

141 lines
4.5 KiB
Elixir
Raw Normal View History

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.
use Tesla
2021-09-01 12:43:55 +00:00
alias ArchitexWeb.Endpoint
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-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-09-01 12:43:55 +00:00
@adapter {Tesla.Adapter.Finch, name: ArchitexWeb.HTTPClient}
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()
def client(server_name) do
# TODO: When implementing resolving homeservers, probably create
# a homeserver struct instead of using domain names directly.
Tesla.client(
[
{Tesla.Middleware.Opts, [server_name: server_name]},
SignRequest,
{Tesla.Middleware.BaseUrl, "http://" <> server_name},
Tesla.Middleware.JSON
],
@adapter
)
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
def get_signing_keys(client) do
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} =
response} <- tesla_request(:get, client, path, GetSigningKeys),
2021-09-01 12:43:55 +00:00
serializable_response <- Architex.to_serializable_map(response),
serializable_response <- Map.drop(serializable_response, [:signatures]),
2021-09-01 12:43:55 +00:00
{:ok, encoded_body} <- Architex.encode_canonical_json(serializable_response),
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.
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
_ -> false
2021-08-13 11:45:10 +00:00
end
end)
|> then(fn
true -> {:ok, response}
false -> :error
end)
else
_ -> :error
end
end
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()
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)
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-09-09 14:00:58 +00:00
# Tesla.get(client, path)
# end
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-09-09 14:00:58 +00:00
# Tesla.get(client, path)
# end
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-09-09 14:00:58 +00:00
# Tesla.get(client, path)
# end
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
end