diff --git a/CHANGELOG.md b/CHANGELOG.md index 9120c576..aac3eada 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 9.1.1 + +**Housekeeping:** + +- Slim down the default request context to `headers`, `ip` and `library`; the remaining data is already available via `headers`. +- Remove the internal `Castle::ClientId::Extract` service and the now-unused `cookies` plumbing in `Castle::Context::GetDefault`. + ## 9.1.0 **Enhancements:** diff --git a/Gemfile.lock b/Gemfile.lock index 8bb3dce1..af387f79 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - castle-rb (9.1.0) + castle-rb (9.1.1) base64 (~> 0.2) GEM diff --git a/lib/castle.rb b/lib/castle.rb index df3056f4..c399fd81 100644 --- a/lib/castle.rb +++ b/lib/castle.rb @@ -78,7 +78,6 @@ castle/headers/format castle/headers/extract castle/secure_mode - castle/client_id/extract castle/ips/extract castle/core/get_connection castle/core/process_response diff --git a/lib/castle/client_id/extract.rb b/lib/castle/client_id/extract.rb deleted file mode 100644 index 7d374d70..00000000 --- a/lib/castle/client_id/extract.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module Castle - module ClientId - # used for extraction of cookies and headers from the request - class Extract - # @param headers [Hash] - # @param cookies [NilClass|Hash] - def initialize(headers, cookies) - @headers = headers - @cookies = cookies || {} - end - - # extracts client id - # @return [String] - def call - @headers['X-Castle-Client-Id'] || @cookies['__cid'] || '' - end - end - end -end diff --git a/lib/castle/context/get_default.rb b/lib/castle/context/get_default.rb index 6f5aeb6c..4885544c 100644 --- a/lib/castle/context/get_default.rb +++ b/lib/castle/context/get_default.rb @@ -3,50 +3,21 @@ module Castle module Context class GetDefault - def initialize(request, cookies = nil) + def initialize(request) @pre_headers = Castle::Headers::Filter.new(request).call - @cookies = cookies || request.cookies - @request = request end def call - { - client_id: client_id, - active: true, - headers: headers, - ip: ip, - library: { - name: 'castle-rb', - version: Castle::VERSION - } - }.tap do |result| - result[:locale] = locale if locale - result[:user_agent] = user_agent if user_agent - end + { headers: headers, ip: ip, library: { name: 'castle-rb', version: Castle::VERSION } } end private - # @return [String] - def locale - @pre_headers['Accept-Language'] - end - - # @return [String] - def user_agent - @pre_headers['User-Agent'] - end - # @return [String] def ip Castle::IPs::Extract.new(@pre_headers).call end - # @return [String] - def client_id - Castle::ClientId::Extract.new(@pre_headers, @cookies).call - end - # formatted and filtered headers # @return [Hash] def headers diff --git a/lib/castle/context/prepare.rb b/lib/castle/context/prepare.rb index b0ffc041..1e86d0c2 100644 --- a/lib/castle/context/prepare.rb +++ b/lib/castle/context/prepare.rb @@ -9,7 +9,7 @@ class << self # @param options [Hash] # @return [Hash] def call(request, options = {}) - default_context = Castle::Context::GetDefault.new(request, options[:cookies]).call + default_context = Castle::Context::GetDefault.new(request).call Castle::Context::Merge.call(default_context, options[:context]) end end diff --git a/lib/castle/version.rb b/lib/castle/version.rb index 015dc9d9..16ae6068 100644 --- a/lib/castle/version.rb +++ b/lib/castle/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Castle - VERSION = '9.1.0' + VERSION = '9.1.1' end diff --git a/spec/lib/castle/client_id/extract_spec.rb b/spec/lib/castle/client_id/extract_spec.rb deleted file mode 100644 index 94365818..00000000 --- a/spec/lib/castle/client_id/extract_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Castle::ClientId::Extract do - subject(:extractor) { described_class.new(formatted_headers, cookies) } - - let(:formatted_headers) { Castle::Headers::Filter.new(request).call } - let(:client_id_cookie) { 'abcd' } - let(:client_id_header) { 'abcde' } - let(:cookies) { request.cookies } - let(:request) { Rack::Request.new(env) } - let(:env) { Rack::MockRequest.env_for('/', headers) } - - context 'with client_id' do - let(:headers) { { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', 'HTTP_COOKIE' => "__cid=#{client_id_cookie};other=efgh" } } - - it { expect(extractor.call).to eql(client_id_cookie) } - end - - context 'with X-Castle-Client-Id header' do - let(:headers) { { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', 'HTTP_X_CASTLE_CLIENT_ID' => client_id_header } } - - it 'appends the client_id' do - expect(extractor.call).to eql(client_id_header) - end - end - - context 'when cookies undefined' do - let(:cookies) { nil } - let(:headers) { {} } - - it { expect(extractor.call).to eql('') } - 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 diff --git a/spec/lib/castle/client_spec.rb b/spec/lib/castle/client_spec.rb index 3248df1f..81ae2e39 100644 --- a/spec/lib/castle/client_spec.rb +++ b/spec/lib/castle/client_spec.rb @@ -20,19 +20,7 @@ let(:client_with_no_timestamp) { described_class.new(context: request_to_context) } let(:headers) { { 'Content-Length': '0', 'User-Agent': ua, 'X-Forwarded-For': ip.to_s, Cookie: true } } - let(:context) do - { - client_id: 'abcd', - active: true, - user_agent: ua, - headers: headers, - ip: ip, - library: { - name: 'castle-rb', - version: '2.2.0' - } - } - end + let(:context) { { headers: headers, ip: ip, library: { name: 'castle-rb', version: '2.2.0' } } } let(:time_now) { Time.now } let(:time_auto) { time_now.utc.iso8601(3) } diff --git a/spec/lib/castle/context/get_default_spec.rb b/spec/lib/castle/context/get_default_spec.rb index f25b9891..496ad13c 100644 --- a/spec/lib/castle/context/get_default_spec.rb +++ b/spec/lib/castle/context/get_default_spec.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true RSpec.describe Castle::Context::GetDefault do - subject { described_class.new(request, nil) } + subject { described_class.new(request) } let(:ip) { '1.2.3.4' } - let(:client_id) { 'abcd' } let(:env) do Rack::MockRequest.env_for( @@ -12,7 +11,7 @@ 'HTTP_X_FORWARDED_FOR' => ip, 'HTTP_ACCEPT_LANGUAGE' => 'en', 'HTTP_USER_AGENT' => 'test', - 'HTTP_COOKIE' => "__cid=#{client_id};other=efgh", + 'HTTP_COOKIE' => 'other=efgh', 'HTTP_CONTENT_LENGTH' => '0' ) end @@ -31,11 +30,8 @@ before { stub_const('Castle::VERSION', version) } - it { expect(default_context[:active]).to be(true) } it { expect(default_context[:headers]).to eql(result_headers) } it { expect(default_context[:ip]).to eql(ip) } - it { expect(default_context[:client_id]).to eql(client_id) } it { expect(default_context[:library][:name]).to eql('castle-rb') } it { expect(default_context[:library][:version]).to eql(version) } - it { expect(default_context[:user_agent]).to eql('test') } end diff --git a/spec/lib/castle/context/prepare_spec.rb b/spec/lib/castle/context/prepare_spec.rb index 6c2201a6..1651ae4a 100644 --- a/spec/lib/castle/context/prepare_spec.rb +++ b/spec/lib/castle/context/prepare_spec.rb @@ -14,19 +14,7 @@ ) end let(:request) { Rack::Request.new(env) } - let(:context) do - { - client_id: 'abcd', - active: true, - user_agent: ua, - headers: headers, - ip: ip, - library: { - name: 'castle-rb', - version: '6.0.0' - } - } - end + let(:context) { { headers: headers, ip: ip, library: { name: 'castle-rb', version: '6.0.0' } } } let(:headers) { { 'Content-Length': '0', 'User-Agent': ua, 'X-Forwarded-For': ip.to_s, Cookie: true } } @@ -35,8 +23,6 @@ describe '#call' do subject(:generated) { described_class.call(request) } - context 'when active true' do - it { is_expected.to eql(context) } - end + it { is_expected.to eql(context) } end end diff --git a/spec/lib/castle/payload/prepare_spec.rb b/spec/lib/castle/payload/prepare_spec.rb index 2f171ab2..27eeee66 100644 --- a/spec/lib/castle/payload/prepare_spec.rb +++ b/spec/lib/castle/payload/prepare_spec.rb @@ -16,19 +16,7 @@ let(:request) { Rack::Request.new(env) } let(:headers) { { 'Content-Length': '0', 'User-Agent': ua, 'X-Forwarded-For': ip.to_s, Cookie: true } } - let(:context) do - { - client_id: 'abcd', - active: true, - user_agent: ua, - headers: headers, - ip: ip, - library: { - name: 'castle-rb', - version: '2.2.0' - } - } - end + let(:context) { { headers: headers, ip: ip, library: { name: 'castle-rb', version: '2.2.0' } } } let(:time_now) { Time.now } let(:time_formatted) { time_now.utc.iso8601(3) } @@ -45,8 +33,6 @@ describe '#call' do subject(:generated) { described_class.call(payload_options, request) } - context 'when active true' do - it { is_expected.to eql(result) } - end + it { is_expected.to eql(result) } end end