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

TypeError: no implicit conversion of String into Integer #188

Closed
abmBispo opened this issue Feb 7, 2017 · 4 comments
Closed

TypeError: no implicit conversion of String into Integer #188

abmBispo opened this issue Feb 7, 2017 · 4 comments
Assignees
Milestone

Comments

@abmBispo
Copy link

abmBispo commented Feb 7, 2017

I had some problems handling this library concerning insert an array into JWT.encode, suposing I have a JSON in array format. Testing on padrino IRB, I got this error:

TypeError: no implicit conversion of String into Integer
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jwt-1.5.6/lib/jwt.rb:88:in []'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jwt-1.5.6/lib/jwt.rb:88:in encoded_payload'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jwt-1.5.6/lib/jwt.rb:105:in encode'
	from (irb):12
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/padrino-core-0.13.3.3/lib/padrino-core/cli/base.rb:43:in console'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:in run'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in invoke_command'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:in dispatch'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/base.rb:444:in start'
	from /home/alan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/padrino-core-0.13.3.3/bin/padrino:9:in <top (required)>'
	from /home/alan/Documentos/git/phschool/backend/api/bin/padrino:17:in load'
	from /home/alan/Documentos/git/phschool/backend/api/bin/padrino:17:in <main>'

I have tested the command jwt = JWT.encode([{"id"=>1}],'!chave','HS256') an it raised. What is the best way to handle this error? In the official web page of JWT is possible to convert directly from JSON array to JWT, so why I can't deal with this gem just like that?

@excpt
Copy link
Member

excpt commented Feb 8, 2017

The JWT.encode method expects a hash of claims. You should simply get the first value of the array and put that in as a parameter. There is no other way around. This is simply the current state of the ruby-jwt implementation.

And by the official page of the JWT RFC is here: https://tools.ietf.org/html/rfc7519

JWT.io is a website provided by Auth0, a company that provides a service that is focused around JWT and authentication.

@mgwidmann
Copy link

This broke in version 1.5.5 and was not backwards compatible with 1.5.4

@RedFred7
Copy link

I have a similar problem. The cause is in the implementation of #encode_json. In initial versions (I use 1.5.0) it used MultiJson.encode. Later on it started using JSON.generate, which is much more inflexible regarding types, as it expects a Hash-like or JSON::State-like object.

The thing is, patch version upgrades shouldn't really break backwards compatibility. If there is no critical reason why JSON.generate is used instead of MultiJson.encode, maybe it could be put back in next version?

@excpt excpt added the bug label Apr 12, 2017
@bunnrf
Copy link

bunnrf commented Jun 12, 2017

This is caused by line 30 in encode.rb:

raise InvalidPayload, 'exp claim must be an integer' if payload && payload['exp'] && payload['exp'].is_a?(Time)

If payload is an array, the type error is raised when it tries to index in with the string 'exp'. This could be patched by checking if payload is a Hash, since the next statement,

Encode.base64url_encode(JSON.generate(payload))

does accept an array.

$ rails c
Loading development environment (Rails 4.2.2)
2.3.1 :001 > def JWT::encoded_payload(payload)
2.3.1 :002?>   raise InvalidPayload, 'exp claim must be an integer' if payload && payload.is_a?(Hash) && payload['exp'] && payload['exp'].is_a?(Time)
2.3.1 :003?>   JWT.base64url_encode(JSON.generate(payload))
2.3.1 :004?>   end
 => :encoded_payload
2.3.1 :005 > payload = [{ id: 5 }, { time: 7 }]
 => [{:id=>5}, {:time=>7}]
2.3.1 :006 > token = JWT.encode(payload, CONFIG['secret'], 'HS256')
 => "eyJ0eXAK1QiiOiJIUzI1NiJ9.W3siaWQisidGltZSI6N31d.VMcXr7wklibbl8tJBTNfYBWvAu7iIt-yrmY"
2.3.1 :008 > JWT.decode token, CONFIG['secret'], true, { :algorithm => 'HS256' }
 => [[{"id"=>5}, {"time"=>7}], {"typ"=>"JWT", "alg"=>"HS256"}]

@excpt excpt self-assigned this Aug 24, 2017
@excpt excpt added this to the Version 2.0.0 milestone Aug 24, 2017
@excpt excpt closed this as completed in 4bed3b4 Sep 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants