2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-12 16:37:38 -06:00
|
|
|
class UserAuthenticator
|
2021-03-02 01:13:04 -06:00
|
|
|
def initialize(
|
|
|
|
user,
|
|
|
|
session,
|
|
|
|
authenticator_finder: Users::OmniauthCallbacksController,
|
|
|
|
require_password: true
|
|
|
|
)
|
2013-11-12 16:37:38 -06:00
|
|
|
@user = user
|
2019-09-12 06:11:12 -05:00
|
|
|
@session = session
|
2021-03-02 01:13:04 -06:00
|
|
|
if session&.dig(:authentication) && session[:authentication].is_a?(Hash)
|
2020-06-18 05:01:02 -05:00
|
|
|
@auth_result = Auth::Result.from_session_data(session[:authentication], user: user)
|
2020-06-17 05:15:53 -05:00
|
|
|
end
|
2013-11-12 16:37:38 -06:00
|
|
|
@authenticator_finder = authenticator_finder
|
2021-03-02 01:13:04 -06:00
|
|
|
@require_password = require_password
|
2013-11-12 16:37:38 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
if authenticated?
|
|
|
|
@user.active = true
|
2020-06-18 05:01:02 -05:00
|
|
|
@auth_result.apply_user_attributes!
|
2021-03-02 01:13:04 -06:00
|
|
|
elsif @require_password
|
2013-11-12 16:37:38 -06:00
|
|
|
@user.password_required!
|
|
|
|
end
|
2016-09-07 13:05:46 -05:00
|
|
|
|
2020-06-17 05:15:53 -05:00
|
|
|
@user.skip_email_validation = true if @auth_result && @auth_result.skip_email_validation
|
2013-11-12 16:37:38 -06:00
|
|
|
end
|
|
|
|
|
2014-03-19 22:49:25 -05:00
|
|
|
def has_authenticator?
|
|
|
|
!!authenticator
|
|
|
|
end
|
|
|
|
|
2013-11-12 16:37:38 -06:00
|
|
|
def finish
|
2018-05-22 18:26:07 -05:00
|
|
|
if authenticator
|
2020-06-17 05:15:53 -05:00
|
|
|
authenticator.after_create_account(@user, @auth_result)
|
2018-05-22 18:26:07 -05:00
|
|
|
confirm_email
|
|
|
|
end
|
2024-08-28 20:19:04 -05:00
|
|
|
if @session&.dig(:authentication)
|
|
|
|
@session[:authentication] = @auth_result = nil
|
|
|
|
@session[:authenticated_with_oauth] = true
|
|
|
|
end
|
2013-11-12 16:37:38 -06:00
|
|
|
end
|
|
|
|
|
2017-10-16 12:51:35 -05:00
|
|
|
def email_valid?
|
2020-06-17 05:15:53 -05:00
|
|
|
@auth_result&.email_valid
|
2017-10-16 12:51:35 -05:00
|
|
|
end
|
2013-11-12 16:37:38 -06:00
|
|
|
|
|
|
|
def authenticated?
|
2020-06-18 05:01:02 -05:00
|
|
|
return false if !@auth_result
|
2020-07-10 11:56:33 -05:00
|
|
|
return false if @auth_result&.email&.downcase != @user.email.downcase
|
2021-02-22 06:05:36 -06:00
|
|
|
return false if !@auth_result.email_valid
|
2020-06-18 05:01:02 -05:00
|
|
|
true
|
2013-11-12 16:37:38 -06:00
|
|
|
end
|
|
|
|
|
2017-10-16 12:51:35 -05:00
|
|
|
private
|
|
|
|
|
2018-05-22 18:26:07 -05:00
|
|
|
def confirm_email
|
2021-11-25 01:34:39 -06:00
|
|
|
@user.activate if authenticated?
|
2018-05-22 18:26:07 -05:00
|
|
|
end
|
|
|
|
|
2013-11-12 16:37:38 -06:00
|
|
|
def authenticator
|
|
|
|
if authenticator_name
|
|
|
|
@authenticator ||= @authenticator_finder.find_authenticator(authenticator_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticator_name
|
2020-06-17 05:15:53 -05:00
|
|
|
@auth_result&.authenticator_name
|
2013-11-12 16:37:38 -06:00
|
|
|
end
|
|
|
|
end
|