2024-09-24 17:03:18 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module APIEntrepriseTokenConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
SOON_TO_EXPIRE_DELAY = 1.month
|
|
|
|
|
|
|
|
included do
|
|
|
|
validates :api_entreprise_token, jwt_token: true, allow_blank: true
|
2024-09-26 15:13:51 +02:00
|
|
|
|
2024-09-24 17:03:18 +02:00
|
|
|
before_save :set_api_entreprise_token_expires_at, if: :will_save_change_to_api_entreprise_token?
|
|
|
|
|
|
|
|
def api_entreprise_role?(role)
|
|
|
|
APIEntrepriseToken.new(api_entreprise_token).role?(role)
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_entreprise_token
|
|
|
|
self[:api_entreprise_token].presence || Rails.application.secrets.api_entreprise[:key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_entreprise_token_expired?
|
|
|
|
APIEntrepriseToken.new(api_entreprise_token).expired?
|
|
|
|
end
|
|
|
|
|
2024-09-24 18:59:18 +02:00
|
|
|
def api_entreprise_token_expired_or_expires_soon?
|
2024-09-24 17:03:18 +02:00
|
|
|
api_entreprise_token_expires_at && api_entreprise_token_expires_at <= SOON_TO_EXPIRE_DELAY.from_now
|
|
|
|
end
|
|
|
|
|
2024-09-26 15:13:51 +02:00
|
|
|
def has_custom_api_entreprise_token?
|
|
|
|
self[:api_entreprise_token].present?
|
|
|
|
end
|
|
|
|
|
2024-09-24 17:03:18 +02:00
|
|
|
def set_api_entreprise_token_expires_at
|
2024-09-24 18:33:35 +02:00
|
|
|
self.api_entreprise_token_expires_at = has_custom_api_entreprise_token? ? APIEntrepriseToken.new(api_entreprise_token).expiration : nil
|
2024-09-24 17:03:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|