Make key fetching use ostatus fetching.

This commit is contained in:
Roger Braun 2017-04-29 19:47:56 +02:00
parent 427bac0966
commit ba1ea77001
3 changed files with 15 additions and 13 deletions

View file

@ -21,24 +21,16 @@ def decode(salmon) do
[data, type, encoding, alg, sig] [data, type, encoding, alg, sig]
end end
# TODO rewrite in with-stile
# Make it fetch the key from the saved user if there is one
def fetch_magic_key(salmon) do def fetch_magic_key(salmon) do
[data, _, _, _, _] = decode(salmon) [data, _, _, _, _] = decode(salmon)
doc = XML.parse_document(data) doc = XML.parse_document(data)
{:xmlObj, :string, uri} = :xmerl_xpath.string('string(//author[1]/uri)', doc) uri = XML.string_from_xpath("/entry/author[1]/uri", doc)
uri = to_string(uri) {:ok, info} = Pleroma.Web.OStatus.gather_user_info(uri)
base = URI.parse(uri).host
# TODO: Find out if this endpoint is mandated by the standard. info.magic_key
# At least diaspora does it differently
{:ok, response} = HTTPoison.get(base <> "/.well-known/webfinger", ["Accept": "application/xrd+xml"], [params: [resource: uri]])
doc = XML.parse_document(response.body)
{:xmlObj, :string, magickey} = :xmerl_xpath.string('string(//Link[@rel="magic-public-key"]/@href)', doc)
"data:application/magic-public-key," <> magickey = to_string(magickey)
magickey
end end
def decode_and_validate(magickey, salmon) do def decode_and_validate(magickey, salmon) do

2
test/fixtures/salmon2.xml vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -49,4 +49,12 @@ test "encodes an xml payload with a private key" do
assert doc == decoded_doc assert doc == decoded_doc
end end
test "it gets a magic key" do
# TODO: Make test local
salmon = File.read!("test/fixtures/salmon2.xml")
key = Salmon.fetch_magic_key(salmon)
assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
end
end end