demarches-normaliennes/app/models/concerns/api_entreprise_token_concern.rb
2024-10-15 14:40:49 +02:00

36 lines
1.1 KiB
Ruby

# 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
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 has_custom_api_entreprise_token?
self[:api_entreprise_token].present?
end
def api_entreprise_token_expired?
APIEntrepriseToken.new(api_entreprise_token).expired?
end
def api_entreprise_token_expired_or_expires_soon?
api_entreprise_token_expires_at && api_entreprise_token_expires_at <= SOON_TO_EXPIRE_DELAY.from_now
end
def set_api_entreprise_token_expires_at
self.api_entreprise_token_expires_at = has_custom_api_entreprise_token? ? APIEntrepriseToken.new(api_entreprise_token).expiration : nil
end
end
end