Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

75 lignes
2.1KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Signature do
  5. @behaviour HTTPSignatures.Adapter
  6. alias Pleroma.EctoType.ActivityPub.ObjectValidators
  7. alias Pleroma.Keys
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.ActivityPub
  10. def key_id_to_actor_id(key_id) do
  11. uri =
  12. URI.parse(key_id)
  13. |> Map.put(:fragment, nil)
  14. uri =
  15. if not is_nil(uri.path) and String.ends_with?(uri.path, "/publickey") do
  16. Map.put(uri, :path, String.replace(uri.path, "/publickey", ""))
  17. else
  18. uri
  19. end
  20. maybe_ap_id = URI.to_string(uri)
  21. case ObjectValidators.ObjectID.cast(maybe_ap_id) do
  22. {:ok, ap_id} ->
  23. {:ok, ap_id}
  24. _ ->
  25. case Pleroma.Web.WebFinger.finger(maybe_ap_id) do
  26. %{"ap_id" => ap_id} -> {:ok, ap_id}
  27. _ -> {:error, maybe_ap_id}
  28. end
  29. end
  30. end
  31. def fetch_public_key(conn) do
  32. with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
  33. {:ok, actor_id} <- key_id_to_actor_id(kid),
  34. {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
  35. {:ok, public_key}
  36. else
  37. e ->
  38. {:error, e}
  39. end
  40. end
  41. def refetch_public_key(conn) do
  42. with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
  43. {:ok, actor_id} <- key_id_to_actor_id(kid),
  44. {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
  45. {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
  46. {:ok, public_key}
  47. else
  48. e ->
  49. {:error, e}
  50. end
  51. end
  52. def sign(%User{} = user, headers) do
  53. with {:ok, %{keys: keys}} <- User.ensure_keys_present(user),
  54. {:ok, private_key, _} <- Keys.keys_from_pem(keys) do
  55. HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
  56. end
  57. end
  58. def signed_date, do: signed_date(NaiveDateTime.utc_now())
  59. def signed_date(%NaiveDateTime{} = date) do
  60. Timex.format!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
  61. end
  62. end