Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created April 13, 2011 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottwater/917607 to your computer and use it in GitHub Desktop.
Save scottwater/917607 to your computer and use it in GitHub Desktop.
Quick samples on securing Resque::Server
require 'resque/server'
class SecureResqueServer < Resque::Server
before do
redirect '/' unless some_condition_is_met!
end
end
require 'resque/server'
class SecureResqueServer < Resque::Server
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['admin', 'admin']
end
end
@jcarlos
Copy link

jcarlos commented Jun 8, 2012

An interesting alternative to this is to use an initializer as described in http://asciicasts.com/episodes/271-resque

"We’re not using Devise or any other authentication system in our application so instead we’ll use HTTP Basic Authentication. To do this we’ll create a new initializer in the config/initializers directory called resque_auth.rb."

I have changed a bit the example from ASCIIcasts
/config/initializers/resque_auth.rb:

Resque::Server.use(Rack::Auth::Basic) do |user, password|  
  [user, password] == ["myuser", "secret"]  
end

@donatoaz
Copy link

donatoaz commented Apr 3, 2018

Oddly enough, the railscast alternative did not do it for me. I had to use @scottwater's second solution .

@benr75
Copy link

benr75 commented Mar 27, 2020

I banged my head on this for a while before finding this helpful gist. In Rails 6 I did the following:

config/routes.rb

Rails.application.routes.draw do 
  # YOUR ROUTES HERE
  mount Resque::Server.new, at: "/resque"
end

config/initializers/resque_auth.rb

require 'resque/server'

Resque::Server.use(Rack::Auth::Basic) do |user, password|
  [user, password] == [ENV["RESQUE_HTTP_BASIC_AUTH_USER"], ENV["RESQUE_HTTP_BASIC_AUTH_PASSWORD"]]
end

I don't put the user and password directly in the code, I load those as environment variables from a file I do not check into version control for security.

config/env_config.yml

RESQUE_HTTP_BASIC_AUTH_USER: "YOUR_USER_NAME_HERE"
RESQUE_HTTP_BASIC_AUTH_PASSWORD: "YOUR_PASSWORD_HERE"

config/application.rb

# Add this in the config.before_configuration do block, this is not a complete application.rb
env_file = File.join(Rails.root, 'config', 'env_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
  ENV[key.to_s] = value
end if File.exists?(env_file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment