2024-09-13 16:52:42 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-09-13 16:47:31 +02:00
|
|
|
require "administrate/field/base"
|
|
|
|
|
|
|
|
class JwtField < Administrate::Field::Base
|
|
|
|
def to_s
|
|
|
|
if data.present?
|
|
|
|
begin
|
|
|
|
decoded_token = JWT.decode(data, nil, false)
|
2024-09-17 09:47:46 +02:00
|
|
|
|
|
|
|
return "Token présent, sans expiration" unless decoded_token[0].key?('exp')
|
2024-09-13 16:47:31 +02:00
|
|
|
|
2024-09-17 14:21:54 +02:00
|
|
|
expiration = Time.zone.at(decoded_token[0]['exp'])
|
2024-09-13 16:47:31 +02:00
|
|
|
if expiration < Time.zone.now
|
|
|
|
"Token présent, expiré le #{expiration.strftime('%d/%m/%Y à %H:%M')}"
|
|
|
|
else
|
|
|
|
"Token présent, expirera le #{expiration.strftime('%d/%m/%Y à %H:%M')}"
|
|
|
|
end
|
|
|
|
rescue JWT::DecodeError => e
|
|
|
|
"Token invalide : #{e.message}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
"Pas de token"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|