parse webfinger without guid and public key

This commit is contained in:
Benjamin Neff 2015-07-14 23:45:56 +02:00
parent 35f0af3c55
commit 94f9fe89c1
3 changed files with 44 additions and 29 deletions

View file

@ -70,10 +70,8 @@ module DiasporaFederation
# DER-encoded PKCS#1 key beginning with the text
# "-----BEGIN PUBLIC KEY-----" and ending with "-----END PUBLIC KEY-----".
#
# @note the public key is new in the hcard and is optional now.
#
# @return [String] public key
property :public_key, default: nil
property :public_key
# @!attribute [r] photo_large_url
# @return [String] url to the big avatar (300x300)
@ -172,14 +170,14 @@ module DiasporaFederation
photo_medium_url: photo_from_doc(doc, :photo_medium),
photo_small_url: photo_from_doc(doc, :photo_small),
searchable: (content_from_doc(doc, :searchable) == "true"),
# TODO: public key is new and can be missing
public_key: (content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil?),
# TODO: remove me! ###################
first_name: content_from_doc(doc, :given_name),
last_name: content_from_doc(doc, :family_name)
#######################################
}
# TODO: public key is new and can be missing
data[:public_key] = content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil?
new(data)
end

View file

@ -147,20 +147,23 @@ module DiasporaFederation
def self.from_xml(webfinger_xml)
data = parse_xml_and_validate(webfinger_xml)
hcard_url, seed_url, guid, profile_url, atom_url, salmon_url, public_key = parse_links(data)
links = data[:links]
# TODO: remove! public key is deprecated in webfinger
public_key = parse_link(links, REL_PUBKEY)
new(
acct_uri: data[:subject],
alias_url: data[:aliases].first,
hcard_url: hcard_url,
seed_url: seed_url,
profile_url: profile_url,
atom_url: atom_url,
salmon_url: salmon_url,
hcard_url: parse_link(links, REL_HCARD),
seed_url: parse_link(links, REL_SEED),
profile_url: parse_link(links, REL_PROFILE),
atom_url: parse_link(links, REL_ATOM),
salmon_url: parse_link(links, REL_SALMON),
# TODO: remove me! ##########
guid: guid,
public_key: Base64.strict_decode64(public_key)
guid: parse_link(links, REL_GUID),
public_key: (Base64.strict_decode64(public_key) if public_key)
)
end
@ -209,22 +212,9 @@ module DiasporaFederation
##################################
end
def self.parse_links(data)
links = data[:links]
hcard = parse_link(links, REL_HCARD)
seed = parse_link(links, REL_SEED)
guid = parse_link(links, REL_GUID)
profile = parse_link(links, REL_PROFILE)
atom = parse_link(links, REL_ATOM)
salmon = parse_link(links, REL_SALMON)
pubkey = parse_link(links, REL_PUBKEY)
raise InvalidData, "webfinger xml is incomplete" unless [hcard, seed, guid, profile, atom, salmon, pubkey].all?
[hcard[:href], seed[:href], guid[:href], profile[:href], atom[:href], salmon[:href], pubkey[:href]]
end
private_class_method :parse_links
def self.parse_link(links, rel)
links.find {|l| l[:rel] == rel }
element = links.find {|l| l[:rel] == rel }
element ? element[:href] : nil
end
private_class_method :parse_link
end

View file

@ -97,8 +97,35 @@ XML
expect(wf.public_key).to eq(person.serialized_public_key)
end
it "reads future XML without guid and public key" do
future_xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>#{acct}</Subject>
<Alias>#{person.alias_url}</Alias>
<Link rel="http://microformats.org/profile/hcard" type="text/html" href="#{person.hcard_url}"/>
<Link rel="http://joindiaspora.com/seed_location" type="text/html" href="#{person.url}"/>
<Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="#{person.profile_url}"/>
<Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="#{person.atom_url}"/>
<Link rel="salmon" href="#{person.salmon_url}"/>
</XRD>
XML
wf = Discovery::WebFinger.from_xml(future_xml)
expect(wf.acct_uri).to eq(acct)
expect(wf.alias_url).to eq(person.alias_url)
expect(wf.hcard_url).to eq(person.hcard_url)
expect(wf.seed_url).to eq(person.url)
expect(wf.profile_url).to eq(person.profile_url)
expect(wf.atom_url).to eq(person.atom_url)
expect(wf.salmon_url).to eq(person.salmon_url)
expect(wf.guid).to be_nil
expect(wf.public_key).to be_nil
end
it "fails if the document is empty" do
invalid_xml = <<XML
invalid_xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
</XRD>