Merge commit from fork
Signed-off-by: Claire <claire.github-309c@sitedethib.com> Co-authored-by: ccsimplyspolit <ccsimplyspolit@users.noreply.github.com>
This commit is contained in:
1 parent
aeb73fa842
commit
1e1852ec1f
4 files changed
+35
-13
No files matched your search
@@ -170,6 +170,10 @@ class Auth::SessionsController < Devise::SessionsController
|
||||
)
|
||||
)
|
||||
|
||||
# No need to send a mail for failure to log in with a sign-in token, as that goes throug a mail
|
||||
# as well
|
||||
return if security_measure == :sign_in_token
|
||||
|
||||
# Only send a notification email every hour at most
|
||||
return if redis.set("2fa_failure_notification:#{user.id}", '1', ex: 1.hour, get: true).present?
|
||||
|
||||
|
||||
@@ -12,22 +12,34 @@ module Auth::SignInTokenAuthenticationConcern
|
||||
end
|
||||
|
||||
def authenticate_with_sign_in_token
|
||||
user = self.resource = find_user
|
||||
if user_params[:email].present?
|
||||
user = self.resource = find_user_from_params
|
||||
prompt_for_sign_in_token(user) if user&.valid_password?(user_params[:password])
|
||||
elsif session[:attempt_user_id]
|
||||
user = self.resource = User.find_by(id: session[:attempt_user_id])
|
||||
return if user.nil?
|
||||
|
||||
if params.key?(:sign_in_token_resend) && session[:attempt_user_id]
|
||||
prompt_for_sign_in_token(user, resend: true)
|
||||
elsif user_params[:sign_in_token_attempt].present? && session[:attempt_user_id]
|
||||
authenticate_with_sign_in_token_attempt(user)
|
||||
elsif user.present? && user.valid_password?(user_params[:password])
|
||||
prompt_for_sign_in_token(user)
|
||||
if session[:attempt_user_updated_at] != user.updated_at.to_s
|
||||
restart_session
|
||||
elsif params.key?(:sign_in_token_resend)
|
||||
prompt_for_sign_in_token(user, resend: true)
|
||||
elsif user_params[:sign_in_token_attempt].present?
|
||||
authenticate_with_sign_in_token_attempt(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def authenticate_with_sign_in_token_attempt(user)
|
||||
if check_second_factor_rate_limits(user)
|
||||
flash.now[:alert] = I18n.t('users.rate_limited')
|
||||
return prompt_for_sign_in_token(user)
|
||||
end
|
||||
|
||||
if user.valid_sign_in_token?(user_params[:sign_in_token_attempt])
|
||||
clear_attempt_from_session
|
||||
sign_in(user)
|
||||
user.clear_sign_in_token!
|
||||
on_authentication_success(user, :sign_in_token)
|
||||
else
|
||||
on_authentication_failure(user, :sign_in_token, :invalid_sign_in_token)
|
||||
flash.now[:alert] = I18n.t('users.invalid_sign_in_token')
|
||||
prompt_for_sign_in_token(user)
|
||||
end
|
||||
|
||||
@@ -24,7 +24,7 @@ module User::SignInToken
|
||||
end
|
||||
|
||||
def valid_sign_in_token?(token)
|
||||
Devise.secure_compare(sign_in_token, token)
|
||||
!sign_in_token_expired? && Devise.secure_compare(sign_in_token, token)
|
||||
end
|
||||
|
||||
def sign_in_token_expired?
|
||||
@@ -36,6 +36,12 @@ module User::SignInToken
|
||||
self.sign_in_token_sent_at = Time.now.utc
|
||||
end
|
||||
|
||||
def clear_sign_in_token!
|
||||
self.sign_in_token = nil
|
||||
self.sign_in_token_sent_at = nil
|
||||
save!
|
||||
end
|
||||
|
||||
def disable_sign_in_token!
|
||||
self.sign_in_token = nil
|
||||
self.sign_in_token_sent_at = nil
|
||||
|
||||
@@ -476,7 +476,7 @@ RSpec.describe Auth::SessionsController do
|
||||
before do
|
||||
user.generate_sign_in_token
|
||||
user.save
|
||||
post :create, params: { user: { sign_in_token_attempt: user.sign_in_token } }, session: { attempt_user_id: user.id }
|
||||
post :create, params: { user: { sign_in_token_attempt: user.sign_in_token } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
|
||||
end
|
||||
|
||||
it 'redirects to home' do
|
||||
@@ -490,7 +490,7 @@ RSpec.describe Auth::SessionsController do
|
||||
|
||||
context 'when using an invalid sign in token' do
|
||||
before do
|
||||
post :create, params: { user: { sign_in_token_attempt: 'wrongotp' } }, session: { attempt_user_id: user.id }
|
||||
post :create, params: { user: { sign_in_token_attempt: 'wrongotp' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
|
||||
end
|
||||
|
||||
it 'shows a login error' do
|
||||
@@ -504,7 +504,7 @@ RSpec.describe Auth::SessionsController do
|
||||
|
||||
context 'when requesting a new sign in token' do
|
||||
subject do
|
||||
post :create, params: { sign_in_token_resend: '', user: { sign_in_token_attempt: '' } }, session: { attempt_user_id: user.id }
|
||||
post :create, params: { sign_in_token_resend: '', user: { sign_in_token_attempt: '' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
|
||||
end
|
||||
|
||||
before do
|
||||
|
||||
Reference in new issue
Block a user