Permalink
Browse files

improve webfinger failure handling

* do not raise if profile xrd isn't found
* error out on a ssl error rather than on the unexpected nil value later
* be more verbose about failed xrd fetches
  • Loading branch information...
jhass committed Apr 27, 2012
1 parent 0f15c3e commit 9cb803831f72326200913332ce808bfcfbd63f16
Showing with 29 additions and 6 deletions.
  1. +12 −5 lib/webfinger.rb
  2. +1 −1 lib/webfinger_profile.rb
  3. +16 −0 spec/lib/webfinger_spec.rb
View
@@ -28,8 +28,13 @@ def account=(str)
def get(url)
Rails.logger.info("Getting: #{url} for #{account}")
- begin
- Faraday.get(url).body
+ begin
+ res = Faraday.get(url)
+ return false if res.status == 404
+ res.body
+ rescue OpenSSL::SSL::SSLError => e
+ Rails.logger.info "Failed to fetch #{url}: SSL setup invalid"
+ raise e
rescue => e
Rails.logger.info("Failed to fetch: #{url} for #{account}; #{e.message}")
raise e
@@ -51,7 +56,7 @@ def create_or_update_person_from_webfinger_profile!
else
person = make_person_from_webfinger
end
- FEDERATION_LOGGER.info("successfully webfingered#{@account}")
+ FEDERATION_LOGGER.info("successfully webfingered#{@account}") if person
person
end
@@ -64,7 +69,7 @@ def host_meta_xrd
self.ssl = false
retry
else
- raise I18n.t('webfinger.xrd_fetch_failed', :account => account)
+ raise "there was an error getting the xrd from account #{@account}: #{e.message}"
end
end
end
@@ -90,14 +95,16 @@ def webfinger_profile_url
def webfinger_profile_xrd
@webfinger_profile_xrd ||= get(webfinger_profile_url)
+ FEDERATION_LOGGER.info "#{@account} doesn't exists anymore" if @webfinger_profile_xrd == false
+ @webfinger_profile_xrd
end
def hcard_xrd
@hcard_xrd ||= get(hcard_url)
end
def make_person_from_webfinger
- Person.create_from_webfinger(webfinger_profile, hcard)
+ Person.create_from_webfinger(webfinger_profile, hcard) unless webfinger_profile_xrd == false
end
def host_meta_url
View
@@ -20,7 +20,7 @@ def set_fields
doc.remove_namespaces!
account_string = doc.css('Subject').text.gsub('acct:', '').strip
-
+
raise "account in profile(#{account_string}) and account requested (#{@account}) do not match" if account_string != @account
doc.css('Link').each do |l|
View
@@ -72,6 +72,15 @@
a_request(:get, redirect_url).should have_been_made
end
+
+ it 'returns false on 404' do
+ url ="https://bar.com/.well-known/host-meta"
+ stub_request(:get, url).
+ to_return(:status => 404, :body => nil)
+
+ finger.get(url).should_not == nil
+ finger.get(url).should == false
+ end
end
describe 'existing_person_with_profile?' do
@@ -176,10 +185,17 @@
describe '#make_person_from_webfinger' do
it 'with an hcard and a webfinger_profile, it calls Person.create_from_webfinger' do
finger.stub(:hcard).and_return("hcard")
+ finger.stub(:webfinger_profile_xrd).and_return("webfinger_profile_xrd")
finger.stub(:webfinger_profile).and_return("webfinger_profile")
Person.should_receive(:create_from_webfinger).with("webfinger_profile", "hcard")
finger.make_person_from_webfinger
end
+
+ it 'with an false xrd it does not call Person.create_from_webfinger' do
+ finger.stub(:webfinger_profile_xrd).and_return(false)
+ Person.should_not_receive(:create_from_webfinger)
+ finger.make_person_from_webfinger
+ end
end

0 comments on commit 9cb8038

Please sign in to comment.