intercom-rails 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2fc85ba00358eada296192f07e67ba84f37424ba7a5d8ee9ea5f83e5a0bc60b
4
- data.tar.gz: e3f4bfc29ba15e72c9f19c3c363abeb587ff6668637740edee063c69a329d648
3
+ metadata.gz: 274c2855076beb153ba6dcf5ecdc639bcac1a2515bfe74a7042b84df61eab363
4
+ data.tar.gz: 59e0d09f3dca2b46db7e6e3829d39a89f75eef845c1608f690e87bad41505f0f
5
5
  SHA512:
6
- metadata.gz: eb3a484b71432a961158d13ee67bc43e867d74e4ecfad7d81b58607e7f09aeacb6f9e002df29b3028ac3e71717cece13a2251a0d7a5636ba67264c59465a5167
7
- data.tar.gz: 689566c0e89a88620c018c41e8c49ff23e5aeedc32c180f0684e94d27b2112ba0ce13d5c691a89fa7db0a492352962e41be6e831a8855536fac5c6cfb557d782
6
+ metadata.gz: eebe74df506580a99a1fbe68857ab6c86c140e1db905c4cc96307aa0baa984b2d5efcf852eff43baaf7e6edb4b0937c2340a26b56f602339014478bf92e5c4bd
7
+ data.tar.gz: f3d98e2f4fa9083ba632faa4852028656e20c4b4c14c931dc3caa6dea429957efd9f0402b84be390dd2a97ee9bf0d002622206f455835277844d13f69e5b8aff
data/README.md CHANGED
@@ -69,6 +69,53 @@ It is possible to enable Identity Verification for the Intercom Messenger and yo
69
69
  ```
70
70
  **Note: This example is just for the sake of simplicity, you should never include this secret in source control. Instead, you should use the Rails [secret config](http://guides.rubyonrails.org/4_1_release_notes.html#config-secrets-yml) feature.**
71
71
 
72
+ ### JWT Authentication
73
+ You can enable JWT authentication for enhanced security with the Intercom Messenger. This feature uses JSON Web Tokens (JWTs) to authenticate users instead of the traditional user_hash method. To enable JWT authentication, add the following to your `config/initializers/intercom.rb`:
74
+
75
+ ```ruby
76
+ config.jwt.enabled = true
77
+ ```
78
+
79
+ #### JWT Expiry
80
+ You can set an expiry time for JWTs. This determines how long the token remains valid:
81
+
82
+ ```ruby
83
+ config.jwt.expiry = 12.hours # Token expires after 12 hours
84
+ ```
85
+
86
+ If no expiry is set, the JWT will not include an expiration claim.
87
+
88
+ #### Signed User Fields
89
+ You can specify which user fields should be included in the JWT payload and removed from the client-side settings for enhanced security:
90
+
91
+ ```ruby
92
+ config.jwt.signed_user_fields = [:email, :name, :plan, :team_id]
93
+ ```
94
+
95
+ With this configuration, these fields will be:
96
+ - Included in the signed JWT payload
97
+ - Removed from the client-side `intercomSettings` object
98
+ - Still available to Intercom through the secure JWT
99
+
100
+ #### Per-Request JWT Configuration
101
+ You can also configure JWT settings on a per-request basis using the `intercom_script_tag` helper:
102
+
103
+ ```erb
104
+ <%= intercom_script_tag({
105
+ :user_id => current_user.id,
106
+ :email => current_user.email
107
+ }, {
108
+ :jwt_enabled => true,
109
+ :jwt_expiry => 1.hour
110
+ }) %>
111
+ ```
112
+
113
+ **Important Notes:**
114
+ - JWT authentication requires an `api_secret` to be configured
115
+ - JWT is only generated when a `user_id` is present
116
+ - When JWT is enabled, the `user_id` is removed from client-side settings and only included in the secure JWT
117
+ - Other configured signed fields are also removed from client-side settings when JWT is used
118
+
72
119
  ### Shutdown
73
120
  We make use of first-party cookies so that we can identify your users the next time they open your messenger. When people share devices with someone else, they might be able to see the most recently logged in user’s conversation history until the cookie expires. Because of this, it’s very important to properly shutdown Intercom when a user’s session on your app ends (either manually or due to an automated logout).
74
121
 
@@ -342,7 +389,7 @@ CSP support for automatic insertion exposes two namespaces that can be defined b
342
389
  - String CoreExtensions::IntercomRails::AutoInclude.csp_nonce_hook(controller)
343
390
  - nil CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook(controller, SHA-256 whitelist entry)
344
391
 
345
- For instance, a CSP nonce can be inserted using the [Twitter Secure Headers](https://github.com/twitter/secureheaders) gem with the following code:
392
+ For instance, a CSP nonce can be inserted using the [Github Secure Headers](https://github.com/github/secure_headers) gem with the following code:
346
393
  ```ruby
347
394
  module CoreExtensions
348
395
  module IntercomRails
@@ -143,6 +143,16 @@ module IntercomRails
143
143
  end
144
144
  end
145
145
 
146
+ config_group :jwt do
147
+ config_accessor :enabled
148
+ config_accessor :expiry
149
+ config_accessor :signed_user_fields do |value|
150
+ unless value.nil? || (value.kind_of?(Array) && value.all? { |v| v.kind_of?(Symbol) || v.kind_of?(String) })
151
+ raise ArgumentError, "jwt.signed_user_fields must be an array of symbols or strings"
152
+ end
153
+ end
154
+ end
155
+
146
156
  end
147
157
 
148
158
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'active_support/all'
4
4
  require 'action_view'
5
+ require 'jwt'
5
6
 
6
7
  module IntercomRails
7
8
 
@@ -17,7 +18,7 @@ module IntercomRails
17
18
  include ::ActionView::Helpers::TagHelper
18
19
 
19
20
  attr_reader :user_details, :company_details, :show_everywhere, :session_duration
20
- attr_accessor :secret, :widget_options, :controller, :nonce, :encrypted_mode_enabled, :encrypted_mode
21
+ attr_accessor :secret, :widget_options, :controller, :nonce, :encrypted_mode_enabled, :encrypted_mode, :jwt_enabled, :jwt_expiry
21
22
 
22
23
  def initialize(options = {})
23
24
  self.secret = options[:secret] || Config.api_secret
@@ -25,14 +26,22 @@ module IntercomRails
25
26
  self.controller = options[:controller]
26
27
  @show_everywhere = options[:show_everywhere]
27
28
  @session_duration = session_duration_from_config
28
- self.user_details = options[:find_current_user_details] ? find_current_user_details : options[:user_details]
29
+ self.jwt_enabled = options[:jwt_enabled] || Config.jwt.enabled
30
+ self.jwt_expiry = options[:jwt_expiry] || Config.jwt.expiry
31
+
32
+ initial_user_details = if options[:find_current_user_details]
33
+ find_current_user_details
34
+ else
35
+ options[:user_details] || {}
36
+ end
37
+
38
+ lead_attributes = find_lead_attributes
39
+
40
+ self.user_details = initial_user_details.merge(lead_attributes)
29
41
 
30
42
  self.encrypted_mode_enabled = options[:encrypted_mode] || Config.encrypted_mode
31
43
  self.encrypted_mode = IntercomRails::EncryptedMode.new(secret, options[:initialization_vector], {:enabled => encrypted_mode_enabled})
32
44
 
33
- # Request specific custom data for non-signed up users base on lead_attributes
34
- self.user_details = self.user_details.merge(find_lead_attributes)
35
-
36
45
  self.company_details = if options[:find_current_company_details]
37
46
  find_current_company_details
38
47
  elsif options[:user_details]
@@ -113,12 +122,43 @@ module IntercomRails
113
122
  "window.intercomSettings = #{plaintext_javascript};#{intercom_encrypted_payload_javascript}(function(){var w=window;var ic=w.Intercom;if(typeof ic===\"function\"){ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='#{Config.library_url || "https://widget.intercom.io/widget/#{j app_id}"}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()"
114
123
  end
115
124
 
125
+ def generate_jwt
126
+ return nil unless user_details[:user_id].present?
127
+
128
+ payload = { user_id: user_details[:user_id].to_s }
129
+
130
+ if jwt_expiry
131
+ payload[:exp] = jwt_expiry.from_now.to_i
132
+ end
133
+
134
+ if Config.jwt.signed_user_fields.present?
135
+ Config.jwt.signed_user_fields.each do |field|
136
+ field = field.to_sym
137
+ payload[field] = user_details[field].to_s if user_details[field].present?
138
+ end
139
+ end
140
+
141
+ JWT.encode(payload, secret, 'HS256')
142
+ end
143
+
116
144
  def user_details=(user_details)
117
145
  @user_details = DateHelper.convert_dates_to_unix_timestamps(user_details || {})
118
146
  @user_details = @user_details.with_indifferent_access.tap do |u|
119
147
  [:email, :name, :user_id].each { |k| u.delete(k) if u[k].nil? }
120
148
 
121
- u[:user_hash] ||= user_hash if secret.present? && (u[:user_id] || u[:email]).present?
149
+ if secret.present?
150
+ if jwt_enabled && u[:user_id].present?
151
+ u[:intercom_user_jwt] ||= generate_jwt
152
+
153
+ u.delete(:user_id)
154
+ Config.jwt.signed_user_fields&.each do |field|
155
+ u.delete(field.to_sym)
156
+ end
157
+ elsif (u[:user_id] || u[:email]).present?
158
+ u[:user_hash] ||= user_hash
159
+ end
160
+ end
161
+
122
162
  u[:app_id] ||= app_id
123
163
  end
124
164
  end
@@ -1,3 +1,3 @@
1
1
  module IntercomRails
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intercom-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben McRedmond
8
8
  - Ciaran Lee
9
9
  - Darragh Curran
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2024-04-08 00:00:00.000000000 Z
12
+ date: 1980-01-02 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activesupport
@@ -26,6 +25,20 @@ dependencies:
26
25
  - - ">"
27
26
  - !ruby/object:Gem::Version
28
27
  version: '4.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: jwt
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
29
42
  - !ruby/object:Gem::Dependency
30
43
  name: rake
31
44
  requirement: !ruby/object:Gem::Requirement
@@ -102,28 +115,14 @@ dependencies:
102
115
  requirements:
103
116
  - - "~>"
104
117
  - !ruby/object:Gem::Version
105
- version: '2.0'
106
- type: :development
107
- prerelease: false
108
- version_requirements: !ruby/object:Gem::Requirement
109
- requirements:
110
- - - "~>"
111
- - !ruby/object:Gem::Version
112
- version: '2.0'
113
- - !ruby/object:Gem::Dependency
114
- name: thin
115
- requirement: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - "~>"
118
- - !ruby/object:Gem::Version
119
- version: 1.8.0
118
+ version: '3.0'
120
119
  type: :development
121
120
  prerelease: false
122
121
  version_requirements: !ruby/object:Gem::Requirement
123
122
  requirements:
124
123
  - - "~>"
125
124
  - !ruby/object:Gem::Version
126
- version: 1.8.0
125
+ version: '3.0'
127
126
  - !ruby/object:Gem::Dependency
128
127
  name: tzinfo
129
128
  requirement: !ruby/object:Gem::Requirement
@@ -187,7 +186,6 @@ homepage: http://www.intercom.io
187
186
  licenses:
188
187
  - MIT
189
188
  metadata: {}
190
- post_install_message:
191
189
  rdoc_options: []
192
190
  require_paths:
193
191
  - lib
@@ -202,8 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
200
  - !ruby/object:Gem::Version
203
201
  version: '0'
204
202
  requirements: []
205
- rubygems_version: 3.4.10
206
- signing_key:
203
+ rubygems_version: 4.0.3
207
204
  specification_version: 4
208
205
  summary: Rails helper for emitting javascript script tags for Intercom
209
206
  test_files: []