1 defmodule Pleroma.Web.Salmon do
2 @httpoison Application.get_env(:pleroma, :httpoison)
6 alias Pleroma.Web.OStatus.ActivityRepresenter
11 doc = XML.parse_document(salmon)
13 {:xmlObj, :string, data} = :xmerl_xpath.string('string(//me:data[1])', doc)
14 {:xmlObj, :string, sig} = :xmerl_xpath.string('string(//me:sig[1])', doc)
15 {:xmlObj, :string, alg} = :xmerl_xpath.string('string(//me:alg[1])', doc)
16 {:xmlObj, :string, encoding} = :xmerl_xpath.string('string(//me:encoding[1])', doc)
17 {:xmlObj, :string, type} = :xmerl_xpath.string('string(//me:data[1]/@type)', doc)
19 {:ok, data} = Base.url_decode64(to_string(data), ignore: :whitespace)
20 {:ok, sig} = Base.url_decode64(to_string(sig), ignore: :whitespace)
22 encoding = to_string(encoding)
23 type = to_string(type)
25 [data, type, encoding, alg, sig]
28 def fetch_magic_key(salmon) do
29 with [data, _, _, _, _] <- decode(salmon),
30 doc <- XML.parse_document(data),
31 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
32 {:ok, %{info: %{"magic_key" => magic_key}}} <- Pleroma.Web.OStatus.find_or_make_user(uri) do
37 def decode_and_validate(magickey, salmon) do
38 [data, type, encoding, alg, sig] = decode(salmon)
40 signed_text = [data, type, encoding, alg]
41 |> Enum.map(&Base.url_encode64/1)
44 key = decode_key(magickey)
46 verify = :public_key.verify(signed_text, :sha256, sig, key)
55 def decode_key("RSA." <> magickey) do
56 make_integer = fn(bin) ->
57 list = :erlang.binary_to_list(bin)
58 Enum.reduce(list, 0, fn (el, acc) -> (acc <<< 8) ||| el end)
61 [modulus, exponent] = magickey
63 |> Enum.map(&Base.url_decode64!/1)
64 |> Enum.map(make_integer)
66 {:RSAPublicKey, modulus, exponent}
69 def encode_key({:RSAPublicKey, modulus, exponent}) do
70 modulus_enc = :binary.encode_unsigned(modulus) |> Base.url_encode64
71 exponent_enc = :binary.encode_unsigned(exponent) |> Base.url_encode64
73 "RSA.#{modulus_enc}.#{exponent_enc}"
76 def generate_rsa_pem do
77 port = Port.open({:spawn, "openssl genrsa"}, [:binary])
78 {:ok, pem} = receive do
79 {^port, {:data, pem}} -> {:ok, pem}
82 if Regex.match?(~r/RSA PRIVATE KEY/, pem) do
89 def keys_from_pem(pem) do
90 [private_key_code] = :public_key.pem_decode(pem)
91 private_key = :public_key.pem_entry_decode(private_key_code)
92 {:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} = private_key
93 public_key = {:RSAPublicKey, modulus, exponent}
94 {:ok, private_key, public_key}
97 def encode(private_key, doc) do
98 type = "application/atom+xml"
99 encoding = "base64url"
102 signed_text = [doc, type, encoding, alg]
103 |> Enum.map(&Base.url_encode64/1)
106 signature = signed_text
107 |> :public_key.sign(:sha256, private_key)
114 # Don't need proper xml building, these strings are safe to leave unescaped
116 <?xml version="1.0" encoding="UTF-8"?>
117 <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
118 <me:data type="application/atom+xml">#{doc_base64}</me:data>
119 <me:encoding>#{encoding}</me:encoding>
120 <me:alg>#{alg}</me:alg>
121 <me:sig>#{signature}</me:sig>
128 def remote_users(%{data: %{"to" => to}}) do
130 |> Enum.map(fn(id) -> User.get_cached_by_ap_id(id) end)
131 |> Enum.filter(fn(user) -> user && !user.local end)
134 defp send_to_user(%{info: %{"salmon" => salmon}}, feed, poster) do
135 poster.(salmon, feed, [{"Content-Type", "application/magic-envelope+xml"}])
138 defp send_to_user(_,_,_), do: nil
140 def publish(user, activity, poster \\ &@httpoison.post/3)
141 def publish(%{info: %{"keys" => keys}} = user, activity, poster) do
142 feed = ActivityRepresenter.to_simple_form(activity, user, true)
143 |> ActivityRepresenter.wrap_with_entry
144 |> :xmerl.export_simple(:xmerl_xml)
148 {:ok, private, _} = keys_from_pem(keys)
149 {:ok, feed} = encode(private, feed)
151 remote_users(activity)
152 |> Enum.each(fn(remote_user) ->
154 Logger.debug(fn -> "sending salmon to #{remote_user.ap_id}" end)
155 send_to_user(remote_user, feed, poster)
161 def publish(%{id: id}, _, _), do: Logger.debug(fn -> "Keys missing for user #{id}" end)