Class: JWT::JWK::HMAC

Inherits:
KeyBase show all
Defined in:
lib/jwt/jwk/hmac.rb

Constant Summary collapse

KTY =
'oct'
KTYS =
[KTY, String, JWT::JWK::HMAC].freeze
HMAC_PUBLIC_KEY_ELEMENTS =
%i[kty].freeze
HMAC_PRIVATE_KEY_ELEMENTS =
%i[k].freeze
HMAC_KEY_ELEMENTS =
(HMAC_PRIVATE_KEY_ELEMENTS + HMAC_PUBLIC_KEY_ELEMENTS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KeyBase

#<=>, #==, #[], #hash, inherited, #kid

Constructor Details

#initialize(key, params = nil, options = {}) ⇒ HMAC

Returns a new instance of HMAC.



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

def initialize(key, params = nil, options = {})
  params ||= {}

  # For backwards compatibility when kid was a String
  params = { kid: params } if params.is_a?(String)

  key_params = extract_key_params(key)

  params = params.transform_keys(&:to_sym)
  check_jwk(key_params, params)

  super(options, key_params.merge(params))
end

Class Method Details

.import(jwk_data) ⇒ Object



97
98
99
# File 'lib/jwt/jwk/hmac.rb', line 97

def import(jwk_data)
  new(jwk_data)
end

Instance Method Details

#[]=(key, value) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/jwt/jwk/hmac.rb', line 63

def []=(key, value)
  if HMAC_KEY_ELEMENTS.include?(key.to_sym)
    raise ArgumentError, 'cannot overwrite cryptographic key attributes'
  end

  super(key, value)
end

#export(options = {}) ⇒ Object



47
48
49
50
51
# File 'lib/jwt/jwk/hmac.rb', line 47

def export(options = {})
  exported = parameters.clone
  exported.reject! { |k, _| HMAC_PRIVATE_KEY_ELEMENTS.include? k } unless private? && options[:include_private] == true
  exported
end

#key_digestObject



57
58
59
60
61
# File 'lib/jwt/jwk/hmac.rb', line 57

def key_digest
  sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::UTF8String.new(signing_key),
                                      OpenSSL::ASN1::UTF8String.new(KTY)])
  OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end

#keypairObject



26
27
28
# File 'lib/jwt/jwk/hmac.rb', line 26

def keypair
  secret
end

#membersObject



53
54
55
# File 'lib/jwt/jwk/hmac.rb', line 53

def members
  HMAC_KEY_ELEMENTS.each_with_object({}) { |i, h| h[i] = self[i] }
end

#private?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/jwt/jwk/hmac.rb', line 30

def private?
  true
end

#public_keyObject



34
35
36
# File 'lib/jwt/jwk/hmac.rb', line 34

def public_key
  nil
end

#signing_keyObject



42
43
44
# File 'lib/jwt/jwk/hmac.rb', line 42

def signing_key
  secret
end

#verify_keyObject



38
39
40
# File 'lib/jwt/jwk/hmac.rb', line 38

def verify_key
  secret
end