Sha256: 85c590e604698f665142d70241cdb0184235ab79e96ab8fd81966a528a605a94

Contents?: true

Size: 1 KB

Versions: 4

Compression:

Stored size: 1 KB

Contents

module Saml
  module Kit
    class XmlDecryption
      attr_reader :private_key

      def initialize(configuration: Saml::Kit.configuration)
        @private_key = configuration.private_keys(use: :encryption).last
      end

      def decrypt(data)
        encrypted_data = data['EncryptedData']
        symmetric_key = symmetric_key_from(encrypted_data)
        cipher_text = Base64.decode64(encrypted_data["CipherData"]["CipherValue"])
        to_plaintext(cipher_text, symmetric_key, encrypted_data["EncryptionMethod"]['Algorithm'])
      end

      private

      def symmetric_key_from(encrypted_data)
        encrypted_key = encrypted_data['KeyInfo']['EncryptedKey']
        cipher_text = Base64.decode64(encrypted_key['CipherData']['CipherValue'])
        to_plaintext(cipher_text, private_key, encrypted_key["EncryptionMethod"]['Algorithm'])
      end

      def to_plaintext(cipher_text, symmetric_key, algorithm)
        Crypto.decryptor_for(algorithm, symmetric_key).decrypt(cipher_text)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
saml-kit-0.2.8 lib/saml/kit/xml_decryption.rb
saml-kit-0.2.7 lib/saml/kit/xml_decryption.rb
saml-kit-0.2.6 lib/saml/kit/xml_decryption.rb
saml-kit-0.2.5 lib/saml/kit/xml_decryption.rb