arun11299 / cpp-jwt

JSON Web Token library for C++
MIT License
395 stars 111 forks source link

Verify JWT token with Certificate #43

Closed XM37 closed 5 years ago

XM37 commented 5 years ago

Hello,

is it possible to verify the JWT token with a certificate? I tried it with following code line:

auto dec_obj = jwt::decode(token, jwt::params::algorithms({"rs256"}), jwt::params::aud(audience), jwt::params::secret(key), jwt::params::verify(true));

but i get the following exception: verification failed

What is wrong? How can i verify the signature with using a certificate

arun11299 commented 5 years ago

Hello, Can you post a complete example reproducing the issue ?

Thanks

XM37 commented 5 years ago

yes of course:

auto key = read_from_file("/home/dev.cer");
auto audience = "https://dev-py4fdrqn.eu.auth0.com/api/v2/";

auto dec_obj = jwt::decode(token, jwt::params::algorithms({"rs256"}), jwt::params::aud(audience), jwt::params::secret(key), jwt::params::verify(true));

This is the JWT Token:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlFVSXhNMFV3UVVZek5EVTROVFZHTkRneFJEUTVNVEZEUlVORk9EWTJRVEkzT1RBeE1rVXhNQSJ9.eyJpc3MiOiJodHRwczovL2Rldi1weTRmZHJxbi5ldS5hdXRoMC5jb20vIiwic3ViIjoiaHkwR0N2b3lCMGlTZXpQemc4UFQ2ZUNFeEs2TmVPRldAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vZGV2LXB5NGZkcnFuLmV1LmF1dGgwLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTU0NzE1NzM1LCJleHAiOjE1NTQ3OTYxMzUsImF6cCI6Imh5MEdDdm95QjBpU2V6UHpnOFBUNmVDRXhLNk5lT0ZXIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9tZXRhZGF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0Xj6T5wigACzgfEArsja4OSDnmkOECEE3WM5RfGmRL+/ GDm79VPB7a3wqMhozC3qLuOHOmVl6tV/nG+o28u3rvcUydVx0y+4OHDJQvtBgkKX WhIugkp3a81Oy0xHM1aa4zo= -----END CERTIFICATE-----

arun11299 commented 5 years ago

Thanks. I am getting below failure reason: C++ exception with description "token expired" thrown in the test body.

There are the header and payload with verification turned off:

{"alg":"RS256","kid":"QUIxM0UwQUYzNDU4NTVGNDgxRDQ5MTFDRUNFODY2QTI3OTAxMkUxMA","typ":"JWT"}
{"aud":"https://dev-py4fdrqn.eu.auth0.com/api/v2/","azp":"hy0GCvoyB0iSezPzg8PT6eCExK6NeOFW","exp":1554796135,"gty":"client-credentials","iat":1554715735,"iss":"https://dev-py4fdrqn.eu.auth0.com/","scope":"read:client_grants create:client_grants delete:client_grants update:client_grants read:users update:users delete:users create:users read:users_app_metadata update:users_app_metadata delete:users_app_metadata create:users_app_metadata create:user_tickets read:clients update:clients delete:clients create:clients read:client_keys update:client_keys delete:client_keys create:client_keys read:connections update:connections delete:connections create:connections read:resource_servers update:resource_servers delete:resource_servers create:resource_servers read:device_credentials update:device_credentials delete:device_credentials create:device_credentials read:rules update:rules delete:rules create:rules read:rules_configs update:rules_configs delete:rules_configs read:email_provider update:email_provider delete:email_provider create:email_provider blacklist:tokens read:stats read:tenant_settings update:tenant_settings read:logs read:shields create:shields delete:shields read:anomaly_blocks delete:anomaly_blocks update:triggers read:triggers read:grants delete:grants read:guardian_factors update:guardian_factors read:guardian_enrollments delete:guardian_enrollments create:guardian_enrollment_tickets read:user_idp_tokens create:passwords_checking_job delete:passwords_checking_job read:custom_domains delete:custom_domains create:custom_domains read:email_templates create:email_templates update:email_templates read:mfa_policies update:mfa_policies read:roles create:roles delete:roles update:roles","sub":"hy0GCvoyB0iSezPzg8PT6eCExK6NeOFW@clients"}
arun11299 commented 5 years ago

Would it be possible to share the private key as well ?

XM37 commented 5 years ago

I don't have any private key. Its a third-party app. So I found a solution: I must extract the public key from the certificate and than it works!