diff --git a/lib/diaspora_federation/discovery/h_card.rb b/lib/diaspora_federation/discovery/h_card.rb index ceb1373..d2e4e30 100644 --- a/lib/diaspora_federation/discovery/h_card.rb +++ b/lib/diaspora_federation/discovery/h_card.rb @@ -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 diff --git a/lib/diaspora_federation/discovery/web_finger.rb b/lib/diaspora_federation/discovery/web_finger.rb index ddeabd1..a500d43 100644 --- a/lib/diaspora_federation/discovery/web_finger.rb +++ b/lib/diaspora_federation/discovery/web_finger.rb @@ -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 diff --git a/spec/lib/diaspora_federation/discovery/web_finger_spec.rb b/spec/lib/diaspora_federation/discovery/web_finger_spec.rb index 340e321..a2d24a7 100644 --- a/spec/lib/diaspora_federation/discovery/web_finger_spec.rb +++ b/spec/lib/diaspora_federation/discovery/web_finger_spec.rb @@ -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 + + + #{acct} + #{person.alias_url} + + + + + + +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 = <