module Sinatra::GithubWebhooks

Constants

VERSION

Public Instance Methods

github_event() click to toggle source
# File lib/sinatra/github_webhooks.rb, line 11
def github_event
  request.env['HTTP_X_GITHUB_EVENT']
end
payload() click to toggle source
# File lib/sinatra/github_webhooks.rb, line 15
def payload
  ::JSON.parse(payload_body)
end

Private Instance Methods

payload_body() click to toggle source
# File lib/sinatra/github_webhooks.rb, line 21
def payload_body
  request.body.rewind
  payload_body = request.body.read
  verify_signature(payload_body)
  payload_body
end
verify_signature(payload_body) click to toggle source

Taken from developer.github.com/webhooks/securing/

# File lib/sinatra/github_webhooks.rb, line 29
def verify_signature(payload_body)
  unless settings.respond_to?(:github_webhook_secret)
    logger.warn 'No :github_webhook_secret setting found, skipping signature verification'
    return
  end
  signature = Rack::GithubWebhooks::Signature.new(
    settings.github_webhook_secret,
    request.env['HTTP_X_HUB_SIGNATURE'],
    payload_body
  )
  return halt 500, "Signatures didn't match!" unless signature.valid?
end