class Rack::Auth::TravisWebhook

Constants

TRAVIS_CONFIG_URL
VERSION

Public Class Methods

new(app) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 12
def initialize(app)
  @app = app
  @public_key = fetch_public_key
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 17
def call(env)
  return status(405) unless request(env).post?
  return status(401) unless signature(env)
  return status(400) unless payload(env)
  return status(403) unless verify(env)
  @app.call(env)
end

Private Instance Methods

fetch_public_key() click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 47
def fetch_public_key
  config = JSON.parse(open(TRAVIS_CONFIG_URL).read)
  OpenSSL::PKey::RSA.new(
    config['config']['notifications']['webhook']['public_key']
  )
end
payload(env) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 35
def payload(env)
  request(env).POST['payload']
end
request(env) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 27
def request(env)
  @request ||= Rack::Request.new(env)
end
signature(env) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 31
def signature(env)
  env['HTTP_SIGNATURE']
end
status(code) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 54
def status(code)
  [code, {}, []]
end
verify(env) click to toggle source
# File lib/rack/auth/travis_webhook.rb, line 39
def verify(env)
  @public_key.verify(
    OpenSSL::Digest::SHA1.new,
    Base64.decode64(signature(env)),
    payload(env)
  )
end