undefined method 'verify'

368 views
Skip to first unread message

whitehorsesoft

unread,
Aug 21, 2018, 2:44:11 PM8/21/18
to ruby-jwt
With a certain token, if I run:
JWT.decode token, nil, false

... I get a decoded token as expected. the type ('typ') is "JWT", the 'alg' is "RS256", and the expected public key is in "kid".

However, if I run
JWT.decode token, rsa_public_key, true, { algorithm: 'RS256' }

... with the same token, and for "rsa_public_key" the public key in "kid" results from first run, I get a failure:

Traceback (most recent call last):
       16: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
       15: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:27:in `dispatch'
       14: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
       13: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
       12: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
       11: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:424:in `exec'
       10: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:28:in `run'
        9: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `kernel_load'
        8: from _hiddenpath_/.gem/ruby/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `load'
        7: from /usr/bin/irb:11:in `<top (required)>'
        6: from (irb):5
        5: from _hiddenpath2_/vendor/bundle/ruby/2.5.0/gems/jwt-2.1.0/lib/jwt.rb:32:in `decode'
        4: from _hiddenpath2_/vendor/bundle/ruby/2.5.0/gems/jwt-2.1.0/lib/jwt.rb:47:in `decode_verify_signature'
        3: from _hiddenpath2_/vendor/bundle/ruby/2.5.0/gems/jwt-2.1.0/lib/jwt/signature.rb:42:in `verify'
        2: from _hiddenpath2_/vendor/bundle/ruby/2.5.0/gems/jwt-2.1.0/lib/jwt/algos/rsa.rb:15:in `verify'
        1: from _hiddenpath2_/vendor/bundle/ruby/2.5.0/gems/jwt-2.1.0/lib/jwt/security_utils.rb:20:in `verify_rsa'
NoMethodError (undefined method `verify' for "_my_public_rsa":String)

Any suggestions on what is wrong? This happens in both irb and when running ruby scripts outside of irb. I have run 'bundle install', and of course required 'jwt' in all situations. Everything else besides this seems to be in order.

whitehorsesoft

unread,
Aug 21, 2018, 3:12:04 PM8/21/18
to ruby-jwt
After looking into more, I found that it was because I wasn't passing in a proper public key. When creating the public key in the following manner everything performs as expected:
cert = OpenSSL::X509::Certificate.new(my_pem_string)
rsa_public_key
= cert.public_key
Reply all
Reply to author
Forward
0 new messages