class JWT::JWK::HMAC

Constants

HMAC_KEY_ELEMENTS
HMAC_PRIVATE_KEY_ELEMENTS
HMAC_PUBLIC_KEY_ELEMENTS
KTY
KTYS

Public Class Methods

new(key, params = nil, options = {}) click to toggle source
Calls superclass method
# 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

Private Class Methods

import(jwk_data) click to toggle source
# File lib/jwt/jwk/hmac.rb, line 97
def import(jwk_data)
  new(jwk_data)
end

Public Instance Methods

[]=(key, value) click to toggle source
Calls superclass method
# 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 = {}) click to toggle source

See tools.ietf.org/html/rfc7517#appendix-A.3

# 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_digest() click to toggle source
# 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
keypair() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 26
def keypair
  secret
end
members() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 53
def members
  HMAC_KEY_ELEMENTS.each_with_object({}) { |i, h| h[i] = self[i] }
end
private?() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 30
def private?
  true
end
public_key() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 34
def public_key
  nil
end
signing_key() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 42
def signing_key
  secret
end
verify_key() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 38
def verify_key
  secret
end

Private Instance Methods

check_jwk(keypair, params) click to toggle source
# File lib/jwt/jwk/hmac.rb, line 90
def check_jwk(keypair, params)
  raise ArgumentError, 'cannot overwrite cryptographic key attributes' unless (HMAC_KEY_ELEMENTS & params.keys).empty?
  raise JWT::JWKError, "Incorrect 'kty' value: #{keypair[:kty]}, expected #{KTY}" unless keypair[:kty] == KTY
  raise JWT::JWKError, 'Key format is invalid for HMAC' unless keypair[:k]
end
extract_key_params(key) click to toggle source
# File lib/jwt/jwk/hmac.rb, line 77
def extract_key_params(key)
  case key
  when JWT::JWK::HMAC
    key.export(include_private: true)
  when String # Accept String key as input
    { kty: KTY, k: key }
  when Hash
    key.transform_keys(&:to_sym)
  else
    raise ArgumentError, 'key must be of type String or Hash with key parameters'
  end
end
secret() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 73
def secret
  self[:k]
end