Skip to content

Require domain parameter for Intercom cookie deletion #366

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
30 changes: 17 additions & 13 deletions lib/intercom-rails/shutdown_helper.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
module IntercomRails
module ShutdownHelper
# This helper allows to erase cookies when a user log out of an application
# It is recommanded to call this function every time a user log out of your application
# This helper allows to erase cookies when a user logs out of an application
# It is recommended to call this function every time a user logs out of your application
# Do not use before a redirect_to because it will not clear the cookies on a redirection
def self.intercom_shutdown_helper(cookies, domain = nil)
#
# @param cookies [ActionDispatch::Cookies::CookieJar] The cookies object
# @param domain [String] The domain used for the Intercom cookies (required).
# Specify the same domain that Intercom uses for its cookies
# (typically your main domain with a leading dot, e.g. ".yourdomain.com").
def self.intercom_shutdown_helper(cookies, domain)
nil_session = { value: nil, expires: 1.day.ago }
nil_session = nil_session.merge(domain: domain) unless domain.nil? || domain == 'localhost'

if (cookies.is_a?(ActionDispatch::Cookies::CookieJar))
cookies["intercom-session-#{IntercomRails.config.app_id}"] = nil_session
else
controller = cookies
Rails.logger.info("Warning: IntercomRails::ShutdownHelper.intercom_shutdown_helper takes an instance of ActionDispatch::Cookies::CookieJar as an argument since v0.2.34. Passing a controller is depreciated. See https://github.com/intercom/intercom-rails#shutdown for more details.")
controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", nil_session)

unless domain == 'localhost'
dotted_domain = domain.start_with?('.') ? domain : ".#{domain}"
nil_session = nil_session.merge(domain: dotted_domain)
end
rescue

cookies["intercom-session-#{IntercomRails.config.app_id}"] = nil_session
rescue => e
Rails.logger.error("Error in intercom_shutdown_helper: #{e.message}") if defined?(Rails) && Rails.logger
end

def self.prepare_intercom_shutdown(session)
session[:perform_intercom_shutdown] = true
end

def self.intercom_shutdown(session, cookies, domain = nil)
def self.intercom_shutdown(session, cookies, domain)
if session[:perform_intercom_shutdown]
session.delete(:perform_intercom_shutdown)
intercom_shutdown_helper(cookies, domain)
Expand Down
66 changes: 36 additions & 30 deletions spec/shutdown_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@

describe TestController, type: :controller do
include IntercomRails::ShutdownHelper
context 'without domain' do
it 'clears response intercom-session-{app_id} cookie' do
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies)
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
it 'creates session[:perform_intercom_shutdown] var' do
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
expect(session[:perform_intercom_shutdown]).to eq true
end
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
session[:perform_intercom_shutdown] = true
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies)
expect(session[:perform_intercom_shutdown]).to eq nil
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
it 'clears response intercom-session-{app_id} cookie' do
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'intercom.com')
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
context 'with domain' do
it 'clears response intercom-session-{app_id} cookie' do
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'intercom.com')
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
it 'creates session[:perform_intercom_shutdown] var' do
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
expect(session[:perform_intercom_shutdown]).to eq true
end
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
session[:perform_intercom_shutdown] = true
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, 'intercom.com')
expect(session[:perform_intercom_shutdown]).to eq nil
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
it 'creates session[:perform_intercom_shutdown] var' do
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
expect(session[:perform_intercom_shutdown]).to eq true
end
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
session[:perform_intercom_shutdown] = true
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, 'intercom.com')
expect(session[:perform_intercom_shutdown]).to eq nil
expect(cookies.has_key?('intercom-session-abc123')).to eq true
end
it 'adds a leading dot to the domain if not present' do
allow(cookies).to receive(:[]=)
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'intercom.com')
expect(cookies).to have_received(:[]=).with(
"intercom-session-#{IntercomRails.config.app_id}",
hash_including(domain: '.intercom.com')
)
end
it 'keeps the domain as is if it already has a leading dot' do
allow(cookies).to receive(:[]=)
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, '.intercom.com')
expect(cookies).to have_received(:[]=).with(
"intercom-session-#{IntercomRails.config.app_id}",
hash_including(domain: '.intercom.com')
)
end
it 'handles localhost domain specially' do
allow(cookies).to receive(:[]=)
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'localhost')
expect(cookies).to have_received(:[]=).with(
"intercom-session-#{IntercomRails.config.app_id}",
hash_not_including(domain: anything)
)
end
end