Skip to content

Commit

Permalink
Merge pull request #122 from castle/client_id_priority
Browse files Browse the repository at this point in the history
client_id header takes precedence over client_id from cookies
  • Loading branch information
nijikon authored Apr 18, 2018
2 parents 32c7380 + 0d56fe0 commit 135d472
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
4 changes: 2 additions & 2 deletions lib/castle/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/castle/context/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/castle/extractors/client_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions spec/lib/castle/extractors/client_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,25 +15,25 @@
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

context 'with X-Castle-Client-Id header' do
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

Expand All @@ -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

0 comments on commit 135d472

Please sign in to comment.