Using Single Sign-On To Integrate Ning With An External Site

· jonyesno


Overview Ning provide off the peg hosted social networks. The service is free unless you pay to not have their context-driven ads on your pages. Within a few minutes of sign-up you're away.

Particulary cool is that they will let you at the source of your network. You can't then wander off and run it elsewhere, it sits atop of their core web framework that you can't see. With the source (which is encouragingly well written PHP) you can do perform all manner of modifications to bend the template network to your will.

Ning provide their own authentication system, and there's no API to hook in someone else's which is a hassle if you're trying to build a social network alongside another site: maintaining a login for each site is going to annoy the users and be a nightmare to manage. Nil points.

One solution is to build a single sign-on system around what Ning already provide, which is robust, tested and better presented than anything we could achieve! I'd sketched out such a system for a proposal years ago but never had the opportunity to build it. A current project provided the perfect excuse to try this out.

Layout Operation The numbers here match those in the diagram.

  1. User visits www.example.com (which for us happens to be a Rails) site.
  2. The PageController notices the browser supplied no cookie and must therefore log on to Ning before proceeding. The site returns a redirect to Ning's authentication page.
  3. The browser follows this redirect to Ning.
  4. Ning authenticates the user. The login code is modified from the original Ning behaviour. Here, it issues a redirect to sso.example.com with some parameters, including the user's Ning ID and a salted hash to prevent spoofing. In this redirect Ning sets a range of cookies, including one that identifies the user to Ning.
  5. The browser follows this redirect to the SSO server (which happens to be a Merb site - I wanted to try it out!)
  6. The SSO app checks the provided hash against its own idea of what it should be. Assuming they match it considers the Ning ID to be valid. Finally, the SSO app issues another redirect along with cookies for just .example.com. This cookie identifies the user to the external site
  7. In passing, the SSO app keeps track of users it has seen, and if this is a new user it will make an API request to Ning to fetch that user's profile data and create a matching user on the www.example.com site.
  8. The browser follows this last redirect back to Ning. It could be back to www.example.com or Ning depending on the situation.
  9. Ning knows who the user is by merit of its cookies.
  10. As does our external site.

Notes

Specifics I was about to paste bits of modified Ning code, but I'll need to check if I can under the various blurbs you agree too when signing up! Anyhow, the information above should help answer some of the requests for details from the Ning developers forum.

last updated: