It’s good times. Between amazing frameworks like Ruby on Rails and cloud application platforms like Heroku, lots of heavy lifting has been abstracted away and the friction for bringing your product to market has been seriously reduced. The phrase ‘standing on the shoulders of giants’ sounds more and more applicable as each year passes.
To make things even sweeter, Google comes along and creates the Google Apps Marketplace. The short and easy: Google has millions of customers using Google Apps. You can write applications that integrate with various Google Apps services, and can take advantage of their single sign-on infrastructure. Your app can appear on the Google Apps nav bar, allowing them to switch to your product just as easily as they switch over to Google Docs or Calendar. It looks to be a market begging for lots of attention.
Okay. That’s out the way. Now how hard is it to write an app to integrate with Google Apps? Turns out to be a lot easier than I thought. I spent two hours yesterday morning and came up with a reasonable example. You can find the live version of the app here, and grab the full source via GitHub.
The problem to solve was simple: how do I share code snippets with coworkers? I want a private Pastebin or Gist service, but I don’t want to install anything on my own servers. Enter Styled Bits.
The app layout is very simple: I have three main models: Account, User, and Post. Account represents an entire Google Apps domain, User represents a member of said domain, and Post is a code snippet.
There are two major controllers, Main and Posts. Main handles user login and Posts displays the code snippets.
All of this is pretty standard stuff, the only thing that stands out is how the Main controller actually handles authentication. If you take a peak at the index method and related view, you will see that we display a form asking for your Google Apps domain. You can see it live by visiting the front page of Styled Bits.
After taking your domain name, we redirect you to the login method, where we do something like this:
def login
url = "https://www.google.com/accounts/o8/site-xrds?hd=" + params[:domain]
authenticate_with_open_id(
url,
{ :required => ["http://axschema.org/contact/email", :email]})
do |result, identity_url, registration|
if result.successful?
# Succesfully logged in
email = get_email(registration)
account = Account.find_or_create_by_domain(params[:domain])
user = account.users.find_or_create_by_email(email)
session[:user_id] = user.id
redirect_to(posts_path)
else
# Failed to login
flash[:notice] = "Could not log you in."
render :index
end
end
end
As you can see, the simple method authenticate_with_open_id takes care of everything. Don’t you love blocks? Don’t you? That’s all it takes. To get this handy method, just add the following to your project’s Gemfile:
gem 'ruby-openid'
gem 'rack-openid'
gem 'ruby-openid-apps-discovery'
Add the following to the end of your environment.rb to take care of some weirdness with Google OpenID discovery:
require 'gapps_openid'
After that, all you need to do is to install the open_id_authentication plugin:
rails plugin install https://github.com/rails/open_id_authentication.git
With all of that out of the way, you are more or less on your way. Head over here and follow the instructions to become an official vendor and make your app installable. But even without this, Google Apps users will be able to login without issue.
I know that this article is far from perfect, but I hope it will at least inspire others to integrate their apps or create unique solutions for the millions of Google Apps users.