Sha256: 91af84827cbfd95f5e534fa4d65d51606a715c270d1a87244bd423bebfdcb945

Contents?: true

Size: 683 Bytes

Versions: 2

Compression:

Stored size: 683 Bytes

Contents

require_relative './error'

module JWT
  class ClaimsValidator
    NUMERIC_CLAIMS = %i[
      exp
      iat
      nbf
    ].freeze

    def initialize(payload)
      @payload = payload.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
    end

    def validate!
      validate_numeric_claims

      true
    end

    private

    def validate_numeric_claims
      NUMERIC_CLAIMS.each do |claim|
        validate_is_numeric(claim) if @payload.key?(claim)
      end
    end

    def validate_is_numeric(claim)
      return if @payload[claim].is_a?(Numeric)

      raise InvalidPayload, "#{claim} claim must be a Numeric value but it is a #{@payload[claim].class}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jwt-2.3.0 lib/jwt/claims_validator.rb
jwt-2.2.3 lib/jwt/claims_validator.rb