Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP basic auth for Rack::Static app on Heroku

I have a simple Rack app hosted on Heroku. config.ru:

use Rack::Static, 
  :urls => ["/stylesheets", "/images", "/javascripts"],
  :root => "public"

run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}

How can I add HTTP Basic Auth to this? Bonus points if it only works in the production environment.

Thanks

like image 645
user94154 Avatar asked Aug 12 '11 21:08

user94154


2 Answers

If you want to also protect images, stylesheets and javascripts behind basic auth, you need to put Rack::Auth::Basic first:

use Rack::Auth::Basic, "Restricted Area" do |username, password|
  [username, password] == ['admin', 'admin']
end

use Rack::Static, 
  :urls => ["/stylesheets", "/images", "/javascripts"],
  :root => "public"

run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}
like image 135
Titas Avatar answered Sep 28 '22 09:09

Titas


use Rack::Static, 
  :urls => ["/stylesheets", "/images", "/javascripts"],
  :root => "public"

#SOLUTION:
use Rack::Auth::Basic, "Restricted Area" do |username, password|
  [username, password] == ['admin', 'admin']
end

run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}
like image 30
user94154 Avatar answered Sep 28 '22 08:09

user94154