2017-01-28 04:28:46 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Settings::TwoFactorAuthsController < ApplicationController
|
2017-01-28 11:56:10 +09:00
|
|
|
layout 'admin'
|
2017-01-28 04:28:46 +09:00
|
|
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2017-04-09 05:20:08 +09:00
|
|
|
def show; end
|
2017-01-28 04:28:46 +09:00
|
|
|
|
2017-04-09 05:20:08 +09:00
|
|
|
def new
|
|
|
|
redirect_to settings_two_factor_auth_path if current_user.otp_required_for_login
|
2017-01-28 04:28:46 +09:00
|
|
|
|
2017-04-09 05:20:08 +09:00
|
|
|
@confirmation = Form::TwoFactorConfirmation.new
|
|
|
|
current_user.otp_secret = User.generate_otp_secret(32)
|
2017-01-28 04:28:46 +09:00
|
|
|
current_user.save!
|
2017-04-09 05:20:08 +09:00
|
|
|
set_qr_code
|
|
|
|
end
|
2017-01-28 04:28:46 +09:00
|
|
|
|
2017-04-09 05:20:08 +09:00
|
|
|
def create
|
|
|
|
if current_user.validate_and_consume_otp!(confirmation_params[:code])
|
|
|
|
current_user.otp_required_for_login = true
|
2017-04-15 20:26:03 +09:00
|
|
|
@codes = current_user.generate_otp_backup_codes!
|
2017-04-09 05:20:08 +09:00
|
|
|
current_user.save!
|
2017-04-15 20:26:03 +09:00
|
|
|
flash[:notice] = I18n.t('two_factor_auth.enabled_success')
|
2017-04-09 05:20:08 +09:00
|
|
|
else
|
|
|
|
@confirmation = Form::TwoFactorConfirmation.new
|
|
|
|
set_qr_code
|
|
|
|
flash.now[:alert] = I18n.t('two_factor_auth.wrong_code')
|
2017-04-19 22:37:42 +09:00
|
|
|
render :new
|
2017-04-09 05:20:08 +09:00
|
|
|
end
|
2017-01-28 04:28:46 +09:00
|
|
|
end
|
|
|
|
|
2017-04-15 20:26:03 +09: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-28 04:28:46 +09:00
|
|
|
def disable
|
|
|
|
current_user.otp_required_for_login = false
|
|
|
|
current_user.save!
|
|
|
|
|
|
|
|
redirect_to settings_two_factor_auth_path
|
|
|
|
end
|
2017-04-09 05:20:08 +09: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-28 04:28:46 +09:00
|
|
|
end
|