Class: JWT::JWK::Set

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/jwt/jwk/set.rb

Overview

JSON Web Key Set (JWKS) representation tools.ietf.org/html/rfc7517

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwks = nil, options = {}) ⇒ Set

rubocop:disable Metrics/CyclomaticComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jwt/jwk/set.rb', line 15

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

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



13
14
15
# File 'lib/jwt/jwk/set.rb', line 13

def keys
  @keys
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



69
70
71
# File 'lib/jwt/jwk/set.rb', line 69

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

#add(key) ⇒ Object Also known as: <<



64
65
66
67
# File 'lib/jwt/jwk/set.rb', line 64

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

#export(options = {}) ⇒ Object



33
34
35
# File 'lib/jwt/jwk/set.rb', line 33

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

#merge(enum) ⇒ Object



55
56
57
58
# File 'lib/jwt/jwk/set.rb', line 55

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

#reject!(&block) ⇒ Object



45
46
47
48
49
# File 'lib/jwt/jwk/set.rb', line 45

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

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

#select!(&block) ⇒ Object Also known as: filter!



39
40
41
42
43
# File 'lib/jwt/jwk/set.rb', line 39

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

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

#union(enum) ⇒ Object Also known as: |, +



60
61
62
# File 'lib/jwt/jwk/set.rb', line 60

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

#uniq!(&block) ⇒ Object



51
52
53
# File 'lib/jwt/jwk/set.rb', line 51

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