-
-
Save davidkrider/32b803675e6a19b418855522f534e090 to your computer and use it in GitHub Desktop.
Rails - SSO - WordPress - Authenticating against a WordPress install from a Rails site using oAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment