class JWT::JWK::HMAC

Constants

KTY
KTYS

Public Class Methods

new(keypair, kid = nil) click to toggle source
Calls superclass method
# File lib/jwt/jwk/hmac.rb, line 9
def initialize(keypair, kid = nil)
  raise ArgumentError, 'keypair must be of type String' unless keypair.is_a?(String)

  super
  @kid = kid || generate_kid
end

Private Class Methods

import(jwk_data) click to toggle source
# File lib/jwt/jwk/hmac.rb, line 47
def import(jwk_data)
  jwk_k = jwk_data[:k] || jwk_data['k']
  jwk_kid = jwk_data[:kid] || jwk_data['kid']

  raise JWT::JWKError, 'Key format is invalid for HMAC' unless jwk_k

  self.new(jwk_k, jwk_kid)
end

Public Instance Methods

export(options = {}) click to toggle source

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

# File lib/jwt/jwk/hmac.rb, line 25
def export(options = {})
  exported_hash = {
    kty: KTY,
    kid: kid
  }

  return exported_hash unless private? && options[:include_private] == true

  exported_hash.merge(
    k: keypair
  )
end
private?() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 16
def private?
  true
end
public_key() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 20
def public_key
  nil
end

Private Instance Methods

generate_kid() click to toggle source
# File lib/jwt/jwk/hmac.rb, line 40
def generate_kid
  sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::UTF8String.new(keypair),
                                      OpenSSL::ASN1::UTF8String.new(KTY)])
  OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end