Today I started trying to wire up Warden to my Sinatra app for authentication. I was hoping I could get away with just rolling (and testing) my own authentication and putting a filter in place on the actions that needed it, but since Warden is a part of Devise, the very popular authentication system for Rails, it makes sense that Mike wants me to get familiar with it.

Over the last few days I’d researched how others had implemented Warden in a Sinatra app and I came across some really simple solutions and some really ugly ones. The ugly ones involve sticking your entire app inside of the authentication class, which definitely feels like it violates all sorts of architectural principles.

This morning I tried a few of the solutions (even the ugly ones) and unfortunately none of them worked for me. My first hint that this wasn’t going to be easy-peasy was that none of the posts I came across (or the sinatra_warden gem) had been updated within the past couple of years and scrolling through the comments revealed that a lot of people had given up and rolled their own authentication.

So since there was obviously not going to be an easy button I started doing my homework on one of the big beginner-gaps I knew I’d have to start filling in: Rack & Middleware. I’ve come across both plenty of times in the past eight months but my understanding of them was hazy at best. As long as they did their jobs I didn’t ask questions. Well, now they weren’t doing their jobs so it was time to roll up my sleeves and take a look under the hood. (“Can you help me get this alternator out?… Sure, but why’d you loosen all the bolts on your windshield wiper motor?” True story)

I BARELY scratched the surface of these two things today, but the short definition is that Rack is set up to interact with a web server and handle requests to the server and then spit out a response. Rack Middleware filters the requests to handle certain ones in a specific way. Since Warden is a Rack-based middleware, understanding these two basic concepts went a long way towards helping me understand what I need Warden to do.

For a quick overview I recommend checking out this Railscast on Rack Middleware, and then check out the Warden wiki and the Rack documentation. I expect to be spending quite a bit of time on those pages tomorrow, and once (if) I get Warden up and running I’ll extend my definitions.