Ruby Webrick HTTP Authentication
Seems to work OK for me if you do it in pretty much the same style e.g.
class Configuration < HTTPServlet::AbstractServlet
def do_GET(req, res)
HTTPAuth.basic_auth(req, res, "My Realm") {|user, pass|
# block should return true if
# authentication token is valid
user == 'user' && pass == 'topsecret'
}
res.body =
"Authenticated OK\n"
end
end
What is the problem you're having?
Rails running on WEBrick with HTTP authentication
Rails has a built in method for adding http basic authentication.
You should also look at something like Authlogic for authentication and user session management.
How to set the default error pages for a basic Webrick server?
Looking at the source code, it looks like httpresponse.rb
has a "hook" called create_error_page
:
if respond_to?(:create_error_page)
create_error_page()
return
end
So, if you add your own Ruby method called create_error_page
in WEBrick::HTTPResponse
, you can set your own markup:
module WEBrick
class HTTPResponse
def create_error_page
@body = ''
@body << <<-_end_of_html_
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>#{HTMLUtils::escape(@reason_phrase)}</TITLE></HEAD>
<BODY>
<H1>#{HTMLUtils::escape(@reason_phrase)}</H1>
<HR>
<P>Custom error page!</P>
</BODY>
</HTML>
_end_of_html_
end
end
end
Note that you have access to variables like @reason_phrase
and ex.code
. In your case, you can use ex.code
(eg: 401
) to set different content if you wish.
Here is a full example that you can run in an irb
console that displays a custom error page (note that it assumes you have a directory called Public
in your file system):
require 'webrick'
module WEBrick
class HTTPResponse
def create_error_page
@body = ''
@body << <<-_end_of_html_
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>#{HTMLUtils::escape(@reason_phrase)}</TITLE></HEAD>
<BODY>
<H1>#{HTMLUtils::escape(@reason_phrase)}</H1>
<HR>
<P>Custom error page!</P>
</BODY>
</HTML>
_end_of_html_
end
end
end
root = File.expand_path '~/Public'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
trap 'INT' do server.shutdown end
server.start
When you go to http://localhost:8000/bogus
(a page that does not exist), you should see the custom error page, like so:
Hope it helps! :-]
Mimic .htaccess or some other type of password protecting with webrick
You can restrict access by using Rack based basic auth
or IP white listing
Basic Auth
Add the following to your config/environments/development.rb
config.middleware.use Rack::Auth::Basic, "Beta Access" do |username, password|
'secret' == password
end
IP White Listing
I found two gems for this purpose:
rack-auth-ip
rack-ip-whitelist
I would use rack-auth-ip
as it has been there for some time. Add the following to your config/environments/development.rb
config.middleware.use Rack::Auth::IP, %w( YourIPAddress )
Now, the instance is accessible only if the originating IP is in the white list.
Ruby Webrick server not able to verify client certificate
This issue was fixed by providing the cacert file as :SSLCACertificateFile
in server options instead of :SSLClientCA
.
server_options = {
:Host => '0.0.0.0',
:Port => 443,
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT | OpenSSL::SSL::VERIFY_PEER,
:SSLVerifyDepth => 4,
:SSLCertificate => OpenSSL::X509::Certificate.new(File.open('/Users/cert.pem').read),
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open('/Users/key.pem').read),
:SSLCACertificateFile => '/Users/cai.cer'
}
How can Webrick based (Non RoR, Sinatra) web application can be severed using Apache and Passenger?
If you want your application WITH Webrick served by Apache you don't need Passenger
use mod_proxy in Apache with ProxyPass* primitives
If you want Apache + Passenger you have to change Webrick against Rack or Passenger compatible, Merb, Rails, etc...
Access WEBrick server from outside
Have you set up port forwarding on your router? What is the make and model of your router?
See there answers for more help:
Setting up the Webrick to serve to internet my rails app
cant do port forwarding ro my webrick server to access it from the net!
Related Topics
Issue with Precision of Ruby Math Operations
Proc Throws Error When Used with Do End
Create and Initialize Instances of a Class with Sequential Names
How to Give a Date a Background Color with Axlsx
Jekyll Custom Theme- Gemspec Bundle Install Error: Unexpected Unary-, Expecting Keyword_Do
Finding Parenthesis via Regular Expression
Check Whether a Variable Is a String in Ruby
Rails Byebug Did Not Stop Application
Utc Time Resets to 2000-01-01 (Ruby). How to Prevent the Time from Resetting
Ruby: Append Text to the 2Nd Line of a File
Error: Missing Rvm Environment File After Doing Rvm Upgrade Command - Passenger 4.0.23