<commit_before>require 'json/jwt'

module OpenIDConnect
  class ResponseObject
    class IdToken < ConnectObject
      class InvalidToken < Exception; end

      attr_required :iss, :user_id, :aud, :exp, :nonce
      attr_optional :acr, :auth_time

      validates :acr, :inclusion => {:in => [0, 1, 2, 3, 4]}, :allow_nil => true

      def initialize(attributes = {})
        super
        (all_attributes - [:exp]).each do |key|
          self.send "#{key}=", self.send(key).try(:to_s)
        end
        @exp = @exp.to_i
      end

      def verify!(expected = {})
        exp.to_i >= Time.now.to_i &&
        iss == expected[:issuer] &&
        aud == expected[:client_id] &&
        nonce == expected[:nonce] or
        raise InvalidToken.new('Invalid ID Token')
      end

      include JWTnizable
      class << self
        def decode(jwt_string, key_or_client)
          case key_or_client
          when Client
            OpenIDConnect::AccessToken.new(
              :client => key_or_client,
              :access_token => jwt_string
            ).id_token!
          else
            new JSON::JWT.decode(jwt_string, key_or_client)
          end
        end
      end
    end
  end
end<commit_msg>id token validation fix<commit_after>require 'json/jwt'

module OpenIDConnect
  class ResponseObject
    class IdToken < ConnectObject
      class InvalidToken < Exception; end

      attr_required :iss, :user_id, :aud, :exp, :nonce
      attr_optional :acr, :auth_time

      def initialize(attributes = {})
        super
        (all_attributes - [:exp, :auth_time]).each do |key|
          self.send "#{key}=", self.send(key).try(:to_s)
        end
        @exp = @exp.to_i
      end

      def verify!(expected = {})
        exp.to_i >= Time.now.to_i &&
        iss == expected[:issuer] &&
        aud == expected[:client_id] &&
        nonce == expected[:nonce] or
        raise InvalidToken.new('Invalid ID Token')
      end

      include JWTnizable
      class << self
        def decode(jwt_string, key_or_client)
          case key_or_client
          when Client
            OpenIDConnect::AccessToken.new(
              :client => key_or_client,
              :access_token => jwt_string
            ).id_token!
          else
            new JSON::JWT.decode(jwt_string, key_or_client)
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10744500846023688, 'fraction_numerical': 0.0021150592216582064, 'mean_word_length': 1.941542288557214, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, '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': '4618302', 'n_tokens_mistral': 728, 'n_tokens_neox': 719, 'n_words': 172}