Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #1

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--format nested
13 changes: 4 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ begin
gem.email = "[email protected]"
gem.homepage = "http://github.com/mbleigh/redfinger"
gem.authors = ["Michael Bleigh"]
gem.add_dependency "rest-client"
gem.add_dependency "rest-client", ">= 1.5.0"
gem.add_dependency "nokogiri", ">= 1.4.0"
gem.add_dependency "hashie"
gem.add_development_dependency "rspec", ">= 1.2.9"
Expand All @@ -22,15 +22,10 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov = true
end

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.1.0
41 changes: 28 additions & 13 deletions lib/redfinger/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

module Redfinger
class Client
attr_accessor :account, :domain, :uri_template
attr_accessor :account, :domain, :uri_template, :xrd_timeout, :xrd_open_timeout

def initialize(email, uri_template = nil)
self.account = urify(email)
self.account = normalize(email)
self.domain = account.split('@').last

self.xrd_timeout = 10
self.xrd_open_timeout = 5
end

def finger
self.uri_template ||= retrieve_template_from_xrd
Finger.new RestClient.get(swizzle).body
begin
return Finger.new self.account, RestClient.get(swizzle).body
rescue RestClient::ResourceNotFound
return Finger.new self.account, RestClient.get(swizzle(account_with_scheme)).body
end
end

def xrd_url(ssl = true)
Expand All @@ -22,34 +29,42 @@ def xrd_url(ssl = true)

private

def swizzle
def swizzle(account = nil)
account ||= self.account
uri_template.gsub '{uri}', URI.escape(self.account)
end

def retrieve_template_from_xrd(ssl = true)
doc = Nokogiri::XML::Document.parse(RestClient.get(xrd_url(ssl)).body)
if doc.namespaces["xmlns:hm"] != "http://host-meta.net/xrd/1.0"
xrd_client = RestClient::Resource.new(xrd_url(ssl),
:timeout => self.xrd_timeout,
:open_timeout => self.xrd_open_timeout
)

doc = Nokogiri::XML::Document.parse(xrd_client.get.body)
if doc.namespaces["xmlns"] != "http://docs.oasis-open.org/ns/xri/xrd-1.0"
# it's probably not finger, let's try without ssl
# http://code.google.com/p/webfinger/wiki/WebFingerProtocol
# says first ssl should be tried then without ssl, should fix issue #2
doc = Nokogiri::XML::Document.parse(RestClient.get(xrd_url(false)).body)
end
unless doc.at_xpath('.//hm:Host').content == self.domain
raise Redfinger::SecurityException, "The XRD document's host did not match the account's host."
end

doc.at('Link[rel=lrdd]').attribute('template').value
rescue Errno::ECONNREFUSED, RestClient::ResourceNotFound
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT,
RestClient::RequestTimeout, RestClient::ResourceNotFound, RestClient::Forbidden
if ssl
retrieve_template_from_xrd(false)
else
raise Redfinger::ResourceNotFound, "Unable to find the host XRD file."
end
end

def urify(email)
email = "acct:#{email}" unless email.include?("acct:")
email
def normalize(email)
email.sub! /^acct:/, ''
email.downcase
end

def account_with_scheme
"acct:" + account
end
end
end
6 changes: 3 additions & 3 deletions lib/redfinger/finger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Redfinger
# special helpers that are availale to pull specific types
# of URLs, see Redfinger::LinkHelpers
class Finger
def initialize(xml) # :nodoc:
def initialize(subject, xml) # :nodoc:
@doc = Nokogiri::XML::Document.parse(xml)
@subject = @doc.at_css('Subject').content
@subject = subject
end

# All of the links provided by the Webfinger response.
def links
@links ||= @doc.css('Link').map{|l| Link.new(l)}
@links ||= @doc.css('Link').map{|l| Link.from_xml(l)}
end

def inspect # :nodoc:
Expand Down
10 changes: 6 additions & 4 deletions lib/redfinger/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

module Redfinger
class Link < Hashie::Mash
def initialize(xml_link)
self[:rel] = xml_link['rel']
self[:href] = xml_link['href']
self[:type] = xml_link['type']
def self.from_xml(xml_link)
new_link = Link.new
new_link[:rel] = xml_link['rel']
new_link[:href] = xml_link['href']
new_link[:type] = xml_link['type']
new_link
end

# Outputs the URL of the link, useful for using
Expand Down
2 changes: 1 addition & 1 deletion lib/redfinger/link_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def xfn
protected

def relmap(uri, substring=false)
@doc.css("Link[rel#{'^' if substring}=\"#{uri}\"]").map{|e| Link.new(e)}
@doc.css("Link[rel#{'^' if substring}=\"#{uri}\"]").map{|e| Link.from_xml(e)}
end
end
end
54 changes: 26 additions & 28 deletions redfinger.gemspec
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{redfinger}
s.version = "0.0.3"
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Bleigh"]
s.date = %q{2010-02-13}
s.date = %q{2011-02-07}
s.description = %q{A Ruby Webfinger client}
s.email = %q{[email protected]}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
"README.rdoc"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/redfinger.rb",
"lib/redfinger/client.rb",
"lib/redfinger/finger.rb",
"lib/redfinger/link.rb",
"lib/redfinger/link_helpers.rb",
"redfinger.gemspec",
"spec/redfinger/client_spec.rb",
"spec/redfinger_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
".rspec",
"LICENSE",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/redfinger.rb",
"lib/redfinger/client.rb",
"lib/redfinger/finger.rb",
"lib/redfinger/link.rb",
"lib/redfinger/link_helpers.rb",
"redfinger.gemspec",
"spec/redfinger/client_spec.rb",
"spec/redfinger_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/mbleigh/redfinger}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.rubygems_version = %q{1.5.0}
s.summary = %q{A Ruby WebFinger client.}
s.test_files = [
"spec/redfinger/client_spec.rb",
"spec/redfinger_spec.rb",
"spec/spec_helper.rb"
"spec/redfinger_spec.rb",
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<rest-client>, [">= 1.5.0"])
s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.0"])
s.add_runtime_dependency(%q<hashie>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
s.add_development_dependency(%q<webmock>, [">= 0"])
else
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 1.5.0"])
s.add_dependency(%q<nokogiri>, [">= 1.4.0"])
s.add_dependency(%q<hashie>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<webmock>, [">= 0"])
end
else
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 1.5.0"])
s.add_dependency(%q<nokogiri>, [">= 1.4.0"])
s.add_dependency(%q<hashie>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
Expand Down
31 changes: 20 additions & 11 deletions spec/redfinger/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ class HaltSuccessError < StandardError; end

describe Redfinger::Client do
describe '#new' do
it 'should add acct: if it is not in URI form' do
Redfinger::Client.new('[email protected]').account.should == 'acct:[email protected]'
end

it 'should not add acct: if it is already in URI form' do
Redfinger::Client.new('acct:[email protected]').account.should == 'acct:[email protected]'
it 'should remove acct: if it is already in URI form' do
Redfinger::Client.new('acct:[email protected]').account.should == '[email protected]'
end

it 'should set the domain to whatevers after the @ sign' do
Expand All @@ -29,6 +25,24 @@ class HaltSuccessError < StandardError; end
stub_request(:get, 'http://example.com/.well-known/host-meta').to_raise(HaltSuccessError)
lambda{Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd)}.should raise_error(HaltSuccessError)
end

it 'should make an HTTP request if the HTTPS request times out in Net::HTTP' do
stub_request(:get, 'https://example.com/.well-known/host-meta').to_raise(Errno::ETIMEDOUT)
stub_request(:get, 'http://example.com/.well-known/host-meta').to_raise(HaltSuccessError)
lambda{Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd)}.should raise_error(HaltSuccessError)
end

it 'should make an HTTP request if the HTTPS request times out in RestClient' do
stub_request(:get, 'https://example.com/.well-known/host-meta').to_raise(RestClient::RequestTimeout)
stub_request(:get, 'http://example.com/.well-known/host-meta').to_raise(HaltSuccessError)
lambda{Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd)}.should raise_error(HaltSuccessError)
end

it 'should make an HTTP request if the server throws a 403 forbidden on the HTTPS request' do
stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(:status => [403, "Forbidden"])
stub_request(:get, 'http://example.com/.well-known/host-meta').to_raise(HaltSuccessError)
lambda{Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd)}.should raise_error(HaltSuccessError)
end

it 'should raise Redfinger::ResourceNotFound if HTTP fails as well' do
stub_request(:get, 'https://example.com/.well-known/host-meta').to_raise(Errno::ECONNREFUSED)
Expand All @@ -45,11 +59,6 @@ class HaltSuccessError < StandardError; end
stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(:status => 200, :body => host_xrd)
Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd).should == 'http://example.com/webfinger/?q={uri}'
end

it 'should raise a SecurityException if there is a host mismatch' do
stub_request(:get, 'https://franklin.com/.well-known/host-meta').to_return(:status => 200, :body => host_xrd)
lambda{Redfinger::Client.new('acct:[email protected]').send(:retrieve_template_from_xrd)}.should raise_error(Redfinger::SecurityException)
end
end

describe '#finger' do
Expand Down
19 changes: 7 additions & 12 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@

require 'rubygems'
require 'redfinger'
require 'spec'
require 'spec/autorun'
require 'rspec'
require 'rspec/autorun'
require 'webmock/rspec'

include WebMock
include WebMock::API

def host_xrd
<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<!-- NOTE: this host-meta end-point is a pre-alpha work in progress. Don't rely on it. -->
<!-- Please follow the list at http://groups.google.com/group/webfinger -->
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'
xmlns:hm='http://host-meta.net/xrd/1.0'>
<hm:Host xmlns='http://host-meta.net/xrd/1.0'>example.com</hm:Host>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
<Link rel='lrdd'
template='http://example.com/webfinger/?q={uri}'>
<Title>Resource Descriptor</Title>
Expand All @@ -29,7 +25,6 @@ def finger_xrd
<<-XML
<?xml version='1.0'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
<Subject>acct:[email protected]</Subject>
<Alias>http://www.google.com/profiles/abc</Alias>
<Link rel='http://portablecontacts.net/spec/1.0' href='http://www-opensocial.googleusercontent.com/api/people/'/>
<Link rel='http://webfinger.net/rel/profile-page' href='http://www.google.com/profiles/abc' type='text/html'/>
Expand All @@ -43,11 +38,11 @@ def finger_xrd
XML
end

def stub_success
def stub_success(address = '[email protected]')
stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(:status => 200, :body => host_xrd)
stub_request(:get, /webfinger\/\?q=.*/).to_return(:status => 200, :body => finger_xrd)
stub_request(:get, /webfinger\/\?q=#{address}/).to_return(:status => 200, :body => finger_xrd)
end

Spec::Runner.configure do |config|
RSpec.configure do |config|

end