The web application landscape has changed drastically in the past year or two. Where once every site was a silo unto itself and could reasonably expect users to create a unique login and password for each site, it is now a different story. I sigh every time I have to fill out yet another registration form, wishing instead for a simple "Connect with Facebook", "Sign in with Twitter", or "Log in with OpenID". At the same time, services are more interconnected than ever. One of the best ways to increase the popularity and viability of a new service is by piggybacking it onto the existing user bases of apps such as Twitter, Facebook, and Foursquare.
There are lots of authentication solutions out there for Rails. Many of them even have ways to connect to services such as Facebook or Twitter. But as I used these in project after project I noticed an emerging pattern: they all make too many assumptions about how I want to handle authentication in my application. Sure that makes it a quick start for the vanilla use case, but I honestly can't think of a time when I've dropped in an authentication solution and I was good to go. It's time for a change in perspective.
OmniAuth: The Unassuming Authentication Library
Today is the public release of OmniAuth. OmniAuth is a Rack-based authentication system that abstracts away the gritty, difficult parts of external authentication without assuming anything about what you actually want to do with that authentication information.
What does this mean for you? This means that you can make your app authenticate through Twitter, Facebook, LinkedIn, Google Apps, GitHub, Foursquare, and more and then have complete control from there.
Installation
OmniAuth is available as a gem:
gem install omniauth
Diving In
Using OmniAuth is as simple as using any other Rack middleware. Of course, that's because OmniAuth is simply a Rack middleware. No complicated framework-specific configuration, just a collection of middleware to take the pain out of external authentication. Let's say I have a Sinatra app that I want to be able to authenticate via Twitter or Facebook. Here's what's required:
require 'omniauth' use Rack::Session::Cookie # OmniAuth requires sessions. use OmniAuth::Builder do provider :twitter, "CONSUMER_KEY", "CONSUMER_SECRET" provider :facebook, "APP_ID", "APP_SECRET" end
That's it! Now if I want to send my user to authenticate via Twitter, I send them to the URL /auth/twitter
. For Facebook, /auth/facebook
. The user will automatically be redirected to the appropriate site where they will be able to authenticate. Once authentication is complete, they will be redirected back to /auth/providername/callback
and OmniAuth will automatically fill the omniauth.auth
environment key with information about the user, so for my Sinatra client I just need to add:
get '/auth/:provider/callback' do auth = request.env['omniauth.auth'] "Hello, #{auth['user_info']['name']}, you logged in via #{params['provider']}." end
Of course, I could do a lot more than just print out the user's name. I could also:
- Check for an existing user via the
uid
key of the auth hash and log them in if one exists. - Create a user based on the
uid
and information from theuser_info
hash. - If a user is already logged in, associate this new account with the user so that they can log in using either service or post to both services using respective APIs.
The point here is that OmniAuth doesn't assume that you simply want to log a user in. It lets you make that judgment call and gives you all the information you need to do just about anything you need to do.
OmniAuth works right now for a wide variety of providers, and this list will continue to grow. OmniAuth today supports:
- 37signals ID
- Foursquare
- GitHub
- OpenID (meaning Yahoo, Aol, Google, and many more)
- Google Apps (via OpenID)
- CAS (Central Authentication Service)
- LDAP
A Breath of Fresh Auth
OmniAuth has been my worst-kept secret library for some time now. I've been using it as the go-to authentication strategy for new projects big and small for the last few months, and it feels really refreshing to have so much control over authentication without losing the drop-in ease of use. If you need external authentication but have found existing solutions to lack flexibility, please take a look!
OmniAuth is on GitHub with a growing set of documentation on the GitHub wiki and I have also set up a Google Group to handle any questions that might arise.