Skip to content

Commit

Permalink
extracting key generation out of author
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Sep 14, 2011
1 parent e197337 commit 57e345e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
14 changes: 1 addition & 13 deletions app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
35 changes: 23 additions & 12 deletions lib/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 57e345e

Please sign in to comment.