Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from castle/added_query_params_to_path_check
Browse files Browse the repository at this point in the history
added possibility to set query params conditions
  • Loading branch information
bartes committed Mar 7, 2019
2 parents 22985f4 + a31ee31 commit 26ef0a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/castle/middleware/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def load_config_file
options.identify = (options.identify || {}).merge(file_config['identify'] || {})
options.api_options = (options.api_options || {}).merge(file_config['api_options'] || {})
options.user_traits = (options.user_traits || {}).merge(file_config['user_traits'] || {})
rescue Errno::ENOENT => e
rescue Errno::ENOENT
log(:error, '[Castle] No config file found')
rescue Psych::SyntaxError
Caste::Middleware::ConfigError.new('[Castle] Invalid YAML in config file')
Expand Down
9 changes: 6 additions & 3 deletions lib/castle/middleware/event_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Castle
class Middleware
# Map a request path to a Castle event name
class EventMapper
Mapping = Struct.new(:event, :method, :path, :redirect_url,
Mapping = Struct.new(:event, :method, :path, :redirect_url, :query,
:status, :properties, :user_traits_from_params, :authenticate,
:challenge, :referer, :quitting)

Expand All @@ -23,6 +23,7 @@ def add(event, conditions)
conditions[:method],
conditions[:path],
conditions[:redirect_url],
conditions[:query],
conditions[:status],
conditions.fetch(:properties, {}),
conditions.fetch(:user_traits_from_params, {}),
Expand All @@ -47,6 +48,7 @@ def find_by_rack_request(status, path, headers, request, authenticate = false)
method: request.request_method,
path: path,
authenticate: authenticate,
query: request.query_string,
referer: request.referer.to_s,
redirect_url: headers ? headers['Location'] : nil
)
Expand All @@ -64,8 +66,8 @@ def self.build(config)
end

def self.match?(mapping, conditions)
status, mtd, path, auth, referer, redirect_url = conditions.values_at(
:status, :method, :path, :authenticate, :referer, :redirect_url
status, mtd, path, auth, referer, redirect_url, query = conditions.values_at(
:status, :method, :path, :authenticate, :referer, :redirect_url, :query
)

return false if path.nil?
Expand All @@ -74,6 +76,7 @@ def self.match?(mapping, conditions)
match_prop?(mapping.method, mtd) &&
match_prop?(mapping.redirect_url, redirect_url) &&
match_prop?(mapping.path, path) &&
match_prop?(mapping.query, query) &&
(mapping.authenticate == auth) &&
match_prop?(mapping.referer, referer)
end
Expand Down
53 changes: 35 additions & 18 deletions spec/castle/middleware/event_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,6 @@
}
end

let(:array_config) do
{
'$login.failed' => [
{
'method' => 'POST',
'path' => '/sign_in',
'status' => '302'
},
{
'method' => 'POST',
'path' => '/login',
'status' => '400'
}
]
}
end

let(:broken_path) { "signup/DirMHlj0'))" }

describe '::build' do
Expand Down Expand Up @@ -73,6 +56,23 @@
it { is_expected.to be_nil }
end

context 'when query params included' do
let(:query_param) { 'token=123' }

before { valid_config['$login.failed']['query'] = /token=/ }

context 'when query is used' do
let(:conditions) do
{
query: 'z1=3&token=123&z=2', status: '302',
path: '/sign_in', method: 'POST', authenticate: false
}
end

it { is_expected.to be_an_instance_of(described_class::Mapping) }
end
end

context 'when referer is used' do
let(:referer) { '/test' }

Expand Down Expand Up @@ -124,7 +124,24 @@
end

describe '#find with array config' do
subject { described_class.build(array_config).find(conditions).first }
let(:valid_config) do
{
'$login.failed' => [
{
'method' => 'POST',
'path' => '/sign_in',
'status' => '302'
},
{
'method' => 'POST',
'path' => '/login',
'status' => '400'
}
]
}
end

subject { described_class.build(valid_config).find(conditions).first }

context 'when matching first item' do
let(:conditions) { { status: '302', path: '/sign_in', method: 'POST', authenticate: false } }
Expand Down

0 comments on commit 26ef0a0

Please sign in to comment.