Skip to content
Extraits de code Groupes Projets
Valider 5e6d0c11 rédigé par maxwell's avatar maxwell
Parcourir les fichiers

Merge branch 'master' of github.com:diaspora/diaspora_rails into

friend-refactor, also fixed more specs

Conflicts:
	app/models/person.rb
	app/models/user.rb
	spec/factories.rb
parents cc04d4e0 124fa7c7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 97 ajouts et 94 suppressions
......@@ -4,5 +4,4 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
ENV['GNUPGHOME'] = File.expand_path("../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
Rails::Application.load_tasks
......@@ -5,13 +5,15 @@ class Person
xml_accessor :_id
xml_accessor :email
xml_accessor :url
xml_accessor :key_fingerprint
xml_accessor :serialized_key
xml_accessor :profile, :as => Profile
key :email, String, :unique => true
key :url, String
key :key_fingerprint, String
key :serialized_key, String
key :owner_id, ObjectId
key :user_refs, Integer, :default => 0
......@@ -26,26 +28,28 @@ class Person
timestamps!
before_validation :clean_url
validates_presence_of :email, :url, :key_fingerprint, :profile
validates_presence_of :email, :url, :serialized_key, :profile
validates_format_of :url, :with =>
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
after_destroy :remove_all_traces, :remove_key
scope :friends, where(:_type => "Person", :active => true)
after_destroy :remove_all_traces
def real_name
"#{profile.first_name.to_s} #{profile.last_name.to_s}"
end
def key
GPGME::Ctx.new.get_key key_fingerprint
OpenSSL::PKey::RSA.new( serialized_key )
end
def key= new_key
raise TypeError unless new_key.class == OpenSSL::PKey::RSA
serialized_key = new_key.export
end
def export_key
GPGME::export(key_fingerprint, :armor => true)
key.public_key.export
end
......@@ -82,6 +86,9 @@ class Person
end
end
def mine?(post)
self == post.person
end
protected
......@@ -99,10 +106,4 @@ class Person
self.posts.delete_all
end
def remove_key
puts 'Removing key from keyring in test environment' if Rails.env == 'test'
ctx = GPGME::Ctx.new
ctx.delete_key(key)
end
end
......@@ -49,24 +49,24 @@ class Post
end
#ENCRYPTION
before_validation :sign_if_mine
validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature}
xml_accessor :creator_signature
key :creator_signature, String
def signable_accessors
accessors = self.class.roxml_attrs.collect{|definition|
definition.accessor}
accessors.delete 'person'
accessors.delete 'creator_signature'
accessors
end
before_validation :sign_if_mine
validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature}
xml_accessor :creator_signature
key :creator_signature, String
def signable_accessors
accessors = self.class.roxml_attrs.collect{|definition|
definition.accessor}
accessors.delete 'person'
accessors.delete 'creator_signature'
accessors
end
def signable_string
signable_accessors.collect{|accessor|
(self.send accessor.to_sym).to_s}.join ';'
end
def signable_string
signable_accessors.collect{|accessor|
(self.send accessor.to_sym).to_s}.join ';'
end
def log_inspection
Rails.logger.info self.inspect
......
......@@ -20,6 +20,10 @@ class Profile
self._parent_document.id
end
def person
Person.first(:id => self.person_id)
end
def to_diaspora_xml
"<post>"+ self.to_xml.to_s + "</post>"
end
......
......@@ -28,15 +28,23 @@ class Request
scope :for_user, lambda{ |user| where(:destination_url => user.url) }
scope :from_user, lambda{ |user| where(:destination_url.ne => user.url) }
def self.instantiate(options ={})
def self.instantiate(options = {})
person = options[:from]
self.new(:destination_url => options[:to], :callback_url => person.url, :person => person, :exported_key => person.export_key)
end
def activate_friend
p = Person.where(:url => self.person.url).first
p.active = true
p.save
from_user = Person.first(:url => self.callback_url).owner
puts from_user.inspect
from_user.friends << from_user.pending_friends.delete(person)
end
def set_pending_friend
p = Person.first(:id => self.person.id)
puts p.inspect
self.person.save #save pending friend
end
#ENCRYPTION
......
......@@ -20,15 +20,23 @@ class Retraction
attr_accessor :type
def perform
return unless verify_signature(@creator_signature, Post.first(:id => post_id).person)
begin
return unless signature_valid?
self.type.constantize.destroy(self.post_id)
rescue NameError
Rails.logger.info("Retraction for unknown type recieved.")
end
end
def signature_valid?
target = self.type.constantize.first(:id => self.post_id)
if target.is_a? Person
verify_signature(@creator_signature, self.type.constantize.first(:id => self.post_id))
else
verify_signature(@creator_signature, self.type.constantize.first(:id => self.post_id).person)
end
end
def self.person_id_from(object)
if object.is_a? Person
object.id
......
......@@ -10,8 +10,10 @@ class User
one :person, :class_name => 'Person', :foreign_key => :owner_id
many :friends, :in => :friend_ids, :class_name => 'Person'
#before_validation_on_create :assign_key
before_validation_on_create :assign_key
before_validation :do_bad_things
######## Posting ########
def method_missing(method, *args)
......@@ -32,7 +34,7 @@ class User
######### Friend Requesting
def send_friend_request_to(friend_url)
unless Person.where(:url => friend_url).first
p = Request.instantiate(:to => friend_url, :from => self)
p = Request.instantiate(:to => friend_url, :from => self.person)
if p.save
p.push_to_url friend_url
end
......@@ -53,18 +55,22 @@ class User
def ignore_friend_request(friend_request_id)
request = Request.first(:id => friend_request_id)
person = request.person
person.destroy unless person.active
person.destroy unless self.friends.include? person
request.destroy
end
def receive_friend_request(friend_request)
Rails.logger.info("receiving friend request #{friend_request.to_json}")
GPGME.import(friend_request.exported_key)
friend_request.person.serialized_key = friend_request.exported_key
if Request.where(:callback_url => friend_request.callback_url).first
friend_request.activate_friend
friend_request.destroy
else
friend_request.person.save
friend_request.create_pending_friend
friend_request.save
end
end
......@@ -92,9 +98,7 @@ class User
###Helpers############
def mine?(post)
self == post.person
end
def terse_url
terse= self.url.gsub(/https?:\/\//, '')
......@@ -114,32 +118,11 @@ class User
protected
def assign_key
keys = GPGME.list_keys(self.real_name, true)
if keys.empty?
generate_key
end
self.key_fingerprint = GPGME.list_keys(self.real_name, true).first.subkeys.first.fingerprint
self.person.serialized_key ||= generate_key.export
end
def generate_key
puts "Generating key"
puts paramstring
ctx = GPGME::Ctx.new
ctx.genkey(paramstring, nil, nil)
OpenSSL::PKey::RSA::generate 1024
end
def paramstring
"<GnupgKeyParms format=\"internal\">
Key-Type: DSA
Key-Length: 512
Subkey-Type: ELG-E
Subkey-Length: 512
Name-Real: #{self.real_name}
Name-Comment: #{self.url}
Name-Email: #{self.email}
Expire-Date: 0
</GnupgKeyParms>"
end
end
......@@ -4,5 +4,4 @@ Haml::Template.options[:format] = :html5
# Initialize the rails application
Diaspora::Application.initialize!
ENV['GNUPGHOME'] = File.expand_path("../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
......@@ -26,8 +26,7 @@ Diaspora::Application.configure do
config.action_mailer.delivery_method = :test
config.threadsafe!
ENV['GNUPGHOME'] = File.expand_path("../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
......
......@@ -7,9 +7,6 @@
# Mayor.create(:name => 'Daley', :city => citie
require 'config/environment'
ENV['GNUPGHOME'] = File.expand_path("../../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
def create(backer_number)
backer_info = [ [5072,"George", "Washington"],
......
......@@ -7,8 +7,7 @@
# Mayor.create(:name => 'Daley', :city => citie
require 'config/environment'
ENV['GNUPGHOME'] = File.expand_path("../../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
# Create seed user
user = User.create( :email => "robert@joindiaspora.com", :password => "evankorth", :profile => Profile.new( :first_name => "bobert", :last_name => "brin" ))
......
......@@ -7,8 +7,7 @@
# Mayor.create(:name => 'Daley', :city => citie
require 'config/environment'
ENV['GNUPGHOME'] = File.expand_path("../../../gpg/diaspora-#{Rails.env}/", __FILE__)
GPGME::check_version({})
# Create seed user
user = User.create( :email => "tom@tom.joindiaspora.com", :password => "evankorth", :url => "http://tom.joindiaspora.com/", :profile => Profile.new( :first_name => "Alexander", :last_name => "Hamiltom" ))
......
Fichier supprimé
Fichier supprimé
Fichier supprimé
......@@ -7,7 +7,7 @@ module Diaspora
@@queue = MessageHandler.new
def notify_people
if self.person_id == User.owner.id
if self.person_id == User.owner.person.id
push_to(people_with_permissions)
end
end
......@@ -39,7 +39,8 @@ module Diaspora
end
def people_with_permissions
Person.friends.all
puts "#{self.person.owner.friends.count} foo"
self.person.owner.friends.all
end
def self.build_xml_for(posts)
......
......@@ -7,21 +7,27 @@
end
def verify_signature(signature, person)
return false unless signature && person.key_fingerprint
validity = nil
if person.nil?
Rails.logger.info("Verifying sig on #{signable_string} but no person is here")
return false
elsif person.key.nil?
Rails.logger.info("Verifying sig on #{signable_string} but #{person.real_name} has no key")
return false
elsif signature.nil?
Rails.logger.info("Verifying sig on #{signable_string} but #{person.real_name} did not sign")
return false
end
Rails.logger.info("Verifying sig on #{signable_string} from person #{person.real_name}")
GPGME::verify(signature, signable_string,
{:armor => true, :always_trust => true}){ |signature_analysis|
#puts signature_analysis
validity = signature_analysis.status == GPGME::GPG_ERR_NO_ERROR &&
signature_analysis.fpr == person.key_fingerprint
}
return validity
validity = person.key.verify "SHA", Base64.decode64(signature), signable_string
Rails.logger.info("Validity: #{validity}")
validity
end
protected
def sign_if_mine
if self.person == User.owner
self.creator_signature = sign
end
end
......@@ -32,8 +38,8 @@
def sign_with_key(key)
Rails.logger.info("Signing #{signable_string}")
GPGME::sign(signable_string,nil,
{:armor=> true, :mode => GPGME::SIG_MODE_DETACH, :signers => [key]})
Base64.encode64(key.sign "SHA", signable_string)
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