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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/auth/simples/rules.rb', line 13

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

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

Instance Method Details

#add_exception(url) ⇒ Object



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

def add_exception url
  @exceptions << url
end

#add_ip(ip) ⇒ Object



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

def add_ip ip
	@ips << ip
end

#add_trigger_code(code, url, target) ⇒ Object



54
55
56
# File 'lib/rack/auth/simples/rules.rb', line 54

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

#add_trigger_url(url) ⇒ Object



50
51
52
# File 'lib/rack/auth/simples/rules.rb', line 50

def add_trigger_url url
	@triggers << url
end

#allow_facebookObject



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

def allow_facebook
  @fb = true
end

#allow_localObject



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

def allow_local
  @ips << '127.0.0.1'
end

#parse(env, app) ⇒ Object



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
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rack/auth/simples/rules.rb', line 58

def parse env, app

    return app.call(env) if @fb && env['HTTP_USER_AGENT'] =~ /facebookexternalhit/

    if @opts[:fail] == :forbidden
      fail = [403, {'Content-Type' => 'text/plain' }, ['Forbidden'] ]
    else 
      fail = [302, {'Content-Type' => '', '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 update_cookie(app.call env) if check_cookie(env)


    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].downcase == Rack::Request.new(env).params[@opts[:code_param]].downcase
          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



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

def set_options opts
  @opts.merge! opts
end