OAuthRails

This Gem offers an 'invisible' way for an OAuth Handshake in an Rails application.

Installation

Add this line to your application's Gemfile:

gem 'oauth_rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install oauth_rails

Configure a few constants in config/initializers/oauth_rails.rb

OAuthRails.setup({
  oauth_host: 'https://api.SOMETHING.com',
  oauth_consumer_key: 'OAUTH_CONSUMER_KEY',
  oauth_consumer_secret: 'OAUTH_CONSUMER_SECRET',
  proc_authorization_successful: Proc.new do |controller_context, token, secret|
    # Do something special (like saving) with the OAuth-'token' and 'secret' and
    # use the 'controller_context' to complete the controller behavior
    controller_context.redirect_to controller_context.root_url
  end,
  proc_authorization_failed: Proc.new { |controller_context| controller_context.root_url }
})

Usage

Redirect the user to the oauth_handshake_path and use the procs to define the behaviors for the success and failure situations. Yes, it is so simple! \o/

Advanced configuration

All default settings can be overwritten with the OAuthRails.setup() method

  DEFAULT_SETTINGS = {
  oauth_host: nil,
  oauth_request_token_path: '/v1/request_token',
  oauth_authorize_path: '/v1/authorize',
  oauth_access_token_path: '/v1/access_token',
  oauth_consumer_key: nil,
  oauth_consumer_secret: nil,
  oauth_callback_url: 'http://localhost:3000/oauth/authorize',
  oauth_verifier_key: :oauth_verifier,
  proc_initialize_handshake:            Proc.new { |controller_context| },
  proc_initialize_authorization_failed: Proc.new { |controller_context| OAuthRails.call(:authorization_failed, controller_context) },
  proc_authorization_successful:        Proc.new { |controller_context, token, secret| raise "Run 'OAuthRails.setup()' to set a ':proc_authorization_successful'" },
  proc_authorization_failed:            Proc.new { |controller_context| raise "Run 'OAuthRails.setup()' to set a ':proc_authorization_failed'" }
}

The procs can be used to execute custom code. The procs proc_authorization_successful and proc_authorization_failed are mandatory for the core functionality., they will be called when the authorization was successful or not. They should handle the OAuth data and redirections.

Example application

In progress.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/oauth_rails/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request