Sha256: 726890c8bf8d53a672d4d098eea4dc8184f2f658a691dacfb1d8ce385302c4a7

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module JWT
  module JWK
    class Set
      include Enumerable
      extend Forwardable

      attr_reader :keys

      def initialize(jwks = nil, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
        jwks ||= {}

        @keys = case jwks
                when JWT::JWK::Set # Simple duplication
                  jwks.keys
                when JWT::JWK::KeyBase # Singleton
                  [jwks]
                when Hash
                  jwks = jwks.transform_keys(&:to_sym)
                  [*jwks[:keys]].map { |k| JWT::JWK.new(k, nil, options) }
                when Array
                  jwks.map { |k| JWT::JWK.new(k, nil, options) }
                else
                  raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
        end
      end

      def export(options = {})
        { keys: @keys.map { |k| k.export(options) } }
      end

      def_delegators :@keys, :each, :size, :delete, :dig

      def select!(&block)
        return @keys.select! unless block

        self if @keys.select!(&block)
      end

      def reject!(&block)
        return @keys.reject! unless block

        self if @keys.reject!(&block)
      end

      def uniq!(&block)
        self if @keys.uniq!(&block)
      end

      def merge(enum)
        @keys += JWT::JWK::Set.new(enum.to_a).keys
        self
      end

      def union(enum)
        dup.merge(enum)
      end

      def add(key)
        @keys << JWT::JWK.new(key)
        self
      end

      def ==(other)
        other.is_a?(JWT::JWK::Set) && keys.sort == other.keys.sort
      end

      alias eql? ==
      alias filter! select!
      alias length size
      # For symbolic manipulation
      alias | union
      alias + union
      alias << add
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
jwt-2.8.1 lib/jwt/jwk/set.rb
jwt-2.8.0 lib/jwt/jwk/set.rb
jwt-2.7.1 lib/jwt/jwk/set.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/jwt-2.7.0/lib/jwt/jwk/set.rb
jwt-2.7.0 lib/jwt/jwk/set.rb
jwt-2.6.0 lib/jwt/jwk/set.rb