Class: Rack::SimpleRackBouncer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/simple_rack_bouncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SimpleRackBouncer

See configure for available options



5
6
7
8
# File 'lib/rack/simple_rack_bouncer.rb', line 5

def initialize(app, options = {})
  @app = app
  configure(options)
end

Instance Attribute Details

#ip_address_checksObject (readonly)

Returns the value of attribute ip_address_checks.



72
73
74
# File 'lib/rack/simple_rack_bouncer.rb', line 72

def ip_address_checks
  @ip_address_checks
end

#user_agent_checksObject (readonly)

Used for testing



71
72
73
# File 'lib/rack/simple_rack_bouncer.rb', line 71

def user_agent_checks
  @user_agent_checks
end

Instance Method Details

#_call(env) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rack/simple_rack_bouncer.rb', line 23

def _call(env)
  if access_denied?(env)
    deny_access
  else
    @app.call(env)
  end
end

#access_denied?(env) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rack/simple_rack_bouncer.rb', line 31

def access_denied?(env)
  ip_denied?(env['REMOTE_ADDR']) || user_agent_denied?(env['HTTP_USER_AGENT'])
end

#call(env) ⇒ Object



19
20
21
# File 'lib/rack/simple_rack_bouncer.rb', line 19

def call(env)
  dup._call(env)
end

#configure(options = {}) ⇒ Object

Accepted options are: :deny_user_agents and :deny_ip_address Both accept a single or an array of String, Regexp or Proc objects If a string is given, IP address will be matched by prefix (i.e. ‘127.0’ will match ‘127.0.0.1’ and ‘127.0.2.1’)



13
14
15
16
17
# File 'lib/rack/simple_rack_bouncer.rb', line 13

def configure(options = {})
  @user_agent_checks = [options[:deny_user_agent]].flatten.compact
  @ip_address_checks = [options[:deny_ip_address]].flatten.compact
  @redirect_url = [options[:redirect]].flatten.compact
end

#ip_denied?(ip) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rack/simple_rack_bouncer.rb', line 35

def ip_denied?(ip)
  ip && @ip_address_checks.any? { |check| match_ip?(check, ip) }  
end

#match_ip?(check, ip) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack/simple_rack_bouncer.rb', line 39

def match_ip?(check, ip)
  case check
  when Regexp
    check =~ ip
  when String
    ip[0, check.size] == check
  when Proc
    check.call(ip)
  else
    false
  end
end

#match_user_agent?(check, ua) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rack/simple_rack_bouncer.rb', line 57

def match_user_agent?(check, ua)
  case check
  when Regexp
    check =~ ua
  when String
    ua == check
  when Proc
    check.call(ua)
  else
    false
  end    
end

#user_agent_denied?(ua) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/rack/simple_rack_bouncer.rb', line 52

def user_agent_denied?(ua)
  ua ||= ''
  @user_agent_checks.any? { |check| match_user_agent?(check, ua) }  
end