skypager 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c43ca74ddfecb4de61127287f5f14c6e2ca18145
4
- data.tar.gz: 29eb227714e18fd39294ae82940a8e927cee8496
3
+ metadata.gz: 45efc54b758d0ce0ce85af412020a929f897e585
4
+ data.tar.gz: 28cb1676bf497bdd2dad35ce7688fcadec7c6d24
5
5
  SHA512:
6
- metadata.gz: 96058ca42cfef710a4a0d7939319465f60a138fe328becdbae11b93741c54190c479c642597a0de5f668a800c6cb23a7fe0b822feddeeafd79916ec35e2d81ac
7
- data.tar.gz: 7a973e4722f40ea57cd93634720e6d8a78838c2576f962004f7ddd9570d9e23589534d24493c12dbeae91fd3abca22824bf01bf59de1633e16bce538fa45f827
6
+ metadata.gz: 6d8204f6fa7073aa4471f346b11bda329ce0403dffc5f3b23c0beb956bf3d423a2f287f80374c4093538af8fce40b4d67801bcd6eb314f61734b248b524bff9e
7
+ data.tar.gz: 8493e3e83b102294562c3c703ad49ec08d2f8c09e80e2d847b4d632d794d0e748927877994e70394d1b67ac4d722165cae6d62e9738bc4fab88ab84ef00ef12e
@@ -2,9 +2,11 @@
2
2
  # cookies, and optionally validate their values.
3
3
  #
4
4
  # It should either show a login page, or the requested path
5
+ require 'logger'
6
+
5
7
  module Skypager
6
8
  class Protector
7
- attr_accessor :validator, :options, :app
9
+ attr_accessor :validator, :options, :app, :login_handler
8
10
 
9
11
  # Skypager::Protector.new(options) do |request|
10
12
  # # validate request here
@@ -18,13 +20,38 @@ module Skypager
18
20
  @app = app
19
21
  @options = options
20
22
  @validator = validator
23
+ @logger = options.fetch(:logger) { Logger.new(STDOUT) }
24
+ end
25
+
26
+ def log message
27
+ @logger && @logger.info(message)
28
+ puts message if !@logger
21
29
  end
22
30
 
23
31
  def call(env)
24
32
  request = Rack::Request.new(env)
25
33
 
26
- if !validate(request)
27
- serve_login_path
34
+ log "Received a request: #{ request.env['REQUEST_METHOD'] } #{ request.path }"
35
+
36
+ if request.post?
37
+ log "== Received a post"
38
+ log "#{ login_path } #{ request.path } #{ login_handler.respond_to?(:call) }"
39
+ end
40
+
41
+ if request.post? && request.path == login_path && login_handler.respond_to?(:call)
42
+ log "Handling login"
43
+
44
+ result = login_handler.call(request)
45
+ return serve_login_path if result == false
46
+
47
+ redirect_path = request.params['_redirect_to'] || "/"
48
+
49
+ return redirect_to(redirect_path, result[:cookies] || {})
50
+ end
51
+
52
+ if !validate(request) && request.path != login_path
53
+ log "== Validation failed: #{ request.path }"
54
+ return serve_login_path
28
55
  end
29
56
 
30
57
  path = normalize_path(request.path)
@@ -51,13 +78,42 @@ module Skypager
51
78
  end
52
79
 
53
80
  def serve_not_found_path
54
- not_found_path = options.fetch(:not_found_path, "/not-found.html")
55
81
  serve_file(not_found_path, 404)
56
82
  end
57
83
 
84
+ def redirect_to(path, cookies={})
85
+ status, headers, body = [302, {"Content-Type" => "text/html", "Location" => path}, ["302 You've been redirected"]]
86
+ response = Rack::Response.new(body,status,headers)
87
+
88
+ unless cookies.empty?
89
+ cookies.each do |k,v|
90
+ response.set_cookie(k, cookie_options.merge(:value=>v))
91
+ end
92
+ end
93
+
94
+ response.finish
95
+ end
96
+
97
+ def cookie_options
98
+ options.fetch(:cookie_options) do
99
+ {
100
+ path: "/",
101
+ expires: 180.minutes.from_now,
102
+ domain: ".lvh.me"
103
+ }
104
+ end
105
+ end
106
+
58
107
  def serve_login_path
59
- login_path = options.fetch(:login_path, "/login.html")
60
- [302, {"Content-Type" => "text/html", "Location" => login_path}, ["302 You've been redirected"]]
108
+ redirect_to(login_path)
109
+ end
110
+
111
+ def not_found_path
112
+ options.fetch(:not_found_path, "/not-found.html")
113
+ end
114
+
115
+ def login_path
116
+ options.fetch(:login_path, "/login.html")
61
117
  end
62
118
 
63
119
  def normalize_path(path)
@@ -76,14 +132,22 @@ module Skypager
76
132
  options[:directory_indexes] ? "/index.html" : ".html"
77
133
  end
78
134
  end
135
+
79
136
  def validate(request)
80
137
  return true if validator.nil? || !validator.respond_to?(:call)
81
138
 
139
+ return true if whitelist && request.path.match(whitelist)
140
+
82
141
  validator.call({
83
142
  cookies: request.cookies,
84
143
  params: request.params,
85
144
  env: request.env
86
145
  })
87
146
  end
147
+
148
+ def whitelist
149
+ options.fetch(:whitelist) { /^\/(js|css|img|fonts|favico)/ }
150
+ end
88
151
  end
152
+
89
153
  end
@@ -1,3 +1,3 @@
1
1
  module Skypager
2
- VERSION = "2.1.4"
2
+ VERSION = "2.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skypager
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.4
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Soeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-23 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie