HCard and WebFinger frozen after parsing

This commit is contained in:
Benjamin Neff 2015-07-09 02:16:11 +02:00
parent 88b67d9a83
commit 87509e7af2
4 changed files with 45 additions and 32 deletions

View file

@ -69,8 +69,11 @@ module DiasporaFederation
# for them. This key is used for signing messages. The format is a # for them. This key is used for signing messages. The format is a
# DER-encoded PKCS#1 key beginning with the text # DER-encoded PKCS#1 key beginning with the text
# "-----BEGIN PUBLIC KEY-----" and ending with "-----END PUBLIC KEY-----". # "-----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 # @return [String] public key
property :public_key property :public_key, default: nil
# @!attribute [r] photo_large_url # @!attribute [r] photo_large_url
# @return [String] url to the big avatar (300x300) # @return [String] url to the big avatar (300x300)
@ -160,24 +163,24 @@ module DiasporaFederation
def self.from_html(html_string) def self.from_html(html_string)
doc = parse_html_and_validate(html_string) doc = parse_html_and_validate(html_string)
hc = allocate data = {
hc.instance_eval { guid: content_from_doc(doc, :uid),
@guid = content_from_doc(doc, :uid) nickname: content_from_doc(doc, :nickname),
@nickname = content_from_doc(doc, :nickname) full_name: content_from_doc(doc, :fn),
@full_name = content_from_doc(doc, :fn) url: element_from_doc(doc, :url)["href"],
@url = element_from_doc(doc, :url)["href"] photo_large_url: photo_from_doc(doc, :photo),
@photo_large_url = photo_from_doc(doc, :photo) photo_medium_url: photo_from_doc(doc, :photo_medium),
@photo_medium_url = photo_from_doc(doc, :photo_medium) photo_small_url: photo_from_doc(doc, :photo_small),
@photo_small_url = photo_from_doc(doc, :photo_small) searchable: (content_from_doc(doc, :searchable) == "true"),
@public_key = content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil?
@searchable = content_from_doc(doc, :searchable) == "true"
# TODO: change me! ################### # TODO: change me! ###################
@first_name = content_from_doc(doc, :given_name) first_name: content_from_doc(doc, :given_name),
@last_name = content_from_doc(doc, :family_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 end
private private
@ -271,17 +274,20 @@ module DiasporaFederation
end end
private_class_method :parse_html_and_validate 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]) doc.at_css(SELECTORS[selector])
end 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 element_from_doc(doc, content_selector).content
end 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"] element_from_doc(doc, photo_selector)["src"]
end end
private_class_method :photo_from_doc
end end
end end
end end

View file

@ -149,22 +149,19 @@ module DiasporaFederation
hcard_url, seed_url, guid, profile_url, atom_url, salmon_url, public_key = parse_links(data) hcard_url, seed_url, guid, profile_url, atom_url, salmon_url, public_key = parse_links(data)
wf = allocate new(
wf.instance_eval { acct_uri: data[:subject],
@acct_uri = data[:subject] alias_url: data[:aliases].first,
@alias_url = data[:aliases].first hcard_url: hcard_url,
@hcard_url = hcard_url seed_url: seed_url,
@seed_url = seed_url profile_url: profile_url,
@profile_url = profile_url atom_url: atom_url,
@atom_url = atom_url salmon_url: salmon_url,
@salmon_url = salmon_url
# TODO: remove me! ########## # TODO: remove me! ##########
@guid = guid guid: guid,
@public_key = Base64.strict_decode64(public_key) public_key: Base64.strict_decode64(public_key)
############################## )
}
wf
end end
private private

View file

@ -136,6 +136,11 @@ HTML
expect(hcard.last_name).to eq(person.last_name) expect(hcard.last_name).to eq(person.last_name)
end 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 it "searchable is false, if it is empty in html" do
changed_html = html.sub( changed_html = html.sub(
"class=\"searchable\">#{person.searchable}<", "class=\"searchable\">#{person.searchable}<",

View file

@ -61,6 +61,11 @@ XML
expect(wf.public_key).to eq(person.serialized_public_key) expect(wf.public_key).to eq(person.serialized_public_key)
end 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 it "reads old-style XML" do
historic_xml = <<-XML historic_xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>