项目作者: dahal

项目描述 :
Oauth2 strategy for ADP Workforce Now
高级语言: Ruby
项目地址: git://github.com/dahal/omniauth-adp-oauth2.git
创建时间: 2016-08-22T22:12:08Z
项目社区:https://github.com/dahal/omniauth-adp-oauth2

开源协议:

下载


Gem Version
Code Climate
Issue Count

OmniAuth ADP OAuth2 Strategy

Strategy to authenticate with ADP via OpenID CConnect in OmniAuth.

Installation

Add to your Gemfile:

  1. gem 'omniauth-adp-oauth2'

Then bundle install.

Usage

Here’s an example for adding the middleware to a Rails app in config/initializers/omniauth.rb:

  1. Rails.application.config.middleware.use OmniAuth::Builder do
  2. provider :adp_oauth2, ENV["ADP_CONSUMER_CLIENT_ID"], ENV["ADP_CONSUMER_CLIENT_SECRET"]
  3. end

Devise

First define your application id and secret in config/initializers/devise.rb.

  1. config.omniauth :adp_oauth2, "ADP_CONSUMER_CLIENT_ID", "ADP_CONSUMER_CLIENT_SECRET", { }

Then add the following to ‘config/routes.rb’ so the callback routes are defined.

  1. devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

Make sure your model is omniauthable. Generally this is “/app/models/user.rb”

  1. devise :omniauthable, :omniauth_providers => [:adp_oauth2]

Then make sure your callbacks controller is setup.

  1. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  2. def adp_oauth2
  3. # You need to implement the method below in your model (e.g. app/models/user.rb)
  4. @user = User.from_omniauth(request.env["omniauth.auth"])
  5. if @user.persisted?
  6. flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "ADP"
  7. sign_in_and_redirect @user, :event => :authentication
  8. else
  9. session["devise.adp_data"] = request.env["omniauth.auth"]
  10. redirect_to new_user_registration_url
  11. end
  12. end
  13. end

and bind to or create the user

  1. def self.from_omniauth(access_token)
  2. data = access_token.info
  3. user = User.where(:email => data["email"]).first
  4. # Uncomment the section below if you want users to be created if they don't exist
  5. # unless user
  6. # user = User.create(name: data["name"],
  7. # email: data["email"],
  8. # password: Devise.friendly_token[0,20]
  9. # )
  10. # end
  11. user
  12. end

For your views you can login using:

  1. <%= link_to "Sign in with ADP", user_adp_oauth2_omniauth_authorize_path %>
  2. <%# Devise prior 4.1.0: %>
  3. <%= link_to "Sign in with ADP", user_omniauth_authorize_path(:adp_oauth2) %>

An overview is available at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

Auth Hash

Here’s an example of an authentication hash available in the callback by accessing request.env["omniauth.auth"]:

Credits

License

Copyright (c) 2016 by Puru Dahal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.