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

Verify tokens without throwing exceptions #124

Open
kwando opened this issue Jan 18, 2016 · 10 comments
Open

Verify tokens without throwing exceptions #124

kwando opened this issue Jan 18, 2016 · 10 comments
Assignees
Milestone

Comments

@kwando
Copy link

kwando commented Jan 18, 2016

It would be very nice to be able to verify a token without having to rescue exceptions..

@excpt
Copy link
Member

excpt commented Jan 18, 2016

Hi @kwando,

do you mean something like this?

Pseudocode:

exp = Time.now.to_i + 4 * 3600
exp_payload = { :data => 'data', :exp => exp }

token = JWT.encode exp_payload, hmac_secret, 'HS256'

decoded_token = JWT.decode token, hmac_secret, true, { :algorithm => 'HS256' }

if JWT.has_error?
  puts JWT.get_errors # returns array of errors ['Exp is invalid', 'Algo does not match.']
end

@kwando
Copy link
Author

kwando commented Jan 18, 2016

Not with global state like that.

exp = Time.now.to_i + 4 * 3600
exp_payload = { :data => 'data', :exp => exp }

token = JWT.encode(exp_payload, hmac_secret, 'HS256')

result = JWT.decode(token, hmac_secret, true, { :algorithm => 'HS256' })

if result.errors?
  puts result.errors # returns array of errors ['Exp is invalid', 'Algo does not match.']
end

result.value # returns the decoded claims

@fabioxgn
Copy link

fabioxgn commented Feb 7, 2016

@kwando @excpt agreed. It is never nice to use exception for flow control: http://programmers.stackexchange.com/a/189225

The main problem of doing this would be backwards compatibility.

@excpt
Copy link
Member

excpt commented Feb 8, 2016

@fabioxgn If we're planning this one correct we introduce simply an API change / break with version 2.0. This shouldn't be a problem.

@kwando
Copy link
Author

kwando commented Feb 9, 2016

I'm willing to invest some time into this endeavor. I think the verification API needs an overhaul too and it would be a good to look into that if we are doing a 2.0.

@excpt
Copy link
Member

excpt commented Feb 9, 2016

@kwando Looking forward seeing your ideas.

You may have a look at #110 for a more advanced discussion into the 2.0 verification API.

@excpt excpt added the 2.0 label Mar 23, 2016
@excpt excpt removed the 2.0 label Feb 6, 2017
@JoeWoodward
Copy link

what ever happened to this. It seems like flow control is still managed through exceptions. Am I missing something?

@excpt
Copy link
Member

excpt commented Feb 25, 2018

This proposed change didn’t make it into 2.0. This is still an open issue.

@ab320012
Copy link
Contributor

ab320012 commented Apr 23, 2018

@excpt @JoeWoodward i think it would make sense to introduce a new class like DecodedToken with the interface #errors and #value. We can initialize the class at the beginning of JWT#decode method and return at the end.

@Tonyynot14
Copy link

How can these exceptions be rescued? It just throws a 500 server error when they occur.

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

7 participants