I want to implement HTTP basic authentication on my staging server, but only for those outside the local network. I have a Rails 3.1 app. In application.rb, I have the following:
class ApplicationController << ActionController::Base
http_basic_authenticate_with :realm => "Staging", :name => "user", :password => "password" if :need_authentication?
private
def need_authentication?
Rails.env == "staging" && request.remote_addr !~ /^192.168.0.\d{1,3}$/
end
end
Here’s the rub: even when the need_authentication? method explicitly returns false, the app still asks me to authenticate, as if it’s completely ignoring the if clause at the end.
So, is there any way to only require authentication under certain conditions?
This is what worked:
‘Staging’ is the name of the realm. This is not required, but can be used for clarification.