Overriding the OmniAuth callback url for twitter or facebook oath processing.

A lot of people are now using OmniAuth for Oath processing to connect applications to twitter, facebook, linkedin or any number of other SAAS applications. It’s easy and it works well.

I’m using it for some work I’m doing with a client of mine (Practically Green – a great little eco-startup) and ran into an issue with how it created callback url’s — the url where the user is redirected after the Oauth authentication is completed.

The issue was with the way our web listeners are proxied back to the rails application servers. Behind the scenes, the rails application servers have a host name of something that’s not really a valid domain name (say, something like ‘railsserver’).

Omniauth was grabbing this value from the request object and forwarding our users back to an Oauth callback url like ‘http://railsservers/auth/twitter/…’. Obviously, this isn’t what we wanted — the Oauth callbacks were failing.

After digging through the Omniauth code, I ran across the offending piece of code:


 def full_host
  case OmniAuth.config.full_host
   when String
    OmniAuth.config.full_host
   when Proc
    OmniAuth.config.full_host.call(env)
   else
    uri = URI.parse(request.url.gsub(/\?.*$/,''))
    uri.path = ''
    uri.query = nil
    uri.to_s
   end
  end

Basically, if this config paramerter :full_host isn’t set to either a string or a proc, omniauth pulls the uri from the request object — this is where it was messing up.

Since the Omniauth config object is a singleton, I just set that parameter in config/initializers/omniauth.rb like so:

(in config/initializers/omniauth.rb)
# Set the default hostname for omniauth to send callbacks to.
OmniAuth.config.full_host = "http://practicallygreen.com"

With this, the full_host param is a string and now the redirects are always created to come back to where we want.

This entry was posted in agile, leanstartup, programming, rails, twitter and tagged , , , , . Bookmark the permalink.
  • http://7fff.com jgn

    Nice.

    Current score: 0
  • http://www.facebook.com/profile.php?id=582030244 Sergio Mendez-Baiges

    really helpful explanation, thanks!

    Current score: 0
  • Ben

    Hi! Sorry for this newbie question: How can I overwrite this method (basically which file I have to create and here I have to locate it?) Thank you

    Current score: 0
  • http://www.facebook.com/people/Jean-de-la-Vega/100001868695326 Jean de la Vega

    Thanks for this useful tip !

    Current score: 0
  • Anonymous

    Any chance to set this dynamically? i.e. not in the initiailizer?

    Current score: 0
  • http://www.javidjamae.com/ Javid Jamae

    This really helped me out, thanks for posting it!

    Current score: 0
  • http://www.triplepointrobotics.com/ Tom Allen

    Awesome – this saved us a bunch of trouble!

    Current score: 0
  • Anonymous

    I’m struggling with this myself. Did you find a solution to this?

    Current score: 0