Class: Rack::Auth::Simples::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/auth/simples/rules.rb

Instance Method Summary collapse

Constructor Details

#initializeRules

Returns a new instance of Rules.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/auth/simples/rules.rb', line 10

def initialize
	@ips = []
	@triggers = []
    @exceptions = []
    @codes = []

    @opts = {
      :secret => 'SET_VIA_CONFIG',
      :return_url => '/',
      :cookie_name => '_auth_allowed',
      :fail => :forbidden,
      :code_param => 'code'
    }
end

Instance Method Details

#add_exception(url) ⇒ Object



33
34
35
# File 'lib/rack/auth/simples/rules.rb', line 33

def add_exception url
  @exceptions << url
end

#add_ip(ip) ⇒ Object



29
30
31
# File 'lib/rack/auth/simples/rules.rb', line 29

def add_ip ip
	@ips << ip
end

#add_trigger_code(code, url, target) ⇒ Object



45
46
47
# File 'lib/rack/auth/simples/rules.rb', line 45

def add_trigger_code code, url, target
  @codes << {:code => code, :url => url, :target => target}
end

#add_trigger_url(url) ⇒ Object



41
42
43
# File 'lib/rack/auth/simples/rules.rb', line 41

def add_trigger_url url
	@triggers << url
end

#allow_localObject



37
38
39
# File 'lib/rack/auth/simples/rules.rb', line 37

def allow_local
  @ips << '127.0.0.1'
end

#parse(env, app) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rack/auth/simples/rules.rb', line 49

def parse env, app

    if @opts[:fail] == :forbidden
      fail = [403, {'Content-Type' => 'text/plain' }, ['Forbidden'] ]
    else 
      fail = [302, {'Location' => @opts[:fail] }, [] ]
    end

    if env['HTTP_X_FORWARDED_FOR']
      ip = env['HTTP_X_FORWARDED_FOR'].split(',').pop
    else
      ip = env["REMOTE_ADDR"]
    end

    if @exceptions.any?
      @exceptions.each do |ex|
        ex = Regexp.new "^#{Regexp.escape ex}$" if ex.is_a? String
        return app.call(env) if  ex =~ env['PATH_INFO']
      end
    end

    ok = true

    if @ips.any?
      addrs_list = IPAddrList.new(@ips)
      return fail unless addrs_list.include? ip
    end

    
    return app.call(env) if get_cookie(env) == @opts[:secret]


    if @triggers.any?

      if @triggers.include? env['PATH_INFO']

        return set_cookie

      end

      ok = false

    end

    if @codes.any?

      @codes.each do |code|

        if code[:url] == env['PATH_INFO'] and code[:code] == Rack::Request.new(env).params[@opts[:code_param]]
          return set_cookie(code[:target])
        end

      end

      ok = false

    end

    # default to true
    return app.call env if ok

    return fail

end

#set_options(opts) ⇒ Object



25
26
27
# File 'lib/rack/auth/simples/rules.rb', line 25

def set_options opts
  @opts.merge! opts
end