Class: IntercomRails::EncryptedMode
- Inherits:
-
Object
- Object
- IntercomRails::EncryptedMode
- Defined in:
- lib/intercom-rails/encrypted_mode.rb
Constant Summary collapse
- ENCRYPTED_MODE_SETTINGS_WHITELIST =
[:app_id, :session_duration, :widget, :custom_launcher_selector, :hide_default_launcher, :alignment, :horizontal_padding, :vertical_padding]
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#initialization_vector ⇒ Object
readonly
Returns the value of attribute initialization_vector.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #encrypt(payload) ⇒ Object
- #encrypted_javascript(payload) ⇒ Object
-
#initialize(secret, initialization_vector, options) ⇒ EncryptedMode
constructor
A new instance of EncryptedMode.
- #plaintext_part(settings) ⇒ Object
Constructor Details
#initialize(secret, initialization_vector, options) ⇒ EncryptedMode
Returns a new instance of EncryptedMode.
7 8 9 10 11 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 7 def initialize(secret, initialization_vector, ) @secret = secret @initialization_vector = initialization_vector || SecureRandom.random_bytes(12) @enabled = .fetch(:enabled, false) end |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
3 4 5 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 3 def enabled @enabled end |
#initialization_vector ⇒ Object (readonly)
Returns the value of attribute initialization_vector.
3 4 5 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 3 def initialization_vector @initialization_vector end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
3 4 5 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 3 def secret @secret end |
Instance Method Details
#encrypt(payload) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# 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) ⇒ Object
17 18 19 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 17 def encrypted_javascript(payload) enabled ? "window.intercomEncryptedPayload = \"#{encrypt(payload)}\";" : "" end |
#plaintext_part(settings) ⇒ Object
13 14 15 |
# File 'lib/intercom-rails/encrypted_mode.rb', line 13 def plaintext_part(settings) enabled ? settings.slice(*ENCRYPTED_MODE_SETTINGS_WHITELIST) : settings end |