Class: OmniAuth::Suomifi::Test::XmlEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth-suomifi/test/xml_encryptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ XmlEncryptor

Returns a new instance of XmlEncryptor.



11
12
13
14
15
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 11

def initialize(opts)
  @certificate = opts[:encryption_certificate]
  @sign_certificate = opts[:sign_certificate]
  @sign_key = opts[:sign_key]
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



9
10
11
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 9

def certificate
  @certificate
end

#sign_certificateObject (readonly)

Returns the value of attribute sign_certificate.



9
10
11
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 9

def sign_certificate
  @sign_certificate
end

#sign_keyObject (readonly)

Returns the value of attribute sign_key.



9
10
11
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 9

def sign_key
  @sign_key
end

Class Method Details

.encrypted_xml(raw_xml_file, cert, sign_cert, sign_key) ⇒ Object



35
36
37
38
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 35

def self.encrypted_xml(raw_xml_file, cert, sign_cert, sign_key)
  raw_xml = File.read(raw_xml_file)
  encrypted_xml_from_string(raw_xml, cert, sign_cert, sign_key)
end

.encrypted_xml_from_string(raw_xml, cert, sign_cert, sign_key) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 40

def self.encrypted_xml_from_string(raw_xml, cert, sign_cert, sign_key)
  enc = new(
    encryption_certificate: cert,
    sign_certificate: sign_cert,
    sign_key: sign_key
  )

  enc.encrypt(raw_xml)
end

Instance Method Details

#encrypt(raw_xml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/omniauth-suomifi/test/xml_encryptor.rb', line 17

def encrypt(raw_xml)
  doc = XMLSecurity::Document.new(raw_xml)
  assertion = doc.delete_element('//saml2:Assertion')
  return doc.to_s unless assertion

  assertion_signed = Utility.sign_xml_element(assertion.to_s, sign_certificate, sign_key)

  encrypted = doc.root.add_element(
    'saml2:EncryptedAssertion',
    'xmlns:saml2' => 'urn:oasis:names:tc:SAML:2.0:assertion'
  )
  encrypted.add_element(
    REXML::Document.new(encrypted_node_for(assertion_signed))
  )

  doc.to_s
end