2020-10-18 01:39:46 -07:00
|
|
|
class User::SessionsController < Devise::SessionsController
|
2020-10-23 11:45:06 -07:00
|
|
|
def new
|
|
|
|
session.delete(:user_sign_in_uid)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-10-18 01:39:46 -07:00
|
|
|
def create
|
|
|
|
if session.has_key?(:user_sign_in_uid)
|
2020-10-23 11:45:06 -07:00
|
|
|
self.resource = User.find(session.delete(:user_sign_in_uid))
|
2020-10-18 01:39:46 -07:00
|
|
|
else
|
|
|
|
self.resource = warden.authenticate!(auth_options)
|
|
|
|
end
|
|
|
|
|
2020-10-18 02:39:28 -07:00
|
|
|
if resource.active_for_authentication? && resource.otp_module_enabled?
|
2020-10-18 01:39:46 -07:00
|
|
|
if params[:user][:otp_attempt].blank?
|
|
|
|
session[:user_sign_in_uid] = resource.id
|
|
|
|
sign_out(resource)
|
2020-10-23 11:45:06 -07:00
|
|
|
warden.lock!
|
|
|
|
render 'auth/two_factor_authentication'
|
2020-10-18 01:39:46 -07:00
|
|
|
else
|
2020-11-01 09:29:11 -08:00
|
|
|
if params[:user][:otp_attempt].length == 8
|
|
|
|
found = TotpRecoveryCode.where(user_id: resource.id, code: params[:user][:otp_attempt].downcase).delete_all
|
|
|
|
if found == 1
|
2020-11-01 09:41:37 -08:00
|
|
|
flash[:info] = "You have #{TotpRecoveryCode.where(user_id: resource.id).count} recovery codes remaining."
|
2020-11-01 09:29:11 -08:00
|
|
|
continue_sign_in(resource, resource_name)
|
|
|
|
else
|
|
|
|
flash[:error] = t('views.auth.2fa.errors.invalid_code')
|
|
|
|
redirect_to new_user_session_url
|
|
|
|
end
|
|
|
|
elsif resource.authenticate_otp(params[:user][:otp_attempt], drift: APP_CONFIG.fetch(:otp_drift_period, 30).to_i)
|
2020-10-18 01:39:46 -07:00
|
|
|
continue_sign_in(resource, resource_name)
|
|
|
|
else
|
|
|
|
sign_out(resource)
|
2020-10-23 11:58:42 -07:00
|
|
|
flash[:error] = t('views.auth.2fa.errors.invalid_code')
|
2020-10-23 11:45:06 -07:00
|
|
|
redirect_to new_user_session_url
|
2020-10-18 01:39:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
continue_sign_in(resource, resource_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def continue_sign_in(resource, resource_name)
|
|
|
|
set_flash_message!(:notice, :signed_in)
|
|
|
|
sign_in(resource_name, resource)
|
|
|
|
yield resource if block_given?
|
|
|
|
respond_with resource, location: after_sign_in_path_for(resource)
|
|
|
|
end
|
|
|
|
end
|