Skip to content

Instantly share code, notes, and snippets.

@pda
Created January 23, 2017 03:52
Show Gist options
  • Save pda/c47a8daf64629075ac934db5c259a635 to your computer and use it in GitHub Desktop.
Save pda/c47a8daf64629075ac934db5c259a635 to your computer and use it in GitHub Desktop.
Sidekiq::Web v4.2 (no longer on Sinatra) with GitHub OAuth
require "sidekiq"
require "sidekiq/web"
require "warden/github"
Sidekiq.configure_client do |config|
config.redis = {size: 1}
end
# https://gist.github.com/badosu/5a6bb42cc0c0a7ae6e9b01313c61a5b9
class SidekiqGithubChecker
def self.registered(app)
app.helpers do
def warden; env["warden"] end
def github_organization_authenticate!(name)
unless warden.user.organization_member?(name)
halt [401, {}, ["You don't have access to organization #{name}"]]
end
end
end
app.before do
next if current_path == "unauthenticated"
warden.authenticate!
github_organization_authenticate! ENV["GITHUB_ORG"]
end
app.get("/unauthenticated") { [403, {}, [warden.message || ""]] }
app.get "/auth/github/callback" do
if params["error"]
redirect "/unauthenticated"
else
warden.authenticate!
redirect root_path
end
end
end
end
Sidekiq::Web.register SidekiqGithubChecker
Sidekiq::Web.use Warden::Manager do |config|
config.failure_app = Sidekiq::Web
config.default_strategies :github
config.scope_defaults :default, config: {
client_id: ENV["GITHUB_CLIENT_ID"],
client_secret: ENV["GITHUB_CLIENT_SECRET"],
redirect_uri: "/auth/github/callback"
}
config.serialize_from_session { |key| Warden::GitHub::Verifier.load(key) }
config.serialize_into_session { |user| Warden::GitHub::Verifier.dump(user) }
end
run Sidekiq::Web
source "https://rubygems.org"
# ruby web server; run Sidekiq::Web with e.g. `puma --port=80`
# https://github.com/puma/puma
gem "puma"
# provides Sidekiq::Web monitoring UI
# https://github.com/mperham/sidekiq/wiki/Monitoring
gem "sidekiq", "~> 4.2"
# warden strategy for github oauth
# https://github.com/atmos/warden-github
gem "warden-github"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment