Sha256: 8a42c298bec3b3d2029d5ad03226e7eca466f02af3025ae0d34292ca1d44e506

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module IntercomRails
  class EncryptedMode
    attr_reader :secret, :initialization_vector, :enabled

    ENCRYPTED_MODE_SETTINGS_WHITELIST = [:app_id, :session_duration, :widget, :custom_launcher_selector, :hide_default_launcher, :alignment, :horizontal_padding, :vertical_padding]

    def initialize(secret, initialization_vector, options)
      @secret = secret
      @initialization_vector = initialization_vector || SecureRandom.random_bytes(12)
      @enabled = options.fetch(:enabled, false)
    end

    def plaintext_part(settings)
      enabled ? settings.slice(*ENCRYPTED_MODE_SETTINGS_WHITELIST) : settings
    end

    def encrypted_javascript(payload)
      enabled ? "window.intercomEncryptedPayload = \"#{encrypt(payload)}\";" : ""
    end

    def encrypt(payload)
      return nil unless enabled
      payload = payload.except(*ENCRYPTED_MODE_SETTINGS_WHITELIST)
      key = Digest::SHA256.digest(secret)
      cipher = OpenSSL::Cipher.new('aes-256-gcm')
      cipher.encrypt
      cipher.key = key
      cipher.iv = initialization_vector
      json = ActiveSupport::JSON.encode(payload).gsub('<', '\u003C')
      encrypted = initialization_vector + cipher.update(json) + cipher.final + cipher.auth_tag
      Base64.encode64(encrypted).gsub("\n", "\\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
intercom-rails-0.4.1 lib/intercom-rails/encrypted_mode.rb
intercom-rails-0.4.0 lib/intercom-rails/encrypted_mode.rb
intercom-rails-0.3.8 lib/intercom-rails/encrypted_mode.rb