<commit_before># frozen_string_literal: true

require 'jwt'

module Devise
  module Jwt
    # Encode/decode a token, adding some configurable claims to the payload
    class TokenCoder
      ALG = 'HS256'

      def self.encode(payload, config)
        secret = config.secret
        expiration_time = config.expiration_time
        new.send(:encode, payload, secret, expiration_time)
      end

      def self.decode(token, config)
        secret = config.secret
        blacklist = config.blacklist
        new.send(:decode, token, secret, blacklist)
      end

      private

      def encode(payload, secret, expiration_time)
        payload_to_encode = base_payload(expiration_time).merge(payload)
        ::JWT.encode(payload_to_encode, secret, ALG)
      end

      def decode(token, secret, blacklist)
        payload = ::JWT.decode(token, secret, true,
                               algorithm: ALG,
                               verify_jti: true)[0]
        check_in_blacklist(payload, blacklist) if blacklist
        payload
      end

      def check_in_blacklist(payload, blacklist)
        raise JWT::DecodeError if blacklist.include?(payload['jti'])
      end

      # :reek:UtilityFunction
      def base_payload(expiration_time)
        now = Time.now.to_i
        {
          iat: now,
          exp: now + expiration_time,
          jti: SecureRandom.uuid
        }
      end
    end<commit_msg>Fix name collision with expected method in blacklist<commit_after># frozen_string_literal: true

require 'jwt'

module Devise
  module Jwt
    # Encode/decode a token, adding some configurable claims to the payload
    class TokenCoder
      ALG = 'HS256'

      def self.encode(payload, config)
        secret = config.secret
        expiration_time = config.expiration_time
        new.send(:encode, payload, secret, expiration_time)
      end

      def self.decode(token, config)
        secret = config.secret
        blacklist = config.blacklist
        new.send(:decode, token, secret, blacklist)
      end

      private

      def encode(payload, secret, expiration_time)
        payload_to_encode = base_payload(expiration_time).merge(payload)
        ::JWT.encode(payload_to_encode, secret, ALG)
      end

      def decode(token, secret, blacklist)
        payload = ::JWT.decode(token, secret, true,
                               algorithm: ALG,
                               verify_jti: true)[0]
        check_in_blacklist(payload, blacklist) if blacklist
        payload
      end

      def check_in_blacklist(payload, blacklist)
        raise JWT::DecodeError if blacklist.member?(payload['jti'])
      end

      # :reek:UtilityFunction
      def base_payload(expiration_time)
        now = Time.now.to_i
        {
          iat: now,
          exp: now + expiration_time,
          jti: SecureRandom.uuid
        }
      end
    end
{'fraction_non_alphanumeric': 0.07740585774058577, 'fraction_numerical': 0.002789400278940028, 'mean_word_length': 2.1527472527472526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3370655', 'n_tokens_mistral': 829, 'n_tokens_neox': 804, 'n_words': 202}