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

Commit

Permalink
add request validation to API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed May 14, 2015
1 parent 6011ff7 commit 9aa8872
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/slanger/api_server.rb
Expand Up @@ -16,22 +16,24 @@ class ApiServer < Sinatra::Base
set :show_exceptions, false

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

post '/apps/:app_id/events' do
authenticate

# Event and channel data are now serialized in the JSON data
# So, extract and use it
data = JSON.parse(request.body.read.tap{ |s| s.force_encoding('utf-8')})
rv = RequestValidation.new(request.body.read)
socket_id = rv.socket_id
data = rv.data

# Send event to each channel
data["channels"].each { |channel| publish(channel, data['name'], data['data'], data['socket_id']) }
data["channels"].each { |channel| publish(channel, data['name'], data['data'], socket_id) }

status 202
return {}.to_json
end


post '/apps/:app_id/channels/:channel_id/events' do
authenticate

Expand Down

0 comments on commit 9aa8872

Please sign in to comment.