Basic Authentication for Camping
This week I wrote a small web tool in Camping but when I wanted to secure it a little bit I noticed that there was no default way to do HTTP basic authentication. So I wrote some code to do just that. Actually I wrote a lot of documentation and a little bit of code.
With just some downloading and three simple steps you can add basic authentication to you application.
Download the basic_authentication.rb file.
require 'basic_authentication'- Mixin the BasicAuth module:
module Blog include Camping::BasicAuth end - Define how you want to authenticate users:
module Blog def authenticate(u, p) [u,p] == ['admin','secret'] end module_function :authenticate end
And you’re ready to start not letting people into you application. If anyone has a better idea on how and where to define the authenticate method, please drop me a line.
You can use this code under the same restrictions as the Camping framework. Now let’s just hope Why doesn’t sue me for using the Camping module (:
Update: Thijs found a bug in the header handling. The link above has been updated. I will try to give this code a home somewhere and some proper version information when I have some time.
Comments
Add your comment
In order to fight spam on this blog, posting comments from a browser without javascript is currently not supported.
Subscribe
evan 182 days later: (delete | show email)
Using it now, months later. Saved me some time. ¶
wilig 248 days later: (delete)
Thanks very much, this saved me some head scratching. ¶
wasnotrice 274 days later: (delete)
Thanks for sharing this. Perfect for my small private app. ¶
Tammer Saleh 314 days later: (delete | show email)
I heard about this plugin through the ruby on rails podcast. It sounds very useful, but I was much more intrigued by the camping server (and associated rails plugin) you developed it for that monitors your rails applications and collects their runtime exceptions.
Any chance you're group would be willing to release this to the public? It would really help out the rails community. ¶
Tammer Saleh 314 days later: (delete | show email)
...and I just totally spelled "your" wrong. :( ¶
Aredridel 354 days later: (delete)
More succinctly, lose the module_function call and <code>def self.authenticate(u,p)</code> instead. ¶
Manfred Stienstra 354 days later: (delete)
I thought of that too, the code in this post is over a year old. I'm currently using an authentication class within my app module because things got a little bit more complicated (: ¶
Tiago Bastos 392 days later: (delete | show email)
require 'base64' at firt line ;)
Very nice implementation!!! Thanks ¶
Manfred Stienstra 392 days later: (delete)
You don't have to require 'base64' because Camping already requires it to parse multipart mime encoded stuff. ¶
Alexey 757 days later: (delete)
Thanks!!! But needed BasicAuth Class for Ruby CGI! ¶