Thalhammer / jwt-cpp

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

Unable to generate token using ed25519 #333

Closed ericjansen closed 9 months ago

ericjansen commented 9 months ago

What's your question?

Unable to generate token using ed25519

Additional Context

Hi, I need assistance to show me how to generate token using ed25519. Please let me know in which steps that I am wrong. What I did is shown the simple code as below:

int main() {
  std::string ed25519_priv_key = R"(-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCCtVWQ6rPwEt82c8R7DP/ktg2x4R7zNF3WpVlf/QggpQAAAJhD7LsFQ+y7
BQAAAAtzc2gtZWQyNTUxOQAAACCCtVWQ6rPwEt82c8R7DP/ktg2x4R7zNF3WpVlf/QggpQ
AAAEC3Q6yZ9HAd4BLncd74uEC/yNdSGUJQNLhz/t+Z7wwpmYK1VZDqs/AS3zZzxHsM/+S2
DbHhHvM0XdalWV/9CCClAAAAFGphbnNlbmVyaWNAZ21haWwuY29tAQ==)";

  std::string ed25519_pub_key = R"(ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIK1VZDqs/AS3zZzxHsM/+S2DbHhHvM0XdalWV/9CCCl janseneric@gmail.com)";

  auto token = jwt::create()
                .set_issuer("auth0")
                .set_type("JWT")
                .set_id("ed25519-example")
                .set_issued_at(std::chrono::system_clock::now())
                .set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{36000})
                .set_payload_claim("sample",jwt::claim(std::string{"test"}))
                //.sign(jwt::algorithm::ed25519("public.pem","private.pem","",""));
                .sign(jwt::algorithm::ed25519{ed25519_pub_key,ed25519_priv_key,"",""});

  std::cout << "token : \n" << token << '\n';

  return 0;
}

The result of the code is:

Press ENTER or type command to continue
libc++abi: terminating due to uncaught exception of type jwt::error::rsa_exception: failed to load key: bio read failed

Command terminated

Press ENTER or type command to continue

Thank you and regards, Eric

prince-chrismc commented 9 months ago

OpenSSH keys are not supported by OpenSSL. You'll need to use the openssl to generate valid keys.

Some of them are saved in the comments of the test code along with valid keys but theres plenty of stackoverflow.com posts to help you.

prince-chrismc commented 9 months ago

I am marking this as resolved but don't hesitate to reply if you need more help