-
-
Save nateware/302883 to your computer and use it in GitHub Desktop.
draft of Rack request verifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rack' | |
class Rack | |
class PlayCo | |
class RequestValidator | |
# ==== Parameters | |
# app<Rack::Builder>:: The Rack builder which will use this middleware. | |
# path_opts<Hash>:: Path options used to identify path to validate. | |
# | |
# ==== Options (path_opts) | |
# :http_method<Array, String>:: | |
# 'POST', 'GET', 'DELETE' or 'UPDATE' HTTP verb used against | |
# env['REQUEST_METHOD']. | |
# :format<Array, String>:: | |
# 'xml', 'html', 'json', etc - what to test against | |
# :salt<String>:: | |
# secret salt used to hash the request | |
# | |
# ==== Examples | |
# | |
# (Rails 2.x) in RAILS_ROOT/config.ru: | |
# use Rack::PlayCo::RequestValidator :http_method => ['POST'], | |
# :format => ['xml'] | |
def initialize(app, path_opts) | |
@app = app | |
# TODO validate the path_opts and/or set defaults | |
# Accept either a single string or Array for methods/formats | |
@path_opts = path_opts | |
end | |
def call(env) | |
if specified_request_method?(env) && specified_format?(env) | |
if valid_request?(env) | |
@app.call(env) | |
else | |
# IMPORTANT: Must set content-type to same type we received, | |
# and send back a message detailing the error (HMAC? UUID?) | |
[400, {"Content-Type" => "text/html"}, [post_body]] | |
end | |
end | |
end | |
def specified_request_method?(env) | |
@path_opts[:http_method].include?(env['REQUEST_METHOD']) | |
end | |
def specified_format?(env) | |
@path_opts[:format].include?(env['REQUEST_METHOD']) | |
end | |
def valid_request?(env) | |
salt = @path_opts[:salt] || DEFAULT_SALT | |
# | |
# Need to insert all of the logic from lib/client_verifier.rb here, | |
# specifically the verify_client_hmac() method | |
# | |
post_body = env['rack.input'] ? env['rack.input'].read : nil | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment