Class: Salmon::MagicSigEnvelope

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#algObject

Returns the value of attribute alg


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def alg
  @alg
end

#authorObject

Returns the value of attribute author


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def author
  @author
end

#dataObject

Returns the value of attribute data


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def data
  @data
end

#data_typeObject

Returns the value of attribute data_type


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def data_type
  @data_type
end

#encodingObject

Returns the value of attribute encoding


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def encoding
  @encoding
end

#sigObject

Returns the value of attribute sig


8
9
10
# File 'lib/salmon/magic_sig_envelope.rb', line 8

def sig
  @sig
end

Class Method Details

.create(user, activity) ⇒ MagicSigEnvelope


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/salmon/magic_sig_envelope.rb', line 34

def self.create(user, activity)
  env = MagicSigEnvelope.new
  env.author = user.person
  env.data = Base64.urlsafe_encode64(activity)
  env.data_type = env.get_data_type
  env.encoding  = env.get_encoding
  env.alg = env.get_alg

  #TODO: WHY DO WE DOUBLE ENCODE
  env.sig = Base64.urlsafe_encode64(
    user.encryption_key.sign OpenSSL::Digest::SHA256.new, env.signable_string )

  env
end

.parse(doc) ⇒ MagicSigEnvelope


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/salmon/magic_sig_envelope.rb', line 11

def self.parse(doc)
  env = self.new
  ns = {'me'=>'http://salmon-protocol.org/ns/magic-env'}
  env.encoding = doc.search('//me:env/me:encoding', ns).text.strip

  if env.encoding != 'base64url'
    raise ArgumentError, "Magic Signature data must be encoded with base64url, was #{env.encoding}"
  end

  env.data =  doc.search('//me:env/me:data', ns).text
  env.alg = doc.search('//me:env/me:alg', ns).text.strip

  unless 'RSA-SHA256' == env.alg
    raise ArgumentError, "Magic Signature data must be signed with RSA-SHA256, was #{env.alg}"
  end

  env.sig =  doc.search('//me:env/me:sig', ns).text
  env.data_type = doc.search('//me:env/me:data', ns).first['type'].strip

  env
end

Instance Method Details

#get_algString


77
78
79
# File 'lib/salmon/magic_sig_envelope.rb', line 77

def get_alg
  'RSA-SHA256'
end

#get_data_typeString


72
73
74
# File 'lib/salmon/magic_sig_envelope.rb', line 72

def get_data_type
  'application/xml'
end

#get_encodingString


67
68
69
# File 'lib/salmon/magic_sig_envelope.rb', line 67

def get_encoding
  'base64url'
end

#signable_stringString


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

def signable_string
  [@data, Base64.urlsafe_encode64(@data_type),Base64.urlsafe_encode64(@encoding),  Base64.urlsafe_encode64(@alg)].join(".")
end

#to_xmlString


55
56
57
58
59
60
61
62
63
64
# File 'lib/salmon/magic_sig_envelope.rb', line 55

def to_xml
  "<me:env>\n  <me:data type='\#{@data_type}'>\#{@data}</me:data>\n  <me:encoding>\#{@encoding}</me:encoding>\n  <me:alg>\#{@alg}</me:alg>\n  <me:sig>\#{@sig}</me:sig>\n  </me:env>\n"
end