Skip to content

Optionally support expiry of JWTs #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/intercom-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def self.company_association=(*)

config_group :jwt do
config_accessor :enabled
config_accessor :expiry
config_accessor :signed_user_fields do |value|
unless value.nil? || (value.kind_of?(Array) && value.all? { |v| v.kind_of?(Symbol) || v.kind_of?(String) })
raise ArgumentError, "jwt.signed_user_fields must be an array of symbols or strings"
Expand Down
12 changes: 7 additions & 5 deletions lib/intercom-rails/script_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScriptTag
include ::ActionView::Helpers::TagHelper

attr_reader :user_details, :company_details, :show_everywhere, :session_duration
attr_accessor :secret, :widget_options, :controller, :nonce, :encrypted_mode_enabled, :encrypted_mode, :jwt_enabled
attr_accessor :secret, :widget_options, :controller, :nonce, :encrypted_mode_enabled, :encrypted_mode, :jwt_enabled, :jwt_expiry

def initialize(options = {})
self.secret = options[:secret] || Config.api_secret
Expand All @@ -27,6 +27,7 @@ def initialize(options = {})
@show_everywhere = options[:show_everywhere]
@session_duration = session_duration_from_config
self.jwt_enabled = options[:jwt_enabled] || Config.jwt.enabled
self.jwt_expiry = options[:jwt_expiry] || Config.jwt.expiry

initial_user_details = if options[:find_current_user_details]
find_current_user_details
Expand Down Expand Up @@ -124,10 +125,11 @@ def intercom_javascript
def generate_jwt
return nil unless user_details[:user_id].present?

payload = {
user_id: user_details[:user_id].to_s,
exp: 24.hours.from_now.to_i
}
payload = { user_id: user_details[:user_id].to_s }

if jwt_expiry
payload[:exp] = jwt_expiry.from_now.to_i
end

if Config.jwt.signed_user_fields.present?
Config.jwt.signed_user_fields.each do |field|
Expand Down
45 changes: 43 additions & 2 deletions spec/script_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def user
expect(script_tag.intercom_settings[:user_hash]).to be_nil
end

it 'generates a valid JWT with correct payload' do
it 'generates a valid JWT with the correct user_id' do
user_id = '1234'
script_tag = ScriptTag.new(
user_details: { user_id: user_id },
Expand All @@ -343,7 +343,6 @@ def user
decoded_payload = JWT.decode(jwt, 'super-secret', true, { algorithm: 'HS256' })[0]

expect(decoded_payload['user_id']).to eq(user_id)
expect(decoded_payload['exp']).to be_within(5).of(24.hours.from_now.to_i)
end

it 'does not generate JWT when user_id is missing' do
Expand Down Expand Up @@ -467,6 +466,48 @@ def user
expect(script_tag.intercom_settings[:name]).to eq('Test User')
end
end

context 'JWT expiry' do
it 'includes expiry when configured' do
IntercomRails.config.jwt.expiry = 12.hours
script_tag = ScriptTag.new(
user_details: { user_id: '1234' },
jwt_enabled: true
)

jwt = script_tag.intercom_settings[:intercom_user_jwt]
decoded_payload = JWT.decode(jwt, 'super-secret', true, { algorithm: 'HS256' })[0]

expect(decoded_payload['exp']).to be_within(5).of(12.hours.from_now.to_i)
end

it 'omits expiry when not configured' do
IntercomRails.config.jwt.expiry = nil
script_tag = ScriptTag.new(
user_details: { user_id: '1234' },
jwt_enabled: true
)

jwt = script_tag.intercom_settings[:intercom_user_jwt]
decoded_payload = JWT.decode(jwt, 'super-secret', true, { algorithm: 'HS256' })[0]

expect(decoded_payload).not_to have_key('exp')
end

it 'allows overriding expiry via options' do
IntercomRails.config.jwt.expiry = 24.hours
script_tag = ScriptTag.new(
user_details: { user_id: '1234' },
jwt_enabled: true,
jwt_expiry: 1.hour
)

jwt = script_tag.intercom_settings[:intercom_user_jwt]
decoded_payload = JWT.decode(jwt, 'super-secret', true, { algorithm: 'HS256' })[0]

expect(decoded_payload['exp']).to be_within(5).of(1.hour.from_now.to_i)
end
end
end

end