2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
class UserAuthenticator
|
2014-11-17 06:04:29 -05:00
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
def initialize(user, session, authenticator_finder = Users::OmniauthCallbacksController)
|
|
|
|
@user = user
|
2019-09-12 07:11:12 -04:00
|
|
|
@session = session
|
2020-06-17 06:15:53 -04:00
|
|
|
if session[:authentication] && session[:authentication].is_a?(Hash)
|
2020-06-18 06:01:02 -04:00
|
|
|
@auth_result = Auth::Result.from_session_data(session[:authentication], user: user)
|
2020-06-17 06:15:53 -04:00
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
@authenticator_finder = authenticator_finder
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
if authenticated?
|
|
|
|
@user.active = true
|
2020-06-18 06:01:02 -04:00
|
|
|
@auth_result.apply_user_attributes!
|
2013-11-12 17:37:38 -05:00
|
|
|
else
|
|
|
|
@user.password_required!
|
|
|
|
end
|
2016-09-07 14:05:46 -04:00
|
|
|
|
2020-06-17 06:15:53 -04:00
|
|
|
@user.skip_email_validation = true if @auth_result && @auth_result.skip_email_validation
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
|
|
|
|
2014-03-19 23:49:25 -04:00
|
|
|
def has_authenticator?
|
|
|
|
!!authenticator
|
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
def finish
|
2018-05-22 19:26:07 -04:00
|
|
|
if authenticator
|
2020-06-17 06:15:53 -04:00
|
|
|
authenticator.after_create_account(@user, @auth_result)
|
2018-05-22 19:26:07 -04:00
|
|
|
confirm_email
|
|
|
|
end
|
2020-06-17 06:15:53 -04:00
|
|
|
@session[:authentication] = @auth_result = nil if @session[:authentication]
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
|
|
|
|
2017-10-16 13:51:35 -04:00
|
|
|
def email_valid?
|
2020-06-17 06:15:53 -04:00
|
|
|
@auth_result&.email_valid
|
2017-10-16 13:51:35 -04:00
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
|
|
|
|
def authenticated?
|
2020-06-18 06:01:02 -04:00
|
|
|
return false if !@auth_result
|
2020-07-10 12:56:33 -04:00
|
|
|
return false if @auth_result&.email&.downcase != @user.email.downcase
|
2020-06-18 06:01:02 -04:00
|
|
|
return false if @auth_result.email_valid != true # strong check for truth, in case we have another object type
|
|
|
|
true
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
|
|
|
|
2017-10-16 13:51:35 -04:00
|
|
|
private
|
|
|
|
|
2018-05-22 19:26:07 -04:00
|
|
|
def confirm_email
|
|
|
|
if authenticated?
|
|
|
|
EmailToken.confirm(@user.email_tokens.first.token)
|
|
|
|
@user.set_automatic_groups
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
def authenticator
|
|
|
|
if authenticator_name
|
|
|
|
@authenticator ||= @authenticator_finder.find_authenticator(authenticator_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticator_name
|
2020-06-17 06:15:53 -04:00
|
|
|
@auth_result&.authenticator_name
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
2014-11-17 06:04:29 -05:00
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|