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 https://tools.ietf.org/html/rfc7517

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Set.



15
16
17
18
19
20
21
22
23
24
# File 'lib/jwt/jwk/set.rb', line 15

def initialize(jwks = nil, options = {})
  @keys = case jwks
          when nil               then []
          when JWT::JWK::Set     then jwks.keys.dup
          when JWT::JWK::KeyBase then [jwks]
          when Hash              then build_supported_keys(jwks.transform_keys(&:to_sym)[:keys], options)
          when Array             then build_keys(jwks, 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

#initialize_copy(other) ⇒ Object

Ensures a duplicated set owns its key collection. The keys themselves are intentionally shared; only the collection is copied.



28
29
30
31
# File 'lib/jwt/jwk/set.rb', line 28

def initialize_copy(other)
  super
  @keys = @keys.dup
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