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

Commit

Permalink
added new setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Nov 5, 2018
1 parent a00cf4a commit e840c1f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
6 changes: 2 additions & 4 deletions lib/castle/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ def log(level, message)

def track(context, options)
log(:debug, "[Castle] Tracking #{options[:event]}")
castle = ::Castle::Client.new(context)
castle.track(options)
::Castle::Client.new(context).track(options)
rescue Castle::Error => e
log(:warn, "[Castle] Can't send tracking request because #{e} exception")
call_error_handler(e)
end

def authenticate(context, options)
log(:debug, "[Castle] Authenticating #{options[:event]}")
castle = ::Castle::Client.new(context)
castle.authenticate(options)
::Castle::Client.new(context).authenticate(options)
rescue Castle::Error => e
log(:warn, "[Castle] Can't send authenticating request because #{e} exception")
call_error_handler(e)
Expand Down
7 changes: 4 additions & 3 deletions lib/castle/middleware/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Castle
module Middleware
# Configuration object for Middleware
class Configuration
extend SingleForwardable
attr_accessor :options, :events
def_single_delegators :options, :logger, :transport, :error_handler, :api_secret
extend Forwardable
attr_accessor :options, :events, :login
def_delegators :@options, :logger, :transport, :error_handler, :api_secret, :app_id, :deny, :challenge, :logout, :provide_by_id, :provide_by_login_key, :validate_password, :tracker_url

def initialize(options = nil)
self.options = options
Expand All @@ -30,6 +30,7 @@ def setup
def load_config_file
file_config = YAML.load_file(options.file_path)
self.events = file_config['events'] || {}
self.login = file_config['login'] || {}
rescue Errno::ENOENT
Castle::Middleware.log(:error, '[Castle] No config file found')
rescue Psych::SyntaxError
Expand Down
7 changes: 7 additions & 0 deletions lib/castle/middleware/configuration_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ class ConfigurationOptions
%i[
api_secret
app_id
tracker_url
auto_insert_middleware
error_handler
file_path
logger
transport
deny
challenge
logout
provide_by_id
provide_by_login_key
validate_password
].each do |opt|
attr_accessor opt
end
Expand Down
1 change: 0 additions & 1 deletion lib/castle/middleware/event_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def find(conditions)
end

def find_by_rack_request(result, request)
byebug
find(
status: result.first, # Rack status code
method: request.request_method,
Expand Down
5 changes: 5 additions & 0 deletions lib/castle/middleware/request_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ module Middleware
class RequestConfig
attr_reader :user_id
attr_reader :traits
attr_reader :props

def identify(user_id, traits)
@user_id = user_id
@traits = traits
end

def properties(props)
@props = props
end
end
end
end
9 changes: 7 additions & 2 deletions lib/castle/middleware/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def initialize(app)

def call(env)
app_result = app.call(env)
byebug

begin
return app_result unless add_js?(env, app_result[0], app_result[1])
Expand Down Expand Up @@ -46,7 +45,7 @@ def attachment?(headers)
headers['Content-Disposition'].to_s.include?('attachment')
end

def streaming?(env)
def streaming?(headers)
headers['Transfer-Encoding'].to_s == 'chunked'
end

Expand Down Expand Up @@ -101,6 +100,7 @@ def close_old_response(response)
def complete_js_content(env)
commands = [
"\n",
tracker_url_command,
identify_command(env),
secure_command(env),
"\n"
Expand All @@ -109,6 +109,11 @@ def complete_js_content(env)
snippet_cjs_tag + script_tag(commands, env)
end

def tracker_url_command
return unless Castle::Middleware.configuration.tracker_url
"_castle('setTrackerUrl', '#{Castle::Middleware.configuration.tracker_url}');"
end

def identify_command(env)
return unless env['castle'].user_id
"_castle('identify', '#{env['castle'].user_id}');"
Expand Down
70 changes: 63 additions & 7 deletions lib/castle/middleware/tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,91 @@ def initialize(app)
end

def call(env)
byebug
env['castle'] = RequestConfig.new

req = Rack::Request.new(env)

if login?(req)
redirect_result = authentication_verdict(req, env)
return [301, {'Location' => redirect_result}, []] if redirect_result
end

env['castle'].identify(req.session['castle_user_id'], {}) if req.session['castle_user_id']

# [status, headers, body]
app_result = app.call(env)

# Find a matching event from the config
mapping = Middleware.event_mapping.find_by_rack_request(app_result, req)
mapping = Castle::Middleware.event_mapping.find_by_rack_request(app_result, req)

return app_result if mapping.nil?

event_properties = self.class.collect_event_properties(
req.params, mapping.properties
).merge(env['castle'].props || {})

# Send request as configured
Middleware.configuration.transport.(
# Send track request as configured
track(req, env, mapping, event_properties)

app_result
end

private

def authentication_verdict(req, env)
key = req.params.dig(*login_config.dig('authentication', 'key').split('.'))
pass = req.params.dig(*login_config.dig('authentication', 'password').split('.'))

resource = Castle::Middleware.configuration.provide_by_login_key.call(key)
return if resource.nil?

env['castle'].identify(resource.public_send(login_config['user_id']), {})

return unless Castle::Middleware.configuration.validate_password.call(resource, pass)

verdict = authenticate(req, env)

case verdict[:action]
when 'allow'
req.session['castle_user_id'] = env['castle'].user_id
when 'challenge'
redirect_result = Castle::Middleware.configuration.challenge.call(req, resource)
Castle::Middleware.configuration.logout.call(req, env)
redirect_result
when 'deny'
redirect_result = Castle::Middleware.configuration.deny.call(req, resource)
Castle::Middleware.configuration.logout.call(req, env)
redirect_result
end
end

def authenticate(req, env)
Castle::Middleware.authenticate(
Castle::Client.to_context(req),
Castle::Client.to_options(
user_id: env['castle'].user_id,
event: '$login.succeeded'
)
)
end

def track(req, env, mapping, event_properties)
Castle::Middleware.configuration.transport.call(
Castle::Client.to_context(req),
Castle::Client.to_options(
user_id: env['castle'].user_id,
traits: env['castle'].traits,
user_traits: env['castle'].traits,
event: mapping.event,
properties: event_properties
)
)
end

app_result
def login_config
Castle::Middleware.configuration.login
end

def login?(req)
req.path == login_config['path'] && req.request_method == login_config['method'] && req.form_data?
end

class << self
Expand Down

0 comments on commit e840c1f

Please sign in to comment.