Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError: undefined method `group' for <xxxxx> #261

Closed
mackeee-orange opened this issue Apr 9, 2018 · 5 comments
Closed

NoMethodError: undefined method `group' for <xxxxx> #261

mackeee-orange opened this issue Apr 9, 2018 · 5 comments

Comments

@mackeee-orange
Copy link

mackeee-orange commented Apr 9, 2018

Versions:

jwt (2.1.0)
rails (5.1.5)
  actioncable (= 5.1.5)
  actionmailer (= 5.1.5)
  actionpack (= 5.1.5)
  actionview (= 5.1.5)
  activejob (= 5.1.5)
  activemodel (= 5.1.5)
  activerecord (= 5.1.5)
  activesupport (= 5.1.5)
  bundler (>= 1.3.0)
  railties (= 5.1.5)
  sprockets-rails (>= 2.0.0)

I got a error when JWT.encode or JWT.decode with ES256 algorithm.
When I exec them with 'none' algorithm, it spit success.

I show my pry:

[1] pry(main)>     JWT.decode("xxxx.xxxx.xxxx.", "one_pub_key", true, {varify_iat: true, algorithm: 'ES256'})
NoMethodError: undefined method `group' for #<String:0x00007ff408ba7238>
from /Users/mackeee/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/jwt2.1.0/lib/jwt/algos/ecdsa.rb:26:in `verify'
[2] pry(main)> payload = {"admin?" => true, "exp" => 182329552, "iat" => 1523295527, "sid" => "hogehoge_mackeee", "tki" => 0, "uid" => "hogehoge"}
=> {"admin?"=>true, "exp"=>182329552, "iat"=>1523295527, "sid"=>"hogehoge_mackeee", "tki"=>0, "uid"=>"hogehoge"}
[3] pry(main)> token = JWT.encode payload, "one_priv_key", 'ES256'
NoMethodError: undefined method `group' for #<String:0x00007ff408a56f50>
from /Users/mackeee/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/jwt-2.1.0/lib/jwt/algos/ecdsa.rb:15:in `sign'
[4] pry(main)> token = JWT.encode payload.to_s, "one_priv_key", 'ES256'
NoMethodError: undefined method `group' for #<String:0x00007ff4089ff840>
from /Users/mackeee/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/jwt-2.1.0/lib/jwt/algos/ecdsa.rb:15:in `sign'
[5] pry(main)> token = JWT.encode payload, nil, 'none'
=> "yyyy.yyyyy.yyyy."

How can I modify this problem??

@mackeee-orange
Copy link
Author

And I use ruby-v2.4.2

@excpt
Copy link
Member

excpt commented Apr 9, 2018

Hey @mackeee-orange,

Thanks for the detailed reporting.

Please try to give the JWT.encode and JWT.decode methods the raw key object instead of an string.

Example:

pub_key = OpenSSL::PKey.read(File.read('path_to_my_pub_key.pem'))

JWT.decode(payload, pub_key, true, my_options)

@mackeee-orange
Copy link
Author

@excpt Thank you for comment.

Sorry, there is misunderstanding.
I don't have a PEM nor RSA, but only the hex string "04xxx...yyy..." representing the public key.

@ab320012
Copy link
Contributor

ab320012 commented Apr 10, 2018

@mackeee-orange you can generate a public key object from the string in the following way...

# substitute public_key_octet_string with your string
ec = OpenSSL::PKey::EC.new('prime256v1')
key_bn = OpenSSL::BN.new(public_key_octet_string, 2)  # 2 means binary
ec.public_key = OpenSSL::PKey::EC::Point.new(ec.group, key_bn)
#ec.public_key can be passed to JWT.decode

source:
https://stackoverflow.com/questions/22293864/ruby-openssl-convert-elliptic-curve-point-octet-string-into-opensslpkeyec?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

@mackeee-orange
Copy link
Author

thank you @ab320012
I got it by refer your code

hex_string = "04xxxxx.....yyyyy......"
ec = OpenSSL::PKey::EC.new('prime256v1')
key_bn = OpenSSL::BN.new(hex_string, 16)
ec.public_key = OpenSSL::PKey::EC::Point.new(ec.group, key_bn)
JWT.decode @bearer_token, ec, true, { algorithm: 'ES256' } 

@excpt excpt closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants