split login and trusted_device logic

This commit is contained in:
simon lehericey 2019-02-01 17:17:10 +01:00
parent aebd3ff670
commit bee9a108c5
6 changed files with 143 additions and 82 deletions

View file

@ -1,4 +1,6 @@
class ApplicationController < ActionController::Base
include TrustedDeviceConcern
MAINTENANCE_MESSAGE = 'Le site est actuellement en maintenance. Il sera à nouveau disponible dans un court instant.'
# Prevent CSRF attacks by raising an exception.
@ -6,6 +8,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, if: -> { !Rails.env.test? }
before_action :load_navbar_left_pannel_partial_url
before_action :set_raven_context
before_action :redirect_if_untrusted
before_action :authorize_request_for_profiler
before_action :reject, if: -> { Flipflop.maintenance_mode? }
@ -151,4 +154,30 @@ class ApplicationController < ActionController::Base
redirect_to root_path
end
end
def redirect_if_untrusted
if gestionnaire_signed_in? &&
sensitive_path &&
current_gestionnaire.feature_enabled?(:enable_email_login_token) &&
!trusted_device?
send_login_token_or_bufferize(current_gestionnaire)
redirect_to link_sent_path(email: current_gestionnaire.email)
end
end
def sensitive_path
path = request.path_info
if path == '/' ||
path == '/users/sign_out' ||
path.start_with?('/connexion-par-jeton') ||
path.start_with?('/api/') ||
path.start_with?('/lien-envoye')
false
else
true
end
end
end

View file

@ -23,20 +23,7 @@ class Users::SessionsController < Sessions::SessionsController
current_user.update(loged_in_with_france_connect: nil)
end
if gestionnaire_signed_in?
if trusted_device? || !current_gestionnaire.feature_enabled?(:enable_email_login_token)
set_flash_message :notice, :signed_in
redirect_to after_sign_in_path_for(:user)
else
gestionnaire = current_gestionnaire
send_login_token_or_bufferize(gestionnaire)
[:user, :gestionnaire, :administrateur].each { |role| sign_out(role) }
redirect_to link_sent_path(email: gestionnaire.email)
end
elsif user_signed_in?
if gestionnaire_signed_in? || user_signed_in?
set_flash_message :notice, :signed_in
redirect_to after_sign_in_path_for(:user)
else
@ -87,28 +74,24 @@ class Users::SessionsController < Sessions::SessionsController
trust_device
flash.notice = "Merci davoir confirmé votre connexion. Votre navigateur est maintenant authentifié pour #{TRUSTED_DEVICE_PERIOD.to_i / ActiveSupport::Duration::SECONDS_PER_DAY} jours."
user = User.find_by(email: gestionnaire.email)
administrateur = Administrateur.find_by(email: gestionnaire.email)
[user, gestionnaire, administrateur].compact.each { |resource| sign_in(resource) }
# redirect to procedure'url if stored by store_location_for(:user) in dossiers_controller
# redirect to root_path otherwise
redirect_to after_sign_in_path_for(:user)
if gestionnaire_signed_in?
redirect_to after_sign_in_path_for(:user)
else
redirect_to new_user_session_path
end
else
flash[:alert] = 'Votre lien est invalide ou expiré, veuillez-vous reconnecter.'
redirect_to new_user_session_path
flash[:alert] = 'Votre lien est invalide ou expiré, un nouveau vient de vous être envoyé.'
send_login_token_or_bufferize(gestionnaire)
redirect_to link_sent_path(email: gestionnaire.email)
end
end
private
def send_login_token_or_bufferize(gestionnaire)
if !gestionnaire.young_login_token?
login_token = gestionnaire.login_token!
GestionnaireMailer.send_login_token(gestionnaire, login_token).deliver_later
end
end
def try_to_authenticate(klass, remember_me = false)
resource = klass.find_for_database_authentication(email: params[:user][:email])

View file

@ -17,6 +17,13 @@ module TrustedDeviceConcern
(Time.zone.now - TRUSTED_DEVICE_PERIOD) < trusted_device_cookie_created_at
end
def send_login_token_or_bufferize(gestionnaire)
if !gestionnaire.young_login_token?
login_token = gestionnaire.login_token!
GestionnaireMailer.send_login_token(gestionnaire, login_token).deliver_later
end
end
private
def trusted_device_cookie_created_at