Commit bb1d08a4 authored by lain's avatar lain
Browse files

Return keys in webfinger.

parent 09f7ed42
...@@ -16,7 +16,7 @@ defmodule Pleroma.User do ...@@ -16,7 +16,7 @@ defmodule Pleroma.User do
field :ap_id, :string field :ap_id, :string
field :avatar, :map field :avatar, :map
field :local, :boolean, default: true field :local, :boolean, default: true
field :info, :map field :info, :map, default: %{}
timestamps() timestamps()
end end
......
defmodule Pleroma.Web.WebFinger do defmodule Pleroma.Web.WebFinger do
alias Pleroma.XmlBuilder alias Pleroma.XmlBuilder
alias Pleroma.User alias Pleroma.{Repo, User}
alias Pleroma.Web.OStatus alias Pleroma.Web.{XML, Salmon, OStatus}
alias Pleroma.Web.XML
require Logger require Logger
def host_meta() do def host_meta() do
...@@ -28,18 +27,33 @@ def webfinger(resource) do ...@@ -28,18 +27,33 @@ def webfinger(resource) do
end end
def represent_user(user) do def represent_user(user) do
{:ok, user} = ensure_keys_present(user)
{:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
magic_key = Salmon.encode_key(public)
{ {
:XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"}, :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
[ [
{:Subject, "acct:#{user.nickname}@#{Pleroma.Web.host}"}, {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.host}"},
{:Alias, user.ap_id}, {:Alias, user.ap_id},
{:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}}, {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
{:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}} {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
{:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}
] ]
} }
|> XmlBuilder.to_doc |> XmlBuilder.to_doc
end end
def ensure_keys_present(user) do
info = user.info || %{}
if info["keys"] do
{:ok, user}
else
{:ok, pem} = Salmon.generate_rsa_pem
info = Map.put(info, "keys", pem)
Repo.update(Ecto.Changeset.change(user, info: info))
end
end
# FIXME: Make this call the host-meta to find the actual address. # FIXME: Make this call the host-meta to find the actual address.
defp webfinger_address(domain) do defp webfinger_address(domain) do
"//#{domain}/.well-known/webfinger" "//#{domain}/.well-known/webfinger"
......
defmodule Pleroma.Web.WebFingerTest do defmodule Pleroma.Web.WebFingerTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.WebFinger alias Pleroma.Web.WebFinger
import Pleroma.Factory
describe "host meta" do describe "host meta" do
test "returns a link to the xml lrdd" do test "returns a link to the xml lrdd" do
...@@ -26,4 +27,19 @@ test "returns the info for a user" do ...@@ -26,4 +27,19 @@ test "returns the info for a user" do
assert data.salmon == "https://social.heldscal.la/main/salmon/user/29191" assert data.salmon == "https://social.heldscal.la/main/salmon/user/29191"
end end
end end
describe "ensure_keys_present" do
test "it creates keys for a user and stores them in info" do
user = insert(:user)
refute is_binary(user.info["keys"])
{:ok, user} = WebFinger.ensure_keys_present(user)
assert is_binary(user.info["keys"])
end
test "it doesn't create keys if there already are some" do
user = insert(:user, %{info: %{"keys" => "xxx"}})
{:ok, user} = WebFinger.ensure_keys_present(user)
assert user.info["keys"] == "xxx"
end
end
end end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment