Add JSON support to WebFinger

This commit is contained in:
Benjamin Neff 2016-06-22 22:53:37 +02:00
parent 51f73b86fc
commit ea00874c93
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
4 changed files with 189 additions and 51 deletions

View file

@ -107,15 +107,11 @@ module DiasporaFederation
# Creates the XML string from the current WebFinger instance
# @return [String] XML string
def to_xml
doc = XrdDocument.new
to_xrd.to_xml
end
doc.subject = acct_uri
doc.aliases.concat(additional_data[:aliases]) if additional_data[:aliases]
doc.properties.merge!(additional_data[:properties]) if additional_data[:properties]
add_links_to(doc)
doc.to_xml
def to_json
to_xrd.to_json
end
# Creates a WebFinger instance from the given XML string
@ -159,6 +155,16 @@ module DiasporaFederation
end
end
def to_xrd
XrdDocument.new.tap do |xrd|
xrd.subject = acct_uri
xrd.aliases.concat(additional_data[:aliases]) if additional_data[:aliases]
xrd.properties.merge!(additional_data[:properties]) if additional_data[:properties]
add_links_to(xrd)
end
end
def add_links_to(doc)
doc.links << {rel: REL_HCARD, type: "text/html", href: hcard_url}
doc.links << {rel: REL_SEED, type: "text/html", href: seed_url}

View file

@ -69,9 +69,9 @@ module DiasporaFederation
def to_xml
Nokogiri::XML::Builder.new(encoding: "UTF-8") {|xml|
xml.XRD("xmlns" => XMLNS) {
xml.Expires(@expires.strftime(DATETIME_FORMAT)) if @expires.instance_of?(DateTime)
xml.Expires(expires.strftime(DATETIME_FORMAT)) if expires.instance_of?(DateTime)
xml.Subject(@subject) if !@subject.nil? && !@subject.empty?
xml.Subject(subject) if !subject.nil? && !subject.empty?
add_aliases_to(xml)
add_properties_to(xml)
@ -80,6 +80,16 @@ module DiasporaFederation
}.to_xml
end
def to_json
{
subject: subject,
expires: expires,
aliases: (aliases if aliases.any?),
links: (links if links.any?),
properties: (properties if properties.any?)
}.reject {|_, v| v.nil? }
end
# Parse the XRD document from the given string and create a hash containing
# the extracted data.
#
@ -107,23 +117,26 @@ module DiasporaFederation
private
attr_reader :expires
attr_reader :subject
NS = {xrd: XMLNS}.freeze
def add_aliases_to(xml)
@aliases.each do |a|
aliases.each do |a|
next if !a.instance_of?(String) || a.empty?
xml.Alias(a.to_s)
end
end
def add_properties_to(xml)
@properties.each do |type, val|
properties.each do |type, val|
xml.Property(val.to_s, type: type)
end
end
def add_links_to(xml)
@links.each do |l|
links.each do |l|
attrs = {}
LINK_ATTRS.each do |attr|
attrs[attr.to_s] = l[attr] if l.key?(attr)

View file

@ -44,22 +44,36 @@ XML
it_behaves_like "an Entity subclass"
context "generation" do
it "creates a nice XML document" do
wf = Discovery::WebFinger.new(data, aliases: [person.alias_url])
expect(wf.to_xml).to eq(xml)
end
let(:minimal_data) { {acct_uri: acct, hcard_url: person.hcard_url, seed_url: person.url} }
let(:additional_data) {
{
aliases: [person.alias_url, person.profile_url],
properties: {"http://webfinger.example/ns/name" => "Bob Smith"},
links: [
{rel: "http://portablecontacts.net/spec/1.0", href: "https://pod.example.tld/poco/trouble"},
{
rel: "http://webfinger.net/rel/avatar",
type: "image/jpeg",
href: "http://localhost:3000/assets/user/default.png"
},
{rel: "http://openid.net/specs/connect/1.0/issuer", href: "https://pod.example.tld/"}
]
}
}
it "creates minimal XML document" do
wf = Discovery::WebFinger.new(
acct_uri: acct,
hcard_url: person.hcard_url,
seed_url: person.url
)
expect(wf.to_xml).to eq(minimal_xml)
end
context "xml" do
it "creates a nice XML document" do
wf = Discovery::WebFinger.new(data, aliases: [person.alias_url])
expect(wf.to_xml).to eq(xml)
end
it "creates XML document with additional data" do
xml_with_additional_data = <<-XML
it "creates minimal XML document" do
wf = Discovery::WebFinger.new(minimal_data)
expect(wf.to_xml).to eq(minimal_xml)
end
it "creates XML document with additional data" do
xml_with_additional_data = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>#{acct}</Subject>
@ -70,27 +84,123 @@ XML
<Link rel="http://joindiaspora.com/seed_location" type="text/html" href="#{person.url}"/>
<Link rel="http://portablecontacts.net/spec/1.0" href="https://pod.example.tld/poco/trouble"/>
<Link rel="http://webfinger.net/rel/avatar" type="image/jpeg" href="http://localhost:3000/assets/user/default.png"/>
<Link rel="http://openid.net/specs/connect/1.0/issuer" href="https://pod.example.tld/"/>
</XRD>
XML
wf = Discovery::WebFinger.new(
{
acct_uri: acct,
hcard_url: person.hcard_url,
seed_url: person.url
},
aliases: [person.alias_url, person.profile_url],
properties: {"http://webfinger.example/ns/name" => "Bob Smith"},
links: [
{rel: "http://portablecontacts.net/spec/1.0", href: "https://pod.example.tld/poco/trouble"},
{
rel: "http://webfinger.net/rel/avatar",
type: "image/jpeg",
href: "http://localhost:3000/assets/user/default.png"
}
]
)
expect(wf.to_xml).to eq(xml_with_additional_data)
wf = Discovery::WebFinger.new(minimal_data, additional_data)
expect(wf.to_xml).to eq(xml_with_additional_data)
end
end
context "json" do
it "creates a nice JSON document" do
json = <<-JSON
{
"subject": "#{acct}",
"aliases": [
"#{person.alias_url}"
],
"links": [
{
"rel": "http://microformats.org/profile/hcard",
"type": "text/html",
"href": "#{person.hcard_url}"
},
{
"rel": "http://joindiaspora.com/seed_location",
"type": "text/html",
"href": "#{person.url}"
},
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": "#{person.profile_url}"
},
{
"rel": "http://schemas.google.com/g/2010#updates-from",
"type": "application/atom+xml",
"href": "#{person.atom_url}"
},
{
"rel": "salmon",
"href": "#{person.salmon_url}"
},
{
"rel": "http://ostatus.org/schema/1.0/subscribe",
"template": "http://somehost:3000/people?q={uri}"
}
]
}
JSON
wf = Discovery::WebFinger.new(data, aliases: [person.alias_url])
expect(JSON.pretty_generate(wf.to_json)).to eq(json.strip)
end
it "creates minimal JSON document" do
minimal_json = <<-JSON
{
"subject": "#{acct}",
"links": [
{
"rel": "http://microformats.org/profile/hcard",
"type": "text/html",
"href": "#{person.hcard_url}"
},
{
"rel": "http://joindiaspora.com/seed_location",
"type": "text/html",
"href": "#{person.url}"
}
]
}
JSON
wf = Discovery::WebFinger.new(minimal_data)
expect(JSON.pretty_generate(wf.to_json)).to eq(minimal_json.strip)
end
it "creates JSON document with additional data" do
json_with_additional_data = <<-JSON
{
"subject": "#{acct}",
"aliases": [
"#{person.alias_url}",
"#{person.profile_url}"
],
"links": [
{
"rel": "http://microformats.org/profile/hcard",
"type": "text/html",
"href": "#{person.hcard_url}"
},
{
"rel": "http://joindiaspora.com/seed_location",
"type": "text/html",
"href": "#{person.url}"
},
{
"rel": "http://portablecontacts.net/spec/1.0",
"href": "https://pod.example.tld/poco/trouble"
},
{
"rel": "http://webfinger.net/rel/avatar",
"type": "image/jpeg",
"href": "http://localhost:3000/assets/user/default.png"
},
{
"rel": "http://openid.net/specs/connect/1.0/issuer",
"href": "https://pod.example.tld/"
}
],
"properties": {
"http://webfinger.example/ns/name": "Bob Smith"
}
}
JSON
wf = Discovery::WebFinger.new(minimal_data, additional_data)
expect(JSON.pretty_generate(wf.to_json)).to eq(json_with_additional_data.strip)
end
end
end

View file

@ -45,9 +45,8 @@ XML
}
}
context "generation" do
it "creates the xml document" do
doc = Discovery::XrdDocument.new
let(:doc) {
Discovery::XrdDocument.new.tap do |doc|
doc.expires = data[:expires]
doc.subject = data[:subject]
@ -62,15 +61,25 @@ XML
data[:links].each do |h|
doc.links << h
end
end
}
describe "#to_xml" do
it "creates the xml document" do
expect(doc.to_xml).to eq(xml)
end
end
context "parsing" do
describe "#to_json" do
it "provides the hash for json" do
expect(doc.to_json).to eq(data)
end
end
describe ".xml_data" do
it "reads the xml document" do
doc = Discovery::XrdDocument.xml_data(xml)
expect(doc).to eq(data)
hash = Discovery::XrdDocument.xml_data(xml)
expect(hash).to eq(data)
end
it "raises InvalidDocument if the xml is empty" do