class IntercomRails::EncryptedMode

Constants

ENCRYPTED_MODE_SETTINGS_WHITELIST

Attributes

enabled[R]
initialization_vector[R]
secret[R]

Public Class Methods

new(secret, initialization_vector, options) click to toggle source
# File lib/intercom-rails/encrypted_mode.rb, line 7
def initialize(secret, initialization_vector, options)
  @secret = secret
  @initialization_vector = initialization_vector || SecureRandom.random_bytes(12)
  @enabled = options.fetch(:enabled, false)
end

Public Instance Methods

encrypt(payload) click to toggle source
# File lib/intercom-rails/encrypted_mode.rb, line 21
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
encrypted_javascript(payload) click to toggle source
# File lib/intercom-rails/encrypted_mode.rb, line 17
def encrypted_javascript(payload)
  enabled ? "window.intercomEncryptedPayload = \"#{encrypt(payload)}\";" : ""
end
plaintext_part(settings) click to toggle source
# File lib/intercom-rails/encrypted_mode.rb, line 13
def plaintext_part(settings)
  enabled ? settings.slice(*ENCRYPTED_MODE_SETTINGS_WHITELIST) : settings
end