Thalhammer / jwt-cpp

A header only library for creating and validating json web tokens in c++
https://thalhammer.github.io/jwt-cpp/
MIT License
886 stars 241 forks source link

Add bool to verify() #355

Closed dwampler35 closed 3 months ago

dwampler35 commented 3 months ago

What would you like to see added?

Add a return value to verify()

Additional Context

Can we add a bool as a return code for verify()? It's not appropriate to use exceptions in all environments to handle failures.

Thalhammer commented 3 months ago

It's not appropriate to use exceptions in all environments to handle failures

You don't have to use exceptions, the verify method provides a overload that accepts a std::error_code which gets filled with details about the verification error. If you provide a error code the verify method won't throw (except for truly exceptional circumstances like OOM).

Ideally you should check the reason the verification failed and give the provider of the token a hint on what went wrong, but if all you care about is a true/false result you can simply use std::error_code's operator bool to check for errors (true meaning an error occurred).

I hope this clarifies your issue.

dwampler35 commented 3 months ago

Thanks I missed that overload

On Fri, Jul 12, 2024 at 12:19 PM Dominik Thalhammer < @.***> wrote:

It's not appropriate to use exceptions in all environments to handle failures

You don't have to use exceptions, the verify method provides a overload https://github.com/Thalhammer/jwt-cpp/blob/a6927cb8140858c34e05d1a954626b9849fbcdfc/include/jwt-cpp/jwt.h#L3551 that accepts a std::error_code which gets filled with details about the verification error. If you provide a error code the verify method won't throw (except for truly exceptional circumstances like OOM).

Ideally you should check the reason the verification failed and give the provider of the token a hint on what went wrong, but if all you care about is a true/false result you can simply use std::error_code's operator bool https://en.cppreference.com/w/cpp/error/error_code/operator_bool to check for errors (true meaning an error occurred).

I hope this clarifies your issue.

— Reply to this email directly, view it on GitHub https://github.com/Thalhammer/jwt-cpp/issues/355#issuecomment-2226220052, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACCDOIIX2QDQGCYEYHZ6VATZMAT5BAVCNFSM6AAAAABKXICZ3CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRWGIZDAMBVGI . You are receiving this because you authored the thread.Message ID: @.***>