Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active August 29, 2015 14:17
Show Gist options
  • Save bethesque/d46041b8743f2a357a05 to your computer and use it in GitHub Desktop.
Save bethesque/d46041b8743f2a357a05 to your computer and use it in GitHub Desktop.
Using Github oauth with the pact broker
# gem "omniauth-github"
# gem "rack-rewrite"
require 'fileutils'
require 'logger'
require 'sequel'
require 'pact_broker'
require 'omniauth/strategies/github'
require 'rack/rewrite'
DATABASE_CREDENTIALS = {database: "pact_broker",
adapter: "postgres",
host: ENV['DBHOST'],
username: ENV['DBUSER'],
password: ENV['DBPASS'],
:encoding => 'utf8'}
authorize = lambda do |env|
auth = env['omniauth.auth']
if auth['credentials'].fetch('token', nil)
env['rack.session'][:user_id] = auth['info']['nickname']
out='/'
else
out='/auth/failure'
end
return [301, {"Content-Type" => "text/html",
"Location" => "https://pact-broker.mydomain#{out}" }, [""]]
end
use Rack::Session::Cookie, :secret => 'secret',
:old_secret => 'old_secret'
use OmniAuth::Builder do
provider :github, ENV['GIT_KEY'], ENV['GIT_SECRET'], {
:client_options => {
:site => 'https://github.com/api/v3',
:authorize_url => 'https://github.com/login/oauth/authorize',
:token_url => 'https://github.com/login/oauth/access_token',
}
}
end
use Rack::Rewrite do
r301 %r{/(.*)}, '/auth/github', :not => %r{/auth/(.*)}, :if => Proc.new {|rack_env|
rack_env['rack.session'][:user_id].nil?
}
end
map '/auth/github/callback' do
run authorize
end
map '/auth/failure' do
run lambda { |env| [200, {"Content-Type" => "text/html"}, ["Nope."]] }
end
app = PactBroker::App.new do | config |
# change these from their default values if desired
# config.log_dir = "./log"
config.auto_migrate_db = true
# config.use_hal_browser = true
config.database_connection = Sequel.connect(DATABASE_CREDENTIALS.merge(:logger => config.logger))
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment