Skip to content
Extraits de code Groupes Projets
Valider fcdcf88a rédigé par Sarah Mei's avatar Sarah Mei
Parcourir les fichiers

Person.by_webfinger preserves case of identifier. Backfill specs for PublicsController#webfinger.

parent 50f9e8f2
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -15,11 +15,11 @@ class Person ...@@ -15,11 +15,11 @@ class Person
xml_accessor :profile, :as => Profile xml_accessor :profile, :as => Profile
xml_reader :exported_key xml_reader :exported_key
key :url, String key :url, String
key :diaspora_handle, String, :unique => true key :diaspora_handle, String, :unique => true
key :serialized_public_key, String key :serialized_public_key, String
key :owner_id, ObjectId key :owner_id, ObjectId
one :profile, :class_name => 'Profile' one :profile, :class_name => 'Profile'
many :albums, :class_name => 'Album', :foreign_key => :person_id many :albums, :class_name => 'Album', :foreign_key => :person_id
...@@ -31,28 +31,29 @@ class Person ...@@ -31,28 +31,29 @@ class Person
before_validation :clean_url before_validation :clean_url
validates_presence_of :url, :profile, :serialized_public_key validates_presence_of :url, :profile, :serialized_public_key
validates_format_of :url, :with => validates_format_of :url, :with =>
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
def self.search(query) def self.search(query)
return Person.all if query.to_s.empty? return Person.all if query.to_s.empty?
query_tokens = query.to_s.strip.split(" ") query_tokens = query.to_s.strip.split(" ")
full_query_text = Regexp.escape( query.to_s.strip ) full_query_text = Regexp.escape(query.to_s.strip)
p = [] p = []
query_tokens.each do |token| query_tokens.each do |token|
q = Regexp.escape( token.to_s.strip ) q = Regexp.escape(token.to_s.strip)
p = Person.all('profile.first_name' => /^#{q}/i) \ p = Person.all('profile.first_name' => /^#{q}/i) \
| Person.all('profile.last_name' => /^#{q}/i) \ | Person.all('profile.last_name' => /^#{q}/i) \
| p | p
end end
return p return p
end end
def real_name def real_name
"#{profile.first_name.to_s} #{profile.last_name.to_s}" "#{profile.first_name.to_s} #{profile.last_name.to_s}"
end end
def owns?(post) def owns?(post)
self.id == post.person.id self.id == post.person.id
end end
...@@ -71,7 +72,7 @@ class Person ...@@ -71,7 +72,7 @@ class Person
end end
def public_key def public_key
OpenSSL::PKey::RSA.new( serialized_public_key ) OpenSSL::PKey::RSA.new(serialized_public_key)
end end
def exported_key def exported_key
...@@ -83,38 +84,38 @@ class Person ...@@ -83,38 +84,38 @@ class Person
@serialized_public_key = new_key @serialized_public_key = new_key
end end
def self.by_webfinger( identifier, opts = {}) def self.by_webfinger(identifier, opts = {})
#need to check if this is a valid email structure, maybe should do in JS #need to check if this is a valid email structure, maybe should do in JS
local_person = Person.first(:diaspora_handle => identifier.gsub('acct:', '').to_s.downcase) local_person = Person.first(:diaspora_handle => identifier.gsub('acct:', '').to_s)
if local_person if local_person
Rails.logger.info("Do not need to webfinger, found a local person #{local_person.real_name}") Rails.logger.info("Do not need to webfinger, found a local person #{local_person.real_name}")
local_person local_person
elsif !identifier.include?("localhost") && !opts[:local] elsif !identifier.include?("localhost") && !opts[:local]
begin begin
Rails.logger.info("Webfingering #{identifier}") Rails.logger.info("Webfingering #{identifier}")
f = Redfinger.finger(identifier) f = Redfinger.finger(identifier)
rescue SocketError => e rescue SocketError => e
raise "Diaspora server for #{identifier} not found" if e.message =~ /Name or service not known/ raise "Diaspora server for #{identifier} not found" if e.message =~ /Name or service not known/
rescue Errno::ETIMEDOUT => e rescue Errno::ETIMEDOUT => e
raise "Connection timed out to Diaspora server for #{identifier}" raise "Connection timed out to Diaspora server for #{identifier}"
end end
raise "No webfinger profile found at #{identifier}" if f.nil? || f.links.empty? raise "No webfinger profile found at #{identifier}" if f.nil? || f.links.empty?
Person.from_webfinger_profile(identifier, f ) Person.from_webfinger_profile(identifier, f)
end end
end end
def self.from_webfinger_profile( identifier, profile) def self.from_webfinger_profile(identifier, profile)
new_person = Person.new new_person = Person.new
public_key_entry = profile.links.select{|x| x.rel == 'diaspora-public-key'} public_key_entry = profile.links.select { |x| x.rel == 'diaspora-public-key' }
return nil unless public_key_entry return nil unless public_key_entry
pubkey = public_key_entry.first.href pubkey = public_key_entry.first.href
new_person.exported_key = Base64.decode64 pubkey new_person.exported_key = Base64.decode64 pubkey
guid = profile.links.select{|x| x.rel == 'http://joindiaspora.com/guid'}.first.href guid = profile.links.select { |x| x.rel == 'http://joindiaspora.com/guid' }.first.href
new_person.id = guid new_person.id = guid
new_person.diaspora_handle = identifier new_person.diaspora_handle = identifier
...@@ -151,12 +152,12 @@ class Person ...@@ -151,12 +152,12 @@ class Person
self.url ||= "http://localhost:3000/" if self.class == User self.url ||= "http://localhost:3000/" if self.class == User
if self.url if self.url
self.url = 'http://' + self.url unless self.url.match('http://' || 'https://') self.url = 'http://' + self.url unless self.url.match('http://' || 'https://')
self.url = self.url + '/' if self.url[-1,1] != '/' self.url = self.url + '/' if self.url[-1, 1] != '/'
end end
end end
private private
def remove_all_traces def remove_all_traces
Post.all(:person_id => id).each{|p| p.delete} Post.all(:person_id => id).each { |p| p.delete }
end end
end end
...@@ -49,9 +49,28 @@ describe PublicsController do ...@@ -49,9 +49,28 @@ describe PublicsController do
end end
describe 'webfinger' do describe 'webfinger' do
it 'should not try to webfinger out on a request to webfinger' do it "succeeds when the person and user exist locally" do
Redfinger.should_not_receive :finger user = Factory(:user)
post :webfinger, :q => 'remote@example.com' post :webfinger, 'q' => user.person.diaspora_handle
response.should be_success
end
it "404s when the person exists remotely because it is local only" do
stub_success('me@mydiaspora.pod.com')
post :webfinger, 'q' => 'me@mydiaspora.pod.com'
response.should be_not_found
end
it "404s when the person is local but doesn't have an owner" do
person = Factory(:person)
post :webfinger, 'q' => person.diaspora_handle
response.should be_not_found
end
it "404s when the person does not exist locally or remotely" do
stub_failure('me@mydiaspora.pod.com')
post :webfinger, 'q' => 'me@mydiaspora.pod.com'
response.should be_not_found
end end
end end
......
...@@ -164,17 +164,31 @@ describe Person do ...@@ -164,17 +164,31 @@ describe Person do
people = Person.search("Casey Grippi") people = Person.search("Casey Grippi")
people.should == [@friend_four] people.should == [@friend_four]
end end
end
it 'should search by diaspora_handle exactly' do describe ".by_webfinger" do
stub_success("tom@tom.joindiaspora.com") context "local people" do
Person.by_webfinger(@friend_one.diaspora_handle).should == @friend_one before do
@local_person = Factory(:person)
Redfinger.should_not_receive :finger
end
it "finds the local person without calling out" do
person = Person.by_webfinger(@local_person.diaspora_handle)
person.should == @local_person
end
it "finds a local person with a mixed-case username" do
user = Factory(:user, :username => "SaMaNtHa")
person = Person.by_webfinger(user.person.diaspora_handle)
person.should == user.person
end
end end
it 'should create a stub for a remote user' do it 'creates a stub for a remote user' do
stub_success("tom@tom.joindiaspora.com") stub_success("tom@tom.joindiaspora.com")
tom = Person.by_webfinger('tom@tom.joindiaspora.com') tom = Person.by_webfinger('tom@tom.joindiaspora.com')
tom.real_name.include?("Hamiltom").should be true tom.real_name.include?("Hamiltom").should be true
end end
end end
end end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter