From 0d56fe0dd32bedb549c06e199cd2acf005d930cb Mon Sep 17 00:00:00 2001 From: bartes Date: Sat, 14 Apr 2018 09:55:07 +0200 Subject: [PATCH] client_id header takes precedence over client_id from cookies --- CHANGELOG.md | 4 +++ lib/castle/configuration.rb | 4 +-- lib/castle/context/default.rb | 2 +- lib/castle/extractors/client_id.rb | 5 ++-- spec/lib/castle/extractors/client_id_spec.rb | 27 +++++++++++++++----- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab2a917..1deb6539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - [#119](github.com/castle/castle-ruby/pull/119) usage of `traits` key is deprecated, use `user_traits` instead +**Enhancements:** + +- [#122](github.com/castle/castle-ruby/pull/122) `X-Castle-Client-Id` takes precedence over `cid` from `cookies` + ## 3.4.2 (2018-02-26) **Features:** diff --git a/lib/castle/configuration.rb b/lib/castle/configuration.rb index 3eac0bd6..5008a8b8 100644 --- a/lib/castle/configuration.rb +++ b/lib/castle/configuration.rb @@ -65,8 +65,8 @@ def respond_to_missing?(method_name, _include_private) /^(\w+)=$/ =~ method_name end - def method_missing(m, *_args) - raise Castle::ConfigurationError, "there is no such a config #{m}" + def method_missing(setting, *_args) + raise Castle::ConfigurationError, "there is no such a config #{setting}" end end end diff --git a/lib/castle/context/default.rb b/lib/castle/context/default.rb index 7a98edd1..65730ec7 100644 --- a/lib/castle/context/default.rb +++ b/lib/castle/context/default.rb @@ -4,7 +4,7 @@ module Castle module Context class Default def initialize(request, cookies = nil) - @client_id = Extractors::ClientId.new(request, cookies || request.cookies).call('__cid') + @client_id = Extractors::ClientId.new(request, cookies || request.cookies).call @headers = Extractors::Headers.new(request).call @request_ip = Extractors::IP.new(request).call end diff --git a/lib/castle/extractors/client_id.rb b/lib/castle/extractors/client_id.rb index 5af6a54d..95e3a5a1 100644 --- a/lib/castle/extractors/client_id.rb +++ b/lib/castle/extractors/client_id.rb @@ -9,9 +9,8 @@ def initialize(request, cookies) @cookies = cookies || {} end - def call(name) - @cookies[name] || - @request.env.fetch('HTTP_X_CASTLE_CLIENT_ID', '') + def call + @request.env['HTTP_X_CASTLE_CLIENT_ID'] || @cookies['__cid'] || '' end end end diff --git a/spec/lib/castle/extractors/client_id_spec.rb b/spec/lib/castle/extractors/client_id_spec.rb index 3e74bc36..7879963f 100644 --- a/spec/lib/castle/extractors/client_id_spec.rb +++ b/spec/lib/castle/extractors/client_id_spec.rb @@ -3,7 +3,8 @@ describe Castle::Extractors::ClientId do subject(:extractor) { described_class.new(request, cookies) } - let(:client_id) { 'abcd' } + let(:client_id_cookie) { 'abcd' } + let(:client_id_header) { 'abcde' } let(:cookies) { request.cookies } let(:request) { Rack::Request.new(env) } let(:env) do @@ -14,12 +15,12 @@ let(:headers) do { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', - 'HTTP_COOKIE' => "__cid=#{client_id};other=efgh" + 'HTTP_COOKIE' => "__cid=#{client_id_cookie};other=efgh" } end it do - expect(extractor.call('__cid')).to eql(client_id) + expect(extractor.call).to eql(client_id_cookie) end end @@ -27,12 +28,12 @@ let(:headers) do { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', - 'HTTP_X_CASTLE_CLIENT_ID' => client_id + 'HTTP_X_CASTLE_CLIENT_ID' => client_id_header } end it 'appends the client_id' do - expect(extractor.call('__cid')).to eql(client_id) + expect(extractor.call).to eql(client_id_header) end end @@ -41,7 +42,21 @@ let(:headers) { {} } it do - expect(extractor.call('__cid')).to eql('') + expect(extractor.call).to eql('') + end + end + + context 'with X-Castle-Client-Id header and cookies client' do + let(:headers) do + { + 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', + 'HTTP_X_CASTLE_CLIENT_ID' => client_id_header, + 'HTTP_COOKIE' => "__cid=#{client_id_cookie};other=efgh" + } + end + + it 'appends the client_id' do + expect(extractor.call).to eql(client_id_header) end end end