2017-01-27 14:28:46 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Settings::TwoFactorAuthsController < ApplicationController
|
2017-01-27 21:56:10 -05:00
|
|
|
layout 'admin'
|
2017-01-27 14:28:46 -05:00
|
|
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2017-04-08 16:20:08 -04:00
|
|
|
def show; end
|
2017-01-27 14:28:46 -05:00
|
|
|
|
2017-04-08 16:20:08 -04:00
|
|
|
def new
|
|
|
|
redirect_to settings_two_factor_auth_path if current_user.otp_required_for_login
|
2017-01-27 14:28:46 -05:00
|
|
|
|
2017-04-08 16:20:08 -04:00
|
|
|
@confirmation = Form::TwoFactorConfirmation.new
|
|
|
|
current_user.otp_secret = User.generate_otp_secret(32)
|
2017-01-27 14:28:46 -05:00
|
|
|
current_user.save!
|
2017-04-08 16:20:08 -04:00
|
|
|
set_qr_code
|
|
|
|
end
|
2017-01-27 14:28:46 -05:00
|
|
|
|
2017-04-08 16:20:08 -04:00
|
|
|
def create
|
|
|
|
if current_user.validate_and_consume_otp!(confirmation_params[:code])
|
|
|
|
current_user.otp_required_for_login = true
|
2017-04-15 07:26:03 -04:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2017-04-08 16:20:08 -04:00
|
|
|
current_user.save!
|
2017-04-15 07:26:03 -04:00
|
|
|
flash[:notice] = I18n.t('two_factor_auth.enabled_success')
|
2017-04-08 16:20:08 -04:00
|
|
|
else
|
|
|
|
@confirmation = Form::TwoFactorConfirmation.new
|
|
|
|
set_qr_code
|
|
|
|
flash.now[:alert] = I18n.t('two_factor_auth.wrong_code')
|
2017-04-19 09:37:42 -04:00
|
|
|
render :new
|
2017-04-08 16:20:08 -04:00
|
|
|
end
|
2017-01-27 14:28:46 -05:00
|
|
|
end
|
|
|
|
|
2017-04-15 07:26:03 -04:00
|
|
|
def recovery_codes
|
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
|
|
|
current_user.save!
|
|
|
|
flash[:notice] = I18n.t('two_factor_auth.recovery_codes_regenerated')
|
|
|
|
end
|
|
|
|
|
2017-01-27 14:28:46 -05:00
|
|
|
def disable
|
|
|
|
current_user.otp_required_for_login = false
|
|
|
|
current_user.save!
|
|
|
|
|
|
|
|
redirect_to settings_two_factor_auth_path
|
|
|
|
end
|
2017-04-08 16:20:08 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_qr_code
|
|
|
|
@provision_url = current_user.otp_provisioning_uri(current_user.email, issuer: Rails.configuration.x.local_domain)
|
|
|
|
@qrcode = RQRCode::QRCode.new(@provision_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirmation_params
|
|
|
|
params.require(:form_two_factor_confirmation).permit(:code)
|
|
|
|
end
|
2017-01-27 14:28:46 -05:00
|
|
|
end
|