From ca40dda04c114c32ca9ecfe5f998a083448a189c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 28 Apr 2017 17:41:12 +0200 Subject: [PATCH] Add some basic webfingering. --- lib/pleroma/web/web_finger/web_finger.ex | 37 ++++++++++++++++++++++++ test/fixtures/webfinger.xml | 20 +++++++++++++ test/web/web_finger/web_finger_test.exs | 20 ++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/webfinger.xml diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 18459e8f0..08ff6d278 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -2,6 +2,8 @@ defmodule Pleroma.Web.WebFinger do alias Pleroma.XmlBuilder alias Pleroma.User alias Pleroma.Web.OStatus + alias Pleroma.Web.XML + require Logger def host_meta() do base_url = Pleroma.Web.base_url @@ -37,4 +39,39 @@ def represent_user(user) do } |> XmlBuilder.to_doc end + + # FIXME: Make this call the host-meta to find the actual address. + defp webfinger_address(domain) do + "https://#{domain}/.well-known/webfinger" + end + + defp webfinger_from_xml(doc) do + magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc) + "data:application/magic-public-key," <> magic_key = magic_key + topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc) + subject = XML.string_from_xpath("//Subject", doc) + salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc) + data = %{ + magic_key: magic_key, + topic: topic, + subject: subject, + salmon: salmon + } + {:ok, data} + end + + def finger(account, getter \\ &HTTPoison.get/3) do + [name, domain] = String.split(account, "@") + address = webfinger_address(domain) + with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- getter.(address, ["Accept": "application/xrd+xml"], [params: [resource: account]]), + doc <- XML.parse_document(body), + {:ok, data} <- webfinger_from_xml(doc) do + {:ok, data} + else + e -> + Logger.debug("Couldn't finger #{account}.") + Logger.debug(e) + {:error, e} + end + end end diff --git a/test/fixtures/webfinger.xml b/test/fixtures/webfinger.xml new file mode 100644 index 000000000..4cde42e3f --- /dev/null +++ b/test/fixtures/webfinger.xml @@ -0,0 +1,20 @@ + + + acct:shp@social.heldscal.la + https://social.heldscal.la/user/29191 + https://social.heldscal.la/shp + https://social.heldscal.la/index.php/user/29191 + https://social.heldscal.la/index.php/shp + + + + + + + + + + + + + diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs index 8a3007ff9..e5347a2b0 100644 --- a/test/web/web_finger/web_finger_test.exs +++ b/test/web/web_finger/web_finger_test.exs @@ -1,11 +1,29 @@ defmodule Pleroma.Web.WebFingerTest do use Pleroma.DataCase + alias Pleroma.Web.WebFinger describe "host meta" do test "returns a link to the xml lrdd" do - host_info = Pleroma.Web.WebFinger.host_meta + host_info = WebFinger.host_meta() assert String.contains?(host_info, Pleroma.Web.base_url) end end + + describe "fingering" do + test "returns the info for a user" do + user = "shp@social.heldscal.la" + + getter = fn(_url, _headers, [params: [resource: ^user]]) -> + {:ok, %{status_code: 200, body: File.read!("test/fixtures/webfinger.xml")}} + end + + {:ok, data} = WebFinger.finger(user, getter) + + assert data.magic_key == "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB" + assert data.topic == "https://social.heldscal.la/api/statuses/user_timeline/29191.atom" + assert data.subject == "acct:shp@social.heldscal.la" + assert data.salmon == "https://social.heldscal.la/main/salmon/user/29191" + end + end end