Rename methods to avoid confusion between OAuth and OmniAuth
parent
7857ed32ab
commit
b33a9d4449
|
@ -7,7 +7,7 @@ class Auth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
||||||
def self.provides_callback_for(provider)
|
def self.provides_callback_for(provider)
|
||||||
define_method provider do
|
define_method provider do
|
||||||
@provider = provider
|
@provider = provider
|
||||||
@user = User.find_for_oauth(request.env['omniauth.auth'], current_user)
|
@user = User.find_for_omniauth(request.env['omniauth.auth'], current_user)
|
||||||
|
|
||||||
if @user.persisted?
|
if @user.persisted?
|
||||||
record_login_activity
|
record_login_activity
|
||||||
|
|
|
@ -19,18 +19,18 @@ module Omniauthable
|
||||||
end
|
end
|
||||||
|
|
||||||
class_methods do
|
class_methods do
|
||||||
def find_for_oauth(auth, signed_in_resource = nil)
|
def find_for_omniauth(auth, signed_in_resource = nil)
|
||||||
# EOLE-SSO Patch
|
# EOLE-SSO Patch
|
||||||
auth.uid = (auth.uid[0][:uid] || auth.uid[0][:user]) if auth.uid.is_a? Hashie::Array
|
auth.uid = (auth.uid[0][:uid] || auth.uid[0][:user]) if auth.uid.is_a? Hashie::Array
|
||||||
identity = Identity.find_for_oauth(auth)
|
identity = Identity.find_for_omniauth(auth)
|
||||||
|
|
||||||
# If a signed_in_resource is provided it always overrides the existing user
|
# If a signed_in_resource is provided it always overrides the existing user
|
||||||
# to prevent the identity being locked with accidentally created accounts.
|
# to prevent the identity being locked with accidentally created accounts.
|
||||||
# Note that this may leave zombie accounts (with no associated identity) which
|
# Note that this may leave zombie accounts (with no associated identity) which
|
||||||
# can be cleaned up at a later date.
|
# can be cleaned up at a later date.
|
||||||
user = signed_in_resource || identity.user
|
user = signed_in_resource || identity.user
|
||||||
user ||= reattach_for_oauth(auth)
|
user ||= reattach_for_auth(auth)
|
||||||
user ||= create_for_oauth(auth)
|
user ||= create_for_auth(auth)
|
||||||
|
|
||||||
if identity.user.nil?
|
if identity.user.nil?
|
||||||
identity.user = user
|
identity.user = user
|
||||||
|
@ -40,7 +40,9 @@ module Omniauthable
|
||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
def reattach_for_oauth(auth)
|
private
|
||||||
|
|
||||||
|
def reattach_for_auth(auth)
|
||||||
# If allowed, check if a user exists with the provided email address,
|
# If allowed, check if a user exists with the provided email address,
|
||||||
# and return it if they does not have an associated identity with the
|
# and return it if they does not have an associated identity with the
|
||||||
# current authentication provider.
|
# current authentication provider.
|
||||||
|
@ -52,7 +54,7 @@ module Omniauthable
|
||||||
|
|
||||||
return unless ENV['ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH'] == 'true'
|
return unless ENV['ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH'] == 'true'
|
||||||
|
|
||||||
email, email_is_verified = email_from_oauth(auth)
|
email, email_is_verified = email_from_auth(auth)
|
||||||
return unless email_is_verified
|
return unless email_is_verified
|
||||||
|
|
||||||
user = User.find_by(email: email)
|
user = User.find_by(email: email)
|
||||||
|
@ -61,12 +63,12 @@ module Omniauthable
|
||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_for_oauth(auth)
|
def create_for_auth(auth)
|
||||||
# Create a user for the given auth params. If no email was provided,
|
# Create a user for the given auth params. If no email was provided,
|
||||||
# we assign a temporary email and ask the user to verify it on
|
# we assign a temporary email and ask the user to verify it on
|
||||||
# the next step via Auth::SetupController.show
|
# the next step via Auth::SetupController.show
|
||||||
|
|
||||||
email, email_is_verified = email_from_oauth(auth)
|
email, email_is_verified = email_from_auth(auth)
|
||||||
|
|
||||||
user = User.new(user_params_from_auth(email, auth))
|
user = User.new(user_params_from_auth(email, auth))
|
||||||
|
|
||||||
|
@ -81,9 +83,7 @@ module Omniauthable
|
||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def email_from_auth(auth)
|
||||||
|
|
||||||
def email_from_oauth(auth)
|
|
||||||
strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
|
strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
|
||||||
assume_verified = strategy&.security&.assume_email_is_verified
|
assume_verified = strategy&.security&.assume_email_is_verified
|
||||||
email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
|
email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
|
||||||
|
|
|
@ -17,7 +17,7 @@ class Identity < ApplicationRecord
|
||||||
validates :uid, presence: true, uniqueness: { scope: :provider }
|
validates :uid, presence: true, uniqueness: { scope: :provider }
|
||||||
validates :provider, presence: true
|
validates :provider, presence: true
|
||||||
|
|
||||||
def self.find_for_oauth(auth)
|
def self.find_for_omniauth(auth)
|
||||||
find_or_create_by(uid: auth.uid, provider: auth.provider)
|
find_or_create_by(uid: auth.uid, provider: auth.provider)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,16 +3,16 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe Identity do
|
RSpec.describe Identity do
|
||||||
describe '.find_for_oauth' do
|
describe '.find_for_omniauth' do
|
||||||
let(:auth) { Fabricate(:identity, user: Fabricate(:user)) }
|
let(:auth) { Fabricate(:identity, user: Fabricate(:user)) }
|
||||||
|
|
||||||
it 'calls .find_or_create_by' do
|
it 'calls .find_or_create_by' do
|
||||||
expect(described_class).to receive(:find_or_create_by).with(uid: auth.uid, provider: auth.provider)
|
expect(described_class).to receive(:find_or_create_by).with(uid: auth.uid, provider: auth.provider)
|
||||||
described_class.find_for_oauth(auth)
|
described_class.find_for_omniauth(auth)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns an instance of Identity' do
|
it 'returns an instance of Identity' do
|
||||||
expect(described_class.find_for_oauth(auth)).to be_instance_of described_class
|
expect(described_class.find_for_omniauth(auth)).to be_instance_of described_class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -96,7 +96,7 @@ describe 'OmniAuth callbacks' do
|
||||||
|
|
||||||
context 'when a user cannot be built' do
|
context 'when a user cannot be built' do
|
||||||
before do
|
before do
|
||||||
allow(User).to receive(:find_for_oauth).and_return(User.new)
|
allow(User).to receive(:find_for_omniauth).and_return(User.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the new user signup page' do
|
it 'redirects to the new user signup page' do
|
||||||
|
|
Loading…
Reference in New Issue