Sha256: 38b65122ab68293288a31c89ac2237d47018ece7fffa3cf78183b765094a4e3d

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_relative 'claims/audience'
require_relative 'claims/expiration'
require_relative 'claims/issued_at'
require_relative 'claims/issuer'
require_relative 'claims/jwt_id'
require_relative 'claims/not_before'
require_relative 'claims/numeric'
require_relative 'claims/required'
require_relative 'claims/subject'

module JWT
  module Claims
    VerificationContext = Struct.new(:payload, keyword_init: true)

    VERIFIERS = {
      verify_expiration: ->(options) { Claims::Expiration.new(leeway: options[:exp_leeway] || options[:leeway]) },
      verify_not_before: ->(options) { Claims::NotBefore.new(leeway: options[:nbf_leeway] || options[:leeway]) },
      verify_iss: ->(options) { options[:iss] && Claims::Issuer.new(issuers: options[:iss]) },
      verify_iat: ->(*) { Claims::IssuedAt.new },
      verify_jti: ->(options) { Claims::JwtId.new(validator: options[:verify_jti]) },
      verify_aud: ->(options) { options[:aud] && Claims::Audience.new(expected_audience: options[:aud]) },
      verify_sub: ->(options) { options[:sub] && Claims::Subject.new(expected_subject: options[:sub]) },
      required_claims: ->(options) { Claims::Required.new(required_claims: options[:required_claims]) }
    }.freeze

    class << self
      def verify!(payload, options)
        VERIFIERS.each do |key, verifier_builder|
          next unless options[key]

          verifier_builder&.call(options)&.verify!(context: VerificationContext.new(payload: payload))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jwt-2.9.1 lib/jwt/claims.rb