From 57e345e3af6b624626b7a2402dbed7745c890044 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 14 Sep 2011 14:11:38 -0400 Subject: [PATCH] extracting key generation out of author --- app/models/author.rb | 14 +------------- app/models/user.rb | 10 +++++----- lib/crypto.rb | 35 +++++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/app/models/author.rb b/app/models/author.rb index 26dc599e..8668e2c2 100644 --- a/app/models/author.rb +++ b/app/models/author.rb @@ -90,19 +90,7 @@ def reset_key_lease # Retrieves a valid RSA::KeyPair for the Author's public key def retrieve_public_key - # Create the public key from the key stored - - # Retrieve the exponent and modulus from the key string - public_key.match /^RSA\.(.*?)\.(.*)$/ - modulus = Base64::urlsafe_decode64($1) - exponent = Base64::urlsafe_decode64($2) - - modulus = modulus.bytes.inject(0) {|num, byte| (num << 8) | byte } - exponent = exponent.bytes.inject(0) { |num, byte| (num << 8) | byte } - - # Create the public key instance - key = RSA::Key.new(modulus, exponent) - keypair = RSA::KeyPair.new(nil, key) + Crypto.make_rsa_keypair(public_key, nil) end # Returns a locally useful url for the Author diff --git a/app/models/user.rb b/app/models/user.rb index 36066dc6..312b1c81 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,8 +60,8 @@ def generate_rsa_pair end # Retrieves a valid RSA::KeyPair for the User's private key - def self.to_rsa_key - Crypto.make_rsa_key(nil, private_key) + def self.to_rsa_keypair + Crypto.make_rsa_keypair(nil, private_key) end # After a user is created, create the feed and reset the token @@ -169,7 +169,7 @@ def send_follow_notification to_feed_id salmon = OStatus::Salmon.from_follow(author.to_atom, f.author.to_atom) - envelope = salmon.to_xml self.to_rsa_key + envelope = salmon.to_xml self.to_rsa_keypair # Send envelope to Author's Salmon endpoint uri = URI.parse(f.author.salmon_url) @@ -197,7 +197,7 @@ def send_unfollow_notification to_feed_id salmon = OStatus::Salmon.from_unfollow(author.to_atom, f.author.to_atom) - envelope = salmon.to_xml self.to_rsa_key + envelope = salmon.to_xml self.to_rsa_keypair # Send envelope to Author's Salmon endpoint uri = URI.parse(f.author.salmon_url) @@ -213,7 +213,7 @@ def send_mention_notification update_id, to_feed_id base_uri = "http://#{author.domain}/" salmon = OStatus::Salmon.new(u.to_atom(base_uri)) - envelope = salmon.to_xml self.to_rsa_key + envelope = salmon.to_xml self.to_rsa_keypair # Send envelope to Author's Salmon endpoint uri = URI.parse(f.author.salmon_url) diff --git a/lib/crypto.rb b/lib/crypto.rb index c17a8eba..258b2041 100644 --- a/lib/crypto.rb +++ b/lib/crypto.rb @@ -54,17 +54,28 @@ def self.generate_keypair # We don't yet do anything with the public key, but I added it so that when we # need to, it'll be there. - def self.make_rsa_key(public_key, private_key) - # Retrieve the exponent and modulus from the key string - private_key.match /^RSA\.(.*?)\.(.*)$/ - modulus = Base64::urlsafe_decode64($1) - exponent = Base64::urlsafe_decode64($2) - - modulus = modulus.bytes.inject(0) {|num, byte| (num << 8) | byte } - exponent = exponent.bytes.inject(0) { |num, byte| (num << 8) | byte } - - # Create the public key instance - key = RSA::Key.new(modulus, exponent) - RSA::KeyPair.new(key, nil) + def self.make_rsa_keypair(public_key, private_key) + private_key = generate_key(private_key) + public_key = generate_key(public_key) + + RSA::KeyPair.new(private_key, public_key) + end + + private + + def generate_key(key_string) + return nil unless key_string + + key_string.match /^RSA\.(.*?)\.(.*)$/ + + modulus = decode_key($1) + exponent = decode_key($2) + + RSA::Key.new(modulus, exponent) + end + + def decode_key(encoded_key_part) + modulus = Base64::urlsafe_decode64(encoded_key_part) + modulus.bytes.inject(0) {|num, byte| (num << 8) | byte } end end