Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed May 15, 2015
1 parent c0466f2 commit 6991f95
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
63 changes: 32 additions & 31 deletions lib/slanger/api/request_validation.rb
Expand Up @@ -3,8 +3,7 @@ module Api
class RequestValidation < Struct.new :raw_body, :raw_params, :path_info
def initialize(*args)
super(*args)

validate!
validate!
authenticate!
parse_body!
end
Expand All @@ -13,30 +12,10 @@ def body
@body ||= parse_body!
end

def parse_body!
assert_valid_json!(raw_body)
end

def assert_valid_json!(string)
JSON.parse(string)
rescue JSON::ParserError
raise Slanger::InvalidRequest.new("Invalid request body: #{raw_body}")
end

def authenticate!
# Raises Signature::AuthenticationError if request does not authenticate.
Signature::Request.new('POST', path_info, auth_params).
authenticate { |key| Signature::Token.new key, Slanger::Config.secret }
end

def auth_params
params.except('channel_id', 'app_id')
end

def validate!
determine_valid_socket_id
end

def socket_id
@socket_id ||= determine_valid_socket_id
end
Expand All @@ -51,25 +30,26 @@ def data

private

def validate!
raise InvalidRequest.new "no body" unless raw_body.present?
raise InvalidRequest.new "invalid params" unless raw_params.is_a? Hash
raise InvalidRequest.new "invalid path" unless path_info.is_a? String

determine_valid_socket_id
end

def validate_socket_id!(socket_id)
validate_with_regex!(/\A\d+\.\d+\z/, socket_id, "socket_id")

socket_id
end

def validate_channel_id!(channel_id)
validate_with_regex!(/\A[\w@\-;]+\z/, channel_id, "channel_id")

channel_id
end

def validate_with_regex!(value, regex, name)
def validate_with_regex!(regex, value, name)
raise InvalidRequest, "Invalid #{name} #{value.inspect}" unless value =~ regex
end

def determine_valid_socket_id
return validate_socket_id!(data["socket_id"]) if data["socket_id"]
return validate_socket_id!(params["socket_id"]) if params["socket_id"]
value
end

def validate_raw_params!
Expand All @@ -84,6 +64,27 @@ def validate_raw_params!
restricted
end

def authenticate!
# Raises Signature::AuthenticationError if request does not authenticate.
Signature::Request.new('POST', path_info, auth_params).
authenticate { |key| Signature::Token.new key, Slanger::Config.secret }
end

def parse_body!
assert_valid_json!(raw_body)
end

def assert_valid_json!(string)
JSON.parse(string)
rescue JSON::ParserError
raise Slanger::InvalidRequest.new("Invalid request body: #{raw_body}")
end

def determine_valid_socket_id
return validate_socket_id!(data["socket_id"]) if data["socket_id"]
return validate_socket_id!(params["socket_id"]) if params["socket_id"]
end

def user_params
raw_params.reject{|k,_| %w(splat captures).include?(k)}
end
Expand Down
37 changes: 14 additions & 23 deletions lib/slanger/api/server.rb
Expand Up @@ -11,26 +11,21 @@

module Slanger
module Api

class Server < Sinatra::Base
use Rack::FiberPool
set :raise_errors, lambda { false }
set :show_exceptions, false



# Respond with HTTP 401 Unauthorized if request cannot be authenticated.
error(Signature::AuthenticationError) { |e| halt 401, "401 UNAUTHORIZED\n#{e}" }
error(Slanger::Api::InvalidRequest) { |c| halt 400, "Bad Request\n" }

error(Signature::AuthenticationError) { |e| halt 401, "401 UNAUTHORIZED" }
error(Slanger::Api::InvalidRequest) { |c| halt 400, "400 Bad Request" }

before do
validate_request!
valid_request
end

post '/apps/:app_id/events' do
socket_id = validated_request.socket_id
data = validated_request.data
socket_id = valid_request.socket_id
data = valid_request.data

event = Slanger::Api::Event.new(data["name"], data["data"], socket_id)
EventPublisher.publish(data["channels"], event)
Expand All @@ -40,25 +35,21 @@ class Server < Sinatra::Base
end

post '/apps/:app_id/channels/:channel_id/events' do
params = validated_request.params
params = valid_request.params

event = Event.new(params["name"], validated_request.body, validated_request.socket_id)
EventPublisher.publish(validated_request.data["channels"], event)
event = Event.new(params["name"], valid_request.body, valid_request.socket_id)
EventPublisher.publish(valid_request.data["channels"], event)

status 202
return {}.to_json
end

def validate_request!
validated_request
end

def validated_request
@validated_reqest ||= RequestValidation.new(request_body, params, env["PATH_INFO"])
end

def request_body
@request_body ||= request.body.read.tap{|s| s.force_encoding("utf-8")}
def valid_request
@valid_request ||=
begin
request_body ||= request.body.read.tap{|s| s.force_encoding("utf-8")}
RequestValidation.new(request_body, params, env["PATH_INFO"])
end
end
end
end
Expand Down
15 changes: 14 additions & 1 deletion spec/unit/request_validation_spec.rb
Expand Up @@ -20,8 +20,21 @@
end
end

before do
request = mock("request")
request.expects(:authenticate).times(0..2)
Signature::Request.expects(:new).times(0..2).returns request
end

describe "#socket_id" do
it do
rv = Slanger::Api::RequestValidation.new(body("1234.5678"), {}, "")
expect(rv.socket_id).to eq "1234.5678"
end
end

def validate(socket_id)
Slanger::Api::RequestValidation.new(body(socket_id)).socket_id
Slanger::Api::RequestValidation.new(body(socket_id), {}, "").socket_id
end

def body(socket_id)
Expand Down

0 comments on commit 6991f95

Please sign in to comment.