rack-auth-simples 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ rack-auth-simples-0.0.1.gem
@@ -9,9 +9,19 @@ module Rack
9
9
 
10
10
  def initialize
11
11
  @ips = []
12
- @triggers = []
12
+ @triggers = []
13
+
14
+ @opts = {
15
+ :secret => 'SET_VIA_CONFIG',
16
+ :return_url => '/',
17
+ :cookie_name => '_auth_allowed'
18
+ }
13
19
  end
14
20
 
21
+ def set_options opts
22
+ @opts.merge! opts
23
+ end
24
+
15
25
  def add_ip ip
16
26
  @ips << ip
17
27
  end
@@ -24,7 +34,9 @@ module Rack
24
34
  @triggers << url
25
35
  end
26
36
 
27
- def parse_rules env
37
+ def parse env, app
38
+
39
+ fail = [403, {'Content-Type' => 'text/plain' }, ['Forbidden'] ]
28
40
 
29
41
  if env['HTTP_X_FORWARDED_FOR']
30
42
  ip = env['HTTP_X_FORWARDED_FOR'].split(',').pop
@@ -35,21 +47,29 @@ module Rack
35
47
 
36
48
  if @ips.any?
37
49
  addrs_list = IPAddrList.new(@ips)
38
- return false unless addrs_list.include? ip
50
+ return fail unless addrs_list.include? ip
39
51
  end
40
52
 
41
53
  if @triggers.any?
42
54
 
43
- # check cookie, return true if present
55
+ cookie = Rack::Request.new(env).cookies[@opts[:cookie_name]]
56
+
57
+ return app.call(env) if cookie == @opts[:secret]
58
+
59
+ if @triggers.include? env['PATH_INFO']
60
+
61
+ headers = {'Location' => @opts[:return_url]}
62
+ Rack::Utils.set_cookie_header!(headers, @opts[:cookie_name], {:value => @opts[:secret], :path => "/"})
63
+ return [302, headers, ['']]
44
64
 
45
- # check trigger url, if match set cookie and return true
65
+ end
46
66
 
47
- # return false
67
+ return fail
48
68
 
49
69
  end
50
70
 
51
71
  # default to true
52
- return true
72
+ return app.call env
53
73
 
54
74
  end
55
75
 
@@ -13,11 +13,7 @@ module Rack
13
13
 
14
14
  def call env
15
15
 
16
- if @rules.parse_rules
17
- @app.call env
18
- else
19
- return [403, {'Content-Type' => 'text/plain' }, ['Forbidden'] ]
20
- end
16
+ @rules.parse env, @app
21
17
 
22
18
  end
23
19
  end
@@ -1,7 +1,7 @@
1
1
  module Rack
2
2
  module Auth
3
- module Simples
4
- VERSION = "0.0.1"
3
+ class Simples
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-auth-simples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -34,6 +34,7 @@ executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
+ - .gitignore
37
38
  - Gemfile
38
39
  - lib/rack-auth-simples/version.rb
39
40
  - lib/rack/auth/simples.rb