2018-10-30 18:24:29 +01:00
|
|
|
module TrustedDeviceConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
TRUSTED_DEVICE_COOKIE_NAME = :trusted_device
|
|
|
|
TRUSTED_DEVICE_PERIOD = 1.month
|
|
|
|
|
2019-02-04 11:57:50 +01:00
|
|
|
def trust_device(start_at)
|
2018-10-30 18:24:29 +01:00
|
|
|
cookies.encrypted[TRUSTED_DEVICE_COOKIE_NAME] = {
|
2019-02-04 11:57:50 +01:00
|
|
|
value: JSON.generate({ created_at: start_at }),
|
|
|
|
expires: start_at + TRUSTED_DEVICE_PERIOD,
|
2024-07-03 11:04:16 +02:00
|
|
|
httponly: true,
|
|
|
|
secure: Rails.env.production?
|
2018-10-30 18:24:29 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def trusted_device?
|
|
|
|
trusted_device_cookie.present? &&
|
2019-02-01 10:54:20 +01:00
|
|
|
(Time.zone.now - TRUSTED_DEVICE_PERIOD) < trusted_device_cookie_created_at
|
2018-10-30 18:24:29 +01:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def send_login_token_or_bufferize(instructeur)
|
|
|
|
if !instructeur.young_login_token?
|
2023-08-29 16:41:45 +02:00
|
|
|
token = instructeur.create_trusted_device_token
|
|
|
|
InstructeurMailer.send_login_token(instructeur, token).deliver_later
|
2023-08-30 10:44:59 +02:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
2019-02-01 17:17:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 18:24:29 +01:00
|
|
|
private
|
|
|
|
|
2019-02-01 10:54:20 +01:00
|
|
|
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
|