demarches-normaliennes/app/models/concerns/trusted_device_concern.rb

37 lines
974 B
Ruby
Raw Normal View History

2018-10-30 18:24:29 +01:00
module TrustedDeviceConcern
extend ActiveSupport::Concern
TRUSTED_DEVICE_COOKIE_NAME = :trusted_device
TRUSTED_DEVICE_PERIOD = 1.month
def trust_device(start_at)
2018-10-30 18:24:29 +01:00
cookies.encrypted[TRUSTED_DEVICE_COOKIE_NAME] = {
value: JSON.generate({ created_at: start_at }),
expires: start_at + TRUSTED_DEVICE_PERIOD,
2018-10-30 18:24:29 +01:00
httponly: true
}
end
def trusted_device?
trusted_device_cookie.present? &&
(Time.zone.now - TRUSTED_DEVICE_PERIOD) < trusted_device_cookie_created_at
2018-10-30 18:24:29 +01:00
end
2019-02-01 17:17:10 +01:00
def send_login_token_or_bufferize(gestionnaire)
if !gestionnaire.young_login_token?
login_token = gestionnaire.create_trusted_device_token
2019-02-01 17:17:10 +01:00
GestionnaireMailer.send_login_token(gestionnaire, login_token).deliver_later
end
end
2018-10-30 18:24:29 +01:00
private
def trusted_device_cookie_created_at
Time.zone.parse(JSON.parse(trusted_device_cookie)['created_at'])
end
2018-10-30 18:24:29 +01:00
def trusted_device_cookie
cookies.encrypted[TRUSTED_DEVICE_COOKIE_NAME]
end
end