Skip to content

Instantly share code, notes, and snippets.

@davidkrider
Forked from bearded-avenger/devise.rb
Created February 24, 2019 14:43
Show Gist options
  • Select an option

  • Save davidkrider/32b803675e6a19b418855522f534e090 to your computer and use it in GitHub Desktop.

Select an option

Save davidkrider/32b803675e6a19b418855522f534e090 to your computer and use it in GitHub Desktop.

Revisions

  1. Nick Haskins revised this gist May 26, 2016. No changes.
  2. Nick Haskins revised this gist May 26, 2016. No changes.
  3. Nick Haskins revised this gist May 26, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
  4. Nick Haskins revised this gist May 26, 2016. 2 changed files with 20 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions devise.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    config.omniauth :wordpress_hosted, ENV['SSO_KEY'], ENV['SSO_SECRET'],
    strategy_class: OmniAuth::Strategies::WordpressHosted,
    client_options: { site: ENV['SSO_URL'] }
    17 changes: 17 additions & 0 deletions omniauth_callbacks_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

    def wordpress_hosted

    Rails.logger.debug request.env["omniauth.auth"]

    @user = User.find_for_wordpress_oauth2(request.env["omniauth.auth"], current_user)

    if @user.persisted?
    flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "your CG Cookie"
    sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
    else
    session["devise.wordpress_oauth2_data"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
    end
    end
    end
  5. Nick Haskins revised this gist May 26, 2016. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable

    def self.find_for_wordpress_oauth2(oauth, signed_in_user=nil)
    if signed_in_user

    if signed_in_user.email.nil? or signed_in_user.email.eql?('')
    signed_in_user.update_attributes(email: oauth['info']['email'])
    end
    return signed_in_user
    else
    user = User.find_by_provider_and_uid(oauth['provider'], oauth['uid'])
    if user.nil?
    user = User.create!(email: oauth['info']['email'], id: oauth['uid'], provider: oauth['provider'] )
    end
    user
    end
    end

    def self.find_by_provider_and_uid(provider, uid)
    where(provider: provider, id: uid).first
    end

    end
  6. Nick Haskins created this gist May 26, 2016.
    6 changes: 6 additions & 0 deletions gemfile.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #authentication framework
    gem 'devise'
    #oauth2 integration
    gem 'omniauth'
    gem 'omniauth-oauth2', '1.3.1' # DO NOT change this! If we update teo 1.4 the SSO doesnt work anymore
    gem 'omniauth-wordpress_hosted', github: 'jwickard/omniauth-wordpress-oauth2-plugin'