valid period depend on trusted_device_token.created_at

This commit is contained in:
simon lehericey 2019-02-04 11:57:50 +01:00
parent d664f130fd
commit 7de3a18fd1
3 changed files with 10 additions and 8 deletions

View file

@ -75,8 +75,11 @@ class Users::SessionsController < Sessions::SessionsController
.find_by(token: params[:jeton])
if trusted_device_token&.token_valid?
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."
trust_device(trusted_device_token.created_at)
period = ((trusted_device_token.created_at + TRUSTED_DEVICE_PERIOD) - Time.zone.now).to_i / ActiveSupport::Duration::SECONDS_PER_DAY
flash.notice = "Merci davoir confirmé votre connexion. Votre navigateur est maintenant authentifié pour #{period} jours."
# redirect to procedure'url if stored by store_location_for(:user) in dossiers_controller
# redirect to root_path otherwise

View file

@ -4,10 +4,10 @@ module TrustedDeviceConcern
TRUSTED_DEVICE_COOKIE_NAME = :trusted_device
TRUSTED_DEVICE_PERIOD = 1.month
def trust_device
def trust_device(start_at)
cookies.encrypted[TRUSTED_DEVICE_COOKIE_NAME] = {
value: JSON.generate({ created_at: Time.zone.now }),
expires: TRUSTED_DEVICE_PERIOD,
value: JSON.generate({ created_at: start_at }),
expires: start_at + TRUSTED_DEVICE_PERIOD,
httponly: true
}
end

View file

@ -239,9 +239,8 @@ describe Users::SessionsController, type: :controller do
context 'when the cookie is outdated' do
before do
Timecop.freeze(Time.zone.now - TrustedDeviceConcern::TRUSTED_DEVICE_PERIOD - 1.minute)
controller.trust_device(Time.zone.now)
Timecop.return
emission_date = Time.zone.now - TrustedDeviceConcern::TRUSTED_DEVICE_PERIOD - 1.minute
controller.trust_device(emission_date)
end
it { is_expected.to be false }