From e3a81fe58f42935c6116ccc59c73ae41e99ecaae Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 24 Mar 2018 14:45:21 +0100 Subject: [PATCH] Fetch the same data from json webfinger as from xml webfinger. --- lib/pleroma/web/web_finger/web_finger.ex | 15 ++++++++++++--- .../httpoison_mock/winterdienst_webfinger.json | 1 + test/support/httpoison_mock.ex | 6 ++++++ test/web/web_finger/web_finger_test.exs | 12 ++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/httpoison_mock/winterdienst_webfinger.json diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index d8eb6f3e0..7391edcd7 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -118,14 +118,23 @@ defp webfinger_from_xml(doc) do {:ok, data} end - # TODO: maybe fill in other details from JRD webfinger response defp webfinger_from_json(doc) do data = Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn (link, data) -> - case link["type"] do - "application/activity+json" -> + case {link["type"], link["rel"]} do + {"application/activity+json", "self"} -> Map.put(data, "ap_id", link["href"]) + {_, "magic-public-key"} -> + "data:application/magic-public-key," <> magic_key = link["href"] + Map.put(data, "magic_key", magic_key) + {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} -> + Map.put(data, "topic", link["href"]) + {_, "salmon"} -> + Map.put(data, "salmon", link["href"]) + {_, "http://ostatus.org/schema/1.0/subscribe"} -> + Map.put(data, "subscribe_address", link["template"]) _ -> Logger.debug("Unhandled type: #{inspect(link["type"])}") + data end end) {:ok, data} diff --git a/test/fixtures/httpoison_mock/winterdienst_webfinger.json b/test/fixtures/httpoison_mock/winterdienst_webfinger.json new file mode 100644 index 000000000..e7bfba9ed --- /dev/null +++ b/test/fixtures/httpoison_mock/winterdienst_webfinger.json @@ -0,0 +1 @@ +{"subject":"acct:winterdienst@gnusocial.de","aliases":["https:\/\/gnusocial.de\/user\/249296","https:\/\/gnusocial.de\/winterdienst","https:\/\/gnusocial.de\/index.php\/user\/249296","https:\/\/gnusocial.de\/index.php\/winterdienst"],"links":[{"rel":"http:\/\/webfinger.net\/rel\/profile-page","type":"text\/html","href":"https:\/\/gnusocial.de\/winterdienst"},{"rel":"http:\/\/gmpg.org\/xfn\/11","type":"text\/html","href":"https:\/\/gnusocial.de\/winterdienst"},{"rel":"describedby","type":"application\/rdf+xml","href":"https:\/\/gnusocial.de\/winterdienst\/foaf"},{"rel":"http:\/\/apinamespace.org\/atom","type":"application\/atomsvc+xml","href":"https:\/\/gnusocial.de\/api\/statusnet\/app\/service\/winterdienst.xml"},{"rel":"http:\/\/apinamespace.org\/twitter","href":"https:\/\/gnusocial.de\/api\/"},{"rel":"http:\/\/schemas.google.com\/g\/2010#updates-from","type":"application\/atom+xml","href":"https:\/\/gnusocial.de\/api\/statuses\/user_timeline\/249296.atom"},{"rel":"magic-public-key","href":"data:application\/magic-public-key,RSA.qfYaxztz7ZELrE4v5WpJrPM99SKI3iv9Y3Tw6nfLGk-4CRljNYqV8IYX2FXjeucC_DKhPNnlF6fXyASpcSmA_qupX9WC66eVhFhZ5OuyBOeLvJ1C4x7Hi7Di8MNBxY3VdQuQR0tTaS_YAZCwASKp7H6XEid3EJpGt0EQZoNzRd8=.AQAB"},{"rel":"salmon","href":"https:\/\/gnusocial.de\/main\/salmon\/user\/249296"},{"rel":"http:\/\/salmon-protocol.org\/ns\/salmon-replies","href":"https:\/\/gnusocial.de\/main\/salmon\/user\/249296"},{"rel":"http:\/\/salmon-protocol.org\/ns\/salmon-mention","href":"https:\/\/gnusocial.de\/main\/salmon\/user\/249296"},{"rel":"http:\/\/ostatus.org\/schema\/1.0\/subscribe","template":"https:\/\/gnusocial.de\/main\/ostatussub?profile={uri}"}]} \ No newline at end of file diff --git a/test/support/httpoison_mock.ex b/test/support/httpoison_mock.ex index 5d021a3b4..c70598691 100644 --- a/test/support/httpoison_mock.ex +++ b/test/support/httpoison_mock.ex @@ -9,6 +9,12 @@ def get("http://framatube.org/.well-known/webfinger?resource=acct:framasoft@fram body: File.read!("test/fixtures/httpoison_mock/framasoft@framatube.org.json") }} end + def get("http://gnusocial.de/.well-known/webfinger?resource=acct:winterdienst@gnusocial.de", [Accept: "application/xrd+xml,application/jrd+json"], [follow_redirect: true]) do + {:ok, %Response{ + status_code: 200, + body: File.read!("test/fixtures/httpoison_mock/winterdienst_webfinger.json") + }} + end def get("https://social.heldscal.la/.well-known/webfinger", [Accept: "application/xrd+xml,application/jrd+json"], [params: [resource: "nonexistant@social.heldscal.la"], follow_redirect: true]) do {:ok, %Response{ diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs index ff60391fd..c7ad206eb 100644 --- a/test/web/web_finger/web_finger_test.exs +++ b/test/web/web_finger/web_finger_test.exs @@ -45,6 +45,18 @@ test "returns the ActivityPub actor URI for an ActivityPub user" do {:ok, _data} = WebFinger.finger(user) end + test "returns the correctly for json ostatus users" do + user = "winterdienst@gnusocial.de" + + {:ok, data} = WebFinger.finger(user) + + assert data["magic_key"] == "RSA.qfYaxztz7ZELrE4v5WpJrPM99SKI3iv9Y3Tw6nfLGk-4CRljNYqV8IYX2FXjeucC_DKhPNnlF6fXyASpcSmA_qupX9WC66eVhFhZ5OuyBOeLvJ1C4x7Hi7Di8MNBxY3VdQuQR0tTaS_YAZCwASKp7H6XEid3EJpGt0EQZoNzRd8=.AQAB" + assert data["topic"] == "https://gnusocial.de/api/statuses/user_timeline/249296.atom" + assert data["subject"] == "acct:winterdienst@gnusocial.de" + assert data["salmon"] == "https://gnusocial.de/main/salmon/user/249296" + assert data["subscribe_address"] == "https://gnusocial.de/main/ostatussub?profile={uri}" + end + test "it works for friendica" do user = "lain@squeet.me"