Sha256: e0d9e1596dbd8ca9e0bd5de4bf3c2e4968c7bd40dbb4f69ec585d91e1e168f37

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

module Saml
  module Elements
    class EncryptedID
      include ::XmlMapper
      include ::Saml::Base
      include ::Saml::XMLHelpers

      tag 'EncryptedID'

      attr_accessor :xml_node

      register_namespace 'saml', ::Saml::SAML_NAMESPACE
      namespace 'saml'

      has_one :encrypted_data, Xmlenc::Builder::EncryptedData
      has_many :encrypted_keys, Xmlenc::Builder::EncryptedKey, xpath: './'
      has_one :name_id, Saml::Elements::NameId

      validates :encrypted_data, presence: true

      def initialize(*args)
        options = args.extract_options!
        super(*(args << options))
      end

      def encrypt(key_descriptors, key_options = {})
        key_descriptors = Array(key_descriptors)
        encrypted_keys = []

        self.encrypted_data = Xmlenc::Builder::EncryptedData.new
        self.encrypted_data.set_key_retrieval_method Xmlenc::Builder::RetrievalMethod.new(
          uri: "##{key_options[:id]}"
        )
        self.encrypted_data.set_encryption_method(
          algorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc'
        )

        key_descriptors.each do |key_descriptor|
          encrypted_key = self.encrypted_data.encrypt(name_id_xml, key_options)
          encrypted_key.set_encryption_method(
            algorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
            digest_method_algorithm: 'http://www.w3.org/2000/09/xmldsig#sha1'
          )

          encrypted_key.set_key_name(key_descriptor.key_info.key_name)
          encrypted_key.encrypt(key_descriptor.certificate.public_key)

          encrypted_keys << encrypted_key
        end

        self.encrypted_keys = encrypted_keys
        self.name_id = nil
      end

      private

      def name_id_xml
        Nokogiri::XML(name_id.to_xml).root.to_xml
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
libsaml-3.0.5 lib/saml/elements/encrypted_id.rb
libsaml-3.0.4 lib/saml/elements/encrypted_id.rb
libsaml-3.0.3 lib/saml/elements/encrypted_id.rb
libsaml-3.0.2 lib/saml/elements/encrypted_id.rb
libsaml-3.0.1 lib/saml/elements/encrypted_id.rb