Skip to content

Commit 4a282e7

Browse files
committed
Add support for HTTP Basic Authentication
1 parent a0b9e6c commit 4a282e7

10 files changed

+102
-24
lines changed

.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RESQUE_WEB_HTTP_BASIC_AUTH_USER=user
2+
RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=secret

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ test/dummy/log/*.log
2323
test/dummy/tmp/
2424
test/dummy/.sass-cache
2525
*.gem
26+
vendor/ruby

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ gem 'resque', :git => 'https://github.com/resque/resque.git', :branch => "1-x-st
77
gem 'sqlite3', :platforms => :ruby
88
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
99

10+
group :development, :test do
11+
gem "dotenv-rails"
12+
end
13+
1014
group :test do
1115
gem 'minitest-spec-rails'
1216
gem 'coveralls', :require => false

Gemfile.lock

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GIT
1313
PATH
1414
remote: .
1515
specs:
16-
resque-web (0.0.2)
16+
resque-web (0.0.3)
1717
coffee-rails
1818
jquery-rails
1919
resque
@@ -55,9 +55,9 @@ GEM
5555
atomic (1.1.13)
5656
atomic (1.1.13-java)
5757
builder (3.1.4)
58-
coffee-rails (4.0.0)
58+
coffee-rails (4.0.1)
5959
coffee-script (>= 2.2.0)
60-
railties (>= 4.0.0.beta, < 5.0)
60+
railties (>= 4.0.0, < 5.0)
6161
coffee-script (2.2.0)
6262
coffee-script-source
6363
execjs
@@ -69,6 +69,9 @@ GEM
6969
rest-client
7070
simplecov (>= 0.7)
7171
thor
72+
dotenv (0.9.0)
73+
dotenv-rails (0.9.0)
74+
dotenv (= 0.9.0)
7275
erubis (2.7.0)
7376
execjs (2.0.1)
7477
hike (1.2.3)
@@ -118,9 +121,9 @@ GEM
118121
ref (1.0.5)
119122
rest-client (1.6.7)
120123
mime-types (>= 1.16)
121-
sass (3.2.10)
122-
sass-rails (4.0.0)
123-
railties (>= 4.0.0.beta, < 5.0)
124+
sass (3.2.12)
125+
sass-rails (4.0.1)
126+
railties (>= 4.0.0, < 5.0)
124127
sass (>= 3.1.10)
125128
sprockets-rails (~> 2.0.0)
126129
simplecov (0.7.1)
@@ -175,6 +178,7 @@ PLATFORMS
175178
DEPENDENCIES
176179
activerecord-jdbcsqlite3-adapter
177180
coveralls
181+
dotenv-rails
178182
libv8 (= 3.11.8.13)
179183
minitest-spec-rails
180184
mocha

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ Another example of a route constraint using the current user when using Devise o
5656
```ruby
5757
# config/routes.rb
5858
resque_web_constraint = lambda do |request|
59-
current_user = request.env['warden'].user
60-
61-
current_user.present? && current_user.respond_to?(:is_admin?) && current_user.is_admin?
59+
current_user = request.env['warden'].user
60+
current_user.present? && current_user.respond_to?(:is_admin?) && current_user.is_admin?
6261
end
6362

6463
constraints resque_web_constraint do
@@ -67,6 +66,10 @@ end
6766

6867
```
6968

69+
### HTTP Basic Authentication
70+
71+
HTTP Basic Authentication is supported out of the box. Simply set the environment variables `RESQUE_WEB_HTTP_BASIC_AUTH_USER` and `RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD` to turn it on. If you're using Resque with Heroku run `heroku config:set RESQUE_WEB_HTTP_BASIC_AUTH_USER=user RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=secret` to get ResqueWeb secured.
72+
7073
## Screenshot
7174

7275
![Screenshot](http://i.imgur.com/LkNgl.png)

app/controllers/resque_web/application_controller.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ResqueWeb
22
class ApplicationController < ActionController::Base
33
protect_from_forgery
4-
before_filter :set_subtabs
4+
before_filter :set_subtabs, :authorize
55

66
def self.subtabs(*tab_names)
77
return defined?(@subtabs) ? @subtabs : [] if tab_names.empty?
@@ -11,5 +11,13 @@ def self.subtabs(*tab_names)
1111
def set_subtabs(subtabs = self.class.subtabs)
1212
@subtabs = subtabs
1313
end
14+
15+
private
16+
17+
def authorize
18+
if ENV["RESQUE_WEB_HTTP_BASIC_AUTH_USER"] && ENV["RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD"]
19+
authenticate_or_request_with_http_basic {|u, p| u == ENV["RESQUE_WEB_HTTP_BASIC_AUTH_USER"] && p == ENV["RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD"] }
20+
end
21+
end
1422
end
1523
end

test/functional/failures_controller_test.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,45 @@
22

33
module ResqueWeb
44
class FailuresControllerTest < ActionController::TestCase
5+
include ControllerTestHelpers
6+
57
setup do
68
@routes = Engine.routes
79
end
810

911
describe "GET /failures" do
1012
it "renders the index page" do
11-
get :index
13+
visit(:index)
1214
assert_template :index
1315
end
1416
end
1517

1618
describe "DELETE /failures/:id" do
1719
it "deletes the failure" do
1820
Resque::Failure.expects(:remove).with('123')
19-
delete :destroy, :id=>123
21+
visit(:destroy, {:id => 123}, :method => :delete)
2022
assert_redirected_to failures_path
2123
end
2224
end
2325

2426
describe "DELETE /failures/destroy_all" do
2527
it "deletes all failures" do
2628
Resque::Failure.expects(:clear).with('failed')
27-
delete :destroy_all
29+
visit(:destroy_all, nil, :method => :delete)
2830
assert_redirected_to failures_path
2931
end
3032
end
3133

3234
describe "PUT /failures/:id/retry" do
3335
it "retries the failure and remove the original message" do
3436
Resque::Failure.expects(:requeue_and_remove).with('123')
35-
put :retry,:id=>123
37+
visit(:retry, {:id => 123}, :method => :put)
3638
assert_redirected_to failures_path
3739
end
3840
it "retries should work also in case of pre 2.0 Resque" do
3941
Resque::Failure.expects(:requeue).with('123')
4042
Resque::Failure.expects(:remove).with('123')
41-
put :retry,:id=>123
43+
visit(:retry, {:id => 123}, :method => :put)
4244
assert_redirected_to failures_path
4345
end
4446
end
@@ -49,7 +51,7 @@ class FailuresControllerTest < ActionController::TestCase
4951
Resque::Failure.stubs(:requeue_and_remove).returns(true)
5052
Resque::Failure.expects(:requeue_and_remove).with(0)
5153
Resque::Failure.expects(:requeue_and_remove).with(1)
52-
put :retry_all
54+
visit(:retry_all, nil, :method => :put)
5355
assert_redirected_to failures_path
5456
end
5557
it "retries all failures should also work case of pre 2.0 Resque" do
@@ -59,15 +61,14 @@ class FailuresControllerTest < ActionController::TestCase
5961
Resque::Failure.expects(:remove).with(0)
6062
Resque::Failure.expects(:requeue).with(1)
6163
Resque::Failure.expects(:remove).with(1)
62-
put :retry_all
64+
visit(:retry_all, nil, :method => :put)
6365
assert_redirected_to failures_path
6466
end
6567
it "retries all failures using requeue_queue if queue specified" do
6668
Resque::Failure.expects(:requeue_queue).with('myqueue')
67-
put :retry_all,:queue=>"myqueue"
69+
visit(:retry_all, {:queue=>"myqueue"}, :method => :put)
6870
assert_redirected_to failures_path(:queue=>'myqueue')
6971
end
7072
end
71-
7273
end
7374
end
+34-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
require 'test_helper'
22

3-
class OverviewControllerTest < ActionController::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
3+
module ResqueWeb
4+
5+
class OverviewControllerTest < ActionController::TestCase
6+
include ControllerTestHelpers
7+
8+
setup do
9+
@routes = Engine.routes
10+
end
11+
12+
describe "GET /" do
13+
describe "when HTTP Basic Authentication is enabled" do
14+
describe "and the currect username and password are supplied " do
15+
it "should grant me access" do
16+
visit(:show)
17+
assert_response :ok
18+
end
19+
end
20+
21+
describe "and the username and password are not supplied" do
22+
it "should deny me access" do
23+
visit(:show, {}, :auth => false)
24+
assert_response :unauthorized
25+
end
26+
end
27+
end
28+
29+
describe "when HTTP Basic Authentication is disabled" do
30+
it "should grant me access" do
31+
visit(:show, {}, :auth => :disabled)
32+
assert_response :ok
33+
end
34+
end
35+
end
36+
end
737
end

test/functional/queues_controller_test.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
module ResqueWeb
44
class QueuesControllerTest < ActionController::TestCase
5+
include ControllerTestHelpers
6+
57
setup do
68
@routes = Engine.routes
79
end
10+
811
let(:queue_name) { 'example_queue' }
912

1013
it "deletes queues" do
1114
Resque.push(queue_name, :class => 'ExampleJob')
1215
Resque.queues.include?(queue_name).must_equal true
1316

14-
delete :destroy, :id => queue_name
17+
visit(:destroy, {:id => queue_name}, :method => :delete)
1518
assert_redirected_to queues_path
1619

1720
Resque.queues.include?(queue_name).wont_equal true
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module ControllerTestHelpers
2+
def visit(action, params = {}, options = {})
3+
method = options.delete(:method) || :get
4+
5+
user = ENV["RESQUE_WEB_HTTP_BASIC_AUTH_USER"]
6+
password = ENV["RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD"]
7+
8+
if options[:auth] == :disabled
9+
ENV["RESQUE_WEB_HTTP_BASIC_AUTH_USER"] = nil
10+
ENV["RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD"] = nil
11+
else
12+
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(
13+
options[:user] || user, options[:password] || password
14+
) unless options[:auth] == false
15+
end
16+
17+
send(method, action, params)
18+
19+
ENV["RESQUE_WEB_HTTP_BASIC_AUTH_USER"] = user
20+
ENV["RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD"] = password
21+
end
22+
end

0 commit comments

Comments
 (0)