Module: JWT::Signature

Extended by:
Signature
Included in:
Signature
Defined in:
lib/jwt/signature.rb

Overview

Signature logic for JWT

Defined Under Namespace

Classes: ToSign, ToVerify

Constant Summary collapse

ALGOS =
[
  Algos::Hmac,
  Algos::Ecdsa,
  Algos::Rsa,
  Algos::Eddsa,
  Algos::Unsupported
].freeze

Instance Method Summary collapse

Instance Method Details

#sign(algorithm, msg, key) ⇒ Object



31
32
33
34
35
36
# File 'lib/jwt/signature.rb', line 31

def sign(algorithm, msg, key)
  algo = ALGOS.find do |alg|
    alg.const_get(:SUPPORTED).include? algorithm
  end
  algo.sign ToSign.new(algorithm, msg, key)
end

#verify(algorithm, key, signing_input, signature) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jwt/signature.rb', line 38

def verify(algorithm, key, signing_input, signature)
  algo = ALGOS.find do |alg|
    alg.const_get(:SUPPORTED).include? algorithm
  end
  verified = algo.verify(ToVerify.new(algorithm, key, signing_input, signature))
  raise(JWT::VerificationError, 'Signature verification raised') unless verified
rescue OpenSSL::PKey::PKeyError
  raise JWT::VerificationError, 'Signature verification raised'
ensure
  OpenSSL.errors.clear
end