Class: Rack::Auth::IP

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rack/auth/ip.rb

Overview

# you can use block

# ip is IPAddr instance.
use Rack::Auth::IP do |ip|
  Your::Model::IP.count({ :ip => ip.to_s }) != 0 
end

Defined Under Namespace

Modules: Util

Instance Method Summary collapse

Methods included from Util

detect_ip

Constructor Details

#initialize(app, ip_list = nil, &block) ⇒ IP

Returns a new instance of IP.



30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/auth/ip.rb', line 30

def initialize app, ip_list=nil, &block
  @app = app
  @ip_list = ip_list

  if @ip_list && @ip_list.size > 0
    @ip_list = IPAddrList.new(@ip_list)
  end

  @cond = block
end

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/auth/ip.rb', line 41

def call env
  req_ip = IPAddr.new(detect_ip(env))

  if @ip_list
    if @ip_list.include?(req_ip)
      return @app.call(env)
    end
  else
    if @cond && @cond.call(req_ip)
      return @app.call(env)
    end
  end
  return [403, {'Content-Type' => 'text/plain' }, 'Forbidden' ]
end