-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
class Users::SessionsController < Devise::SessionsController | ||
# prepend_before_action :configure_permitted_parameters, if: :devise_controller? | ||
prepend_before_action :otp_authentication, only: :create | ||
|
||
def new | ||
# New login attempts should reset the otp_user_id | ||
session.delete(:otp_user_id) | ||
super | ||
end | ||
|
||
# Override the default Devise create method and check if the user has enabled 2FA | ||
# If 2FA is enabled, render the OTP form page. Otherwise, proceed with the default login flow. | ||
def otp_authentication | ||
devise_parameter_sanitizer.permit(:sign_in, keys: [:otp_attempt]) | ||
|
||
if sign_in_params[:email] | ||
show_otp_code_form | ||
elsif session[:otp_user_id] | ||
validate_otp_code | ||
end | ||
end | ||
|
||
def show_otp_code_form | ||
# Reset the session if the user is trying to login again. | ||
session.delete(:otp_user_id) | ||
|
||
self.resource = User.find_by(email: sign_in_params[:email]) | ||
if resource.valid_password?(sign_in_params[:password]) && resource.otp_required_for_login? | ||
session[:otp_user_id] = resource.id | ||
render :otp, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def validate_otp_code | ||
self.resource = User.find(session[:otp_user_id]) | ||
if resource.validate_and_consume_otp!(sign_in_params[:otp_attempt]) | ||
|
||
set_flash_message!(:notice, :signed_in) | ||
sign_in(resource_name, resource) | ||
redirect_to after_sign_in_path_for(resource) | ||
else | ||
resource.errors.add(:otp_attempt, :invalid) | ||
render :otp, status: :unprocessable_entity | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<% title "Two factor authentication" %> | ||
|
||
<%= fullscreen do |screen| %> | ||
<%= screen.with_form( | ||
title: t(".heading.title", app_name: Kiqr::Config.app_name), | ||
description: t(".heading.description") | ||
) do %> | ||
<main class="flex flex-col gap-4 w-full max-w-xl"> | ||
<%= simple_form_for(resource, as: resource_name, url: new_user_session_path, method: :post) do |f| %> | ||
<%= f.input :otp_attempt, label: t(".form.otp.label"), placeholder: t(".form.otp.placeholder") %> | ||
|
||
<div class="flex justify-between items-center"> | ||
<%= f.button :submit, t(".form.submit_button") %> | ||
</div> | ||
<% end %> | ||
</main> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aecc9f0ffc88c629a51ee97520c76963 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nl6YcBTnI/xYrRDaLXfnCNI8HV9AhUXELHeFfAZ7A7StirCmjsNU1RN64n0XHs9nwaANuWrVpuEmyw1nSkpKBkkaOTY/OFNIjua7gULz43Z0u+yuSxNDXVD1REVWmtKfAioQUWy564MbLNDcbic8zlJJsv3zgEN5cNe6/nLBDov8aG9LFyJg948ZO5QBj2N41dZl5MjesyPIPfFpYtzEwNdrGlHxYtGmmAcl9nRIMBAN6T+26RAkQoxIDPwOk+N384Wxisc5AvenRj+GCLgCZoC9pbNdWYnnZck/yxB8/rA9slCdmTrPxsFTdP2O8ho6LLe23HthKzqVkEwgNXUTiDHeGV76Z83u27tNBclGjxuCtE8GZZsNmq8onexj2PLxy8VlZ4iQhxPu9k7cPEm7t+te476FvSWwpSJUyq3H1+L7OOdMhhVYEEjie8Ue+vmifiZKbxDGMBIEi9/tY3yNmLdHBT/JnXr3AegJIS5+UHh4J5Cl8naTWYrXU2E7/o8x8Hl2Rr4uWXSpuKPIdd0cqzayITl+mDrXfEH0OE/n4WcMYWn5fAnf90AfFgSZ32+WufkisKl7E1cWWDOFpTh+16bz1v7clAazhv9oKmCOOko=--MHfJClNqfFA2siBd--fWeUh7gKNWGNnxnxulvNZQ== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<% unless options.skip_secret_key_base? -%> | ||
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. | ||
secret_key_base: <%= secret_key_base %> | ||
|
||
<% end -%> | ||
# The new encryption features in Active Record. This will enable the new `encrypts` method to encrypt attributes. | ||
# This is a required configuration if you want to enable two factor authentication. | ||
# | ||
# Read more: https://guides.rubyonrails.org/active_record_encryption.html | ||
active_record_encryption: | ||
primary_key: <%= SecureRandom.alphanumeric(32) %> | ||
deterministic_key: <%= SecureRandom.alphanumeric(32) %> | ||
key_derivation_salt: <%= SecureRandom.alphanumeric(32) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "test_helper" | ||
|
||
class Users::SessionsControllerTest < ActionDispatch::IntegrationTest | ||
test "require otp if otp is enabled" do | ||
user = create(:user, :otp_enabled) | ||
post user_session_path, params: {user: {email: user.email, password: user.password}} | ||
assert_response :unprocessable_entity | ||
assert_template "users/sessions/otp" | ||
end | ||
|
||
test "can sign in if otp is disabled" do | ||
user = create(:user) | ||
post user_session_path, params: {user: {email: user.email, password: user.password}} | ||
assert_response :redirect | ||
assert_redirected_to dashboard_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters