Class: Skypager::Protector
- Inherits:
-
Object
- Object
- Skypager::Protector
- Defined in:
- lib/skypager/protector.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#login_handler ⇒ Object
Returns the value of attribute login_handler.
-
#options ⇒ Object
Returns the value of attribute options.
-
#validator ⇒ Object
Returns the value of attribute validator.
Instance Method Summary collapse
- #build_path ⇒ Object
- #call(env) ⇒ Object
- #cookie_options ⇒ Object
-
#initialize(app, options = {}, &validator) ⇒ Protector
constructor
Skypager::Protector.new(options) do |request| # validate request here end.
- #log(message) ⇒ Object
- #login_path ⇒ Object
- #normalize_path(path) ⇒ Object
- #not_found_path ⇒ Object
- #path_extension ⇒ Object
- #redirect_to(path, cookies = {}) ⇒ Object
- #serve_file(file, status = 200) ⇒ Object
- #serve_login_path ⇒ Object
- #serve_not_found_path ⇒ Object
- #validate(request) ⇒ Object
- #whitelist ⇒ Object
Constructor Details
#initialize(app, options = {}, &validator) ⇒ Protector
Skypager::Protector.new(options) do |request|
# validate request here
end
Valid options are:
- build_dir: defaults to ./build
- login_path: defaults to /login/index.html
- not_found_path: defaults to /not-found.html
19 20 21 22 23 24 |
# File 'lib/skypager/protector.rb', line 19 def initialize(app, ={}, &validator) @app = app @options = @validator = validator @logger = .fetch(:logger) { Logger.new(STDOUT) } end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
9 10 11 |
# File 'lib/skypager/protector.rb', line 9 def app @app end |
#login_handler ⇒ Object
Returns the value of attribute login_handler.
9 10 11 |
# File 'lib/skypager/protector.rb', line 9 def login_handler @login_handler end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/skypager/protector.rb', line 9 def @options end |
#validator ⇒ Object
Returns the value of attribute validator.
9 10 11 |
# File 'lib/skypager/protector.rb', line 9 def validator @validator end |
Instance Method Details
#build_path ⇒ Object
71 72 73 74 |
# File 'lib/skypager/protector.rb', line 71 def build_path build_dir = .fetch(:build_dir) { Pathname(Dir.pwd).join('build') } Pathname(build_dir) end |
#call(env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/skypager/protector.rb', line 31 def call(env) request = Rack::Request.new(env) if request.post? log "#{ login_path } #{ request.path } #{ login_handler.respond_to?(:call) }" end if request.post? && request.path == login_path && login_handler.respond_to?(:call) result = login_handler.call(request) return serve_login_path if result == false redirect_path = request.params['_redirect_to'] || "/" return redirect_to(redirect_path, result[:cookies] || {}) end if !validate(request) && request.path != login_path log "== Validation failed: #{ request.path }" return serve_login_path end path = normalize_path(request.path) file = build_path.join(path) if file.exist? serve_file(file) else @app.call(env) end end |
#cookie_options ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/skypager/protector.rb', line 93 def .fetch(:cookie_options) do { path: "/", expires: 180.minutes.from_now, domain: ".lvh.me" } end end |
#log(message) ⇒ Object
26 27 28 29 |
# File 'lib/skypager/protector.rb', line 26 def log @logger && @logger.info() puts if !@logger end |
#login_path ⇒ Object
111 112 113 |
# File 'lib/skypager/protector.rb', line 111 def login_path .fetch(:login_path, "/login.html") end |
#normalize_path(path) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/skypager/protector.rb', line 115 def normalize_path(path) if path == "/" path = "/index.html" elsif !path.to_s.match(/\.\w+/) path.gsub!(/\/$/,'') path + path_extension end path.to_s.gsub(/^\//,'') end |
#not_found_path ⇒ Object
107 108 109 |
# File 'lib/skypager/protector.rb', line 107 def not_found_path .fetch(:not_found_path, "/not-found.html") end |
#path_extension ⇒ Object
126 127 128 129 130 |
# File 'lib/skypager/protector.rb', line 126 def path_extension .fetch(:path_extension ) do [:directory_indexes] ? "/index.html" : ".html" end end |
#redirect_to(path, cookies = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/skypager/protector.rb', line 80 def redirect_to(path, ={}) status, headers, body = [302, {"Content-Type" => "text/html", "Location" => path}, ["302 You've been redirected"]] response = Rack::Response.new(body,status,headers) unless .empty? .each do |k,v| response.(k, .merge(:value=>v)) end end response.finish end |
#serve_file(file, status = 200) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/skypager/protector.rb', line 63 def serve_file(file, status=200) mime_type = Rack::Mime.mime_type(Pathname(file).extname).to_s content = Pathname(file).read.to_s content_length = Rack::Utils.bytesize(content).to_s [status, {"Content-Type" => mime_type,"Content-Length" => content_length}, [content]] end |
#serve_login_path ⇒ Object
103 104 105 |
# File 'lib/skypager/protector.rb', line 103 def serve_login_path redirect_to(login_path) end |
#serve_not_found_path ⇒ Object
76 77 78 |
# File 'lib/skypager/protector.rb', line 76 def serve_not_found_path serve_file(not_found_path, 404) end |
#validate(request) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/skypager/protector.rb', line 132 def validate(request) return true if validator.nil? || !validator.respond_to?(:call) return true if whitelist && request.path.match(whitelist) validator.call({ cookies: request., params: request.params, env: request.env }) end |
#whitelist ⇒ Object
144 145 146 |
# File 'lib/skypager/protector.rb', line 144 def whitelist .fetch(:whitelist) { /^\/(js|css|img|fonts|favico)/ } end |