demarches-normaliennes/app/models/api_entreprise_token.rb

41 lines
776 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-08-05 18:40:47 +02:00
class APIEntrepriseToken
TokenError = Class.new(StandardError)
def initialize(token)
@token = token
end
def token
raise TokenError, I18n.t("api_entreprise.errors.missing_token") if @token.blank?
@token
end
def expired?
decoded_token.key?("exp") && decoded_token["exp"] <= Time.zone.now.to_i
end
def expiration
2024-09-27 16:49:23 +02:00
decoded_token.key?("exp") && Time.zone.at(decoded_token["exp"])
end
def role?(role)
roles.include?(role)
end
private
def roles
Array(decoded_token["roles"] || decoded_token["scopes"])
end
def decoded_token
@decoded_token ||= {}
@decoded_token[token] ||= JWT.decode(token, nil, false)[0]
rescue JWT::DecodeError => e
raise TokenError, e.message
end
end