From 87509e7af2aa039068cb2dedbe29a05725e8c6b9 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 9 Jul 2015 02:16:11 +0200 Subject: [PATCH] HCard and WebFinger frozen after parsing --- lib/diaspora_federation/web_finger/h_card.rb | 42 +++++++++++-------- .../web_finger/web_finger.rb | 25 +++++------ .../web_finger/h_card_spec.rb | 5 +++ .../web_finger/web_finger_spec.rb | 5 +++ 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/lib/diaspora_federation/web_finger/h_card.rb b/lib/diaspora_federation/web_finger/h_card.rb index 8655c75..76025e9 100644 --- a/lib/diaspora_federation/web_finger/h_card.rb +++ b/lib/diaspora_federation/web_finger/h_card.rb @@ -69,8 +69,11 @@ module DiasporaFederation # for them. This key is used for signing messages. The format is a # 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 + property :public_key, default: nil # @!attribute [r] photo_large_url # @return [String] url to the big avatar (300x300) @@ -160,24 +163,24 @@ module DiasporaFederation def self.from_html(html_string) doc = parse_html_and_validate(html_string) - hc = allocate - hc.instance_eval { - @guid = content_from_doc(doc, :uid) - @nickname = content_from_doc(doc, :nickname) - @full_name = content_from_doc(doc, :fn) - @url = element_from_doc(doc, :url)["href"] - @photo_large_url = photo_from_doc(doc, :photo) - @photo_medium_url = photo_from_doc(doc, :photo_medium) - @photo_small_url = photo_from_doc(doc, :photo_small) - @public_key = content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil? - @searchable = content_from_doc(doc, :searchable) == "true" + data = { + guid: content_from_doc(doc, :uid), + nickname: content_from_doc(doc, :nickname), + full_name: content_from_doc(doc, :fn), + url: element_from_doc(doc, :url)["href"], + photo_large_url: photo_from_doc(doc, :photo), + 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: change me! ################### - @first_name = content_from_doc(doc, :given_name) - @last_name = content_from_doc(doc, :family_name) + first_name: content_from_doc(doc, :given_name), + last_name: content_from_doc(doc, :family_name) ####################################### } - hc + # 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 private @@ -271,17 +274,20 @@ module DiasporaFederation end private_class_method :parse_html_and_validate - def element_from_doc(doc, selector) + def self.element_from_doc(doc, selector) doc.at_css(SELECTORS[selector]) end + private_class_method :element_from_doc - def content_from_doc(doc, content_selector) + def self.content_from_doc(doc, content_selector) element_from_doc(doc, content_selector).content end + private_class_method :content_from_doc - def photo_from_doc(doc, photo_selector) + def self.photo_from_doc(doc, photo_selector) element_from_doc(doc, photo_selector)["src"] end + private_class_method :photo_from_doc end end end diff --git a/lib/diaspora_federation/web_finger/web_finger.rb b/lib/diaspora_federation/web_finger/web_finger.rb index 7264bba..a0ddc7a 100644 --- a/lib/diaspora_federation/web_finger/web_finger.rb +++ b/lib/diaspora_federation/web_finger/web_finger.rb @@ -149,22 +149,19 @@ module DiasporaFederation hcard_url, seed_url, guid, profile_url, atom_url, salmon_url, public_key = parse_links(data) - wf = allocate - wf.instance_eval { - @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 + 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, # TODO: remove me! ########## - @guid = guid - @public_key = Base64.strict_decode64(public_key) - ############################## - } - wf + guid: guid, + public_key: Base64.strict_decode64(public_key) + ) end private diff --git a/spec/lib/diaspora_federation/web_finger/h_card_spec.rb b/spec/lib/diaspora_federation/web_finger/h_card_spec.rb index a091107..c7e124a 100644 --- a/spec/lib/diaspora_federation/web_finger/h_card_spec.rb +++ b/spec/lib/diaspora_federation/web_finger/h_card_spec.rb @@ -136,6 +136,11 @@ HTML expect(hcard.last_name).to eq(person.last_name) end + it "is frozen after parsing" do + hcard = WebFinger::HCard.from_html(html) + expect(hcard).to be_frozen + end + it "searchable is false, if it is empty in html" do changed_html = html.sub( "class=\"searchable\">#{person.searchable}<", diff --git a/spec/lib/diaspora_federation/web_finger/web_finger_spec.rb b/spec/lib/diaspora_federation/web_finger/web_finger_spec.rb index a73750b..e5313b7 100644 --- a/spec/lib/diaspora_federation/web_finger/web_finger_spec.rb +++ b/spec/lib/diaspora_federation/web_finger/web_finger_spec.rb @@ -61,6 +61,11 @@ XML expect(wf.public_key).to eq(person.serialized_public_key) end + it "is frozen after parsing" do + wf = WebFinger::WebFinger.from_xml(xml) + expect(wf).to be_frozen + end + it "reads old-style XML" do historic_xml = <<-XML