Class: Salmon::EncryptedSlap

Inherits:
Slap
  • Object
show all
Defined in:
lib/salmon/encrypted_slap.rb

Instance Attribute Summary

Attributes inherited from Slap

#aes_key, #author, #author_id, #iv, #magic_sig, #parsed_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Slap

b64_to_n, build_key, create_by_user_and_activity, decode64url, from_xml, parse_key, #verified_for_key?

Class Method Details

.payload(activity, user, aes_key_hash) ⇒ String

Encrypt the magic sig

Returns:


63
64
65
# File 'lib/salmon/encrypted_slap.rb', line 63

def self.payload(activity, user, aes_key_hash)
  user.person.aes_encrypt(activity, aes_key_hash)
end

Instance Method Details

#header(person) ⇒ String

Construct an encrypted header

Returns:


10
11
12
13
14
15
16
# File 'lib/salmon/encrypted_slap.rb', line 10

def header(person)
  <<XML
    <encrypted_header>
      #{person.encrypt(plaintext_header)}
    </encrypted_header>
XML
end

#parse_data(user) ⇒ Object

Decrypts an encrypted magic sig envelope

Parameters:

  • key_hash (Hash)

    Contains 'key' (aes) and 'iv' values

  • user (User)

50
51
52
# File 'lib/salmon/encrypted_slap.rb', line 50

def parse_data(user)
  user.aes_decrypt(super, {'key' => self.aes_key, 'iv' => self.iv})
end

#plaintext_headerObject


18
19
20
21
22
23
24
25
26
# File 'lib/salmon/encrypted_slap.rb', line 18

def plaintext_header
  header =<<HEADER
<decrypted_header>
<iv>#{iv}</iv>
<aes_key>#{aes_key}</aes_key>
<author_id>#{@author.diaspora_handle}</author_id>
</decrypted_header>
HEADER
end

#process_header(doc) ⇒ Hash

Takes in a doc of the header and sets the author id returns an empty hash

Returns:

  • (Hash)

41
42
43
44
45
# File 'lib/salmon/encrypted_slap.rb', line 41

def process_header(doc)
  self.author_id   = doc.search('author_id').text
  self.aes_key     = doc.search('aes_key').text
  self.iv          = doc.search('iv').text
end

#salmon_header(doc, user) ⇒ Nokogiri::Doc

Decrypts and parses out the salmon header

Returns:

  • (Nokogiri::Doc)

56
57
58
59
# File 'lib/salmon/encrypted_slap.rb', line 56

def salmon_header(doc, user)
  header = user.decrypt(doc.search('encrypted_header').text)
  Nokogiri::XML(header)
end

#xml_for(person) ⇒ String, Boolean

Returns False if RSAError; XML if no error.

Returns:

  • (String, Boolean)

    False if RSAError; XML if no error


29
30
31
32
33
34
35
36
# File 'lib/salmon/encrypted_slap.rb', line 29

def xml_for(person)
  begin
   super 
  rescue OpenSSL::PKey::RSAError => e
    Rails.logger.info("event => :invalid_rsa_key, :identifier => #{person.diaspora_handle}")
    false
  end
end