Giter VIP home page Giter VIP logo

open_id_authentication's Introduction

OpenID authentication engine requires Rails 4.1.0 or above.

Configuring OpenID Authentication

Add the following to the Gemfile:

gem 'open_id_authentication', '~> 0.1'

Add the following to config/application.rb:

config.railties_order = [OpenIdAuthentication::Engine, :main_app, :all]

as the first line after

class Application < Rails::Application

Using OpenID Authentication

Create a link in a view that looks like the following:

= link_to "OpenID Login", open_id_connect_path(url: "OpenID Provider URL", redirect: "Redirect URL")

url is a required parameter and specifies the URL of the OpenID provider. For example, for Google, you could set this to "https://www.google.com/accounts/o8/id".

redirect is an optional parameter. It is set to a URL within your application where the browser will redirect after a successful or failed OpenID authentication with the OpenID provider.

Example:

- google_url = "https://www.google.com/accounts/o8/id"
= link_to "OpenID Login", open_id_connect_path(url: google_url, redirect: root_url)

Default Behavior

If the redirect parameter is not specified, the user will be redirected back to the same location. On a successful login, session[:open_id_data] will be set to the data returned by the OpenID provider. This data contains the email address and full name of the user and can be accessed as follows:

open_id_data = session[:open_id_data]
email = open_id_data['email']
name = open_id_data['fullname']

Customized Behavior

If you want a custom behavior instead of the default behavior outlined above, you can define an on_open_id_login method in your application controller, which will be called on a successful login with the returned OpenID data.

No further processing will be done. You will be responsible for terminating this method with a response.

An example of using this in conjunction with the user_authentication gem is as follows:

class ApplicationController < ActionController::Base
  protect_from_forgery

  def on_open_id_login(data)
    email = Mail::Address.new(data['email'])
    if email.domain != 'example.com'
      return head :forbidden
    end

    account = Account.find_or_create_by! email: email.address
    session[:account_id] = account.id
    set_current_account

    redirect_to params[:redirect] || root_path
  end
end

open_id_authentication's People

Contributors

sujoyg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.