diff --git a/CMakeLists.txt b/CMakeLists.txt index 60dcfde..c6e1b0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,10 @@ cmake_minimum_required(VERSION 3.5) + project(jwp-mosquitto-plugin) +add_subdirectory(ext/jwt-cpp) + add_library(jwp-plugin SHARED src/jwp-plugin.cpp) + +add_executable(jwt-example src/jwt-example.cpp) +target_link_libraries(jwt-example PRIVATE jwt-cpp) diff --git a/README.md b/README.md index baf0544..6488ab5 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ sudo apt install mosquitto-dev g++ cmake libmosquitto-dev mosquitto-clients + +sudo apt install openssl libssl-dev + + +openssl genpkey -algorithm Ed25519 -out priv.key +openssl pkey -in priv.key -pubout > pub.key diff --git a/src/jwt-example.cpp b/src/jwt-example.cpp new file mode 100644 index 0000000..6218984 --- /dev/null +++ b/src/jwt-example.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + std::string pub_key = R"(-----BEGIN PUBLIC KEY----- +MCowBQYDK2VwAyEA+IYMWskcPLcC8IsUy6xsj3whqlzYwFWuAmVR7ue/LLw= +-----END PUBLIC KEY-----)"; + std::string priv_key = R"(-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEID6d/A9UnVV5xXf9RAvXSNTk/a1QNUrzfvawzEAWDh3e +-----END PRIVATE KEY-----)"; + + auto token = jwt::create() + .set_type("JWT") + .set_issuer("jamesp") + .set_subject("jimmy") + .set_audience("mqtt") + .set_payload_claim("topics", jwt::claim(std::string{"{'/help/*', '/test/*'}"})) + .set_expires_at(std::chrono::system_clock::now()) + .sign(jwt::algorithm::ed25519(pub_key, priv_key, "", "")); + + std::cout << "Token: " << token << std::endl; + + auto verifier = jwt::verify() + .allow_algorithm(jwt::algorithm::ed25519(pub_key, "", "", "")) + .with_issuer("jamesp"); + + auto decoded = jwt::decode(token); + + try + { + verifier.verify(decoded); + } + catch(jwt::error::token_verification_exception& exception) + { + std::cout << exception.what() << std::endl; + return -1; + } + + for(auto& e : decoded.get_header_claims()) + { + std::cout << e.first << ": " << e.second.to_json() << std::endl; + } + + std::cout << std::endl; + + for(auto& e : decoded.get_payload_claims()) + { + std::cout << e.first << ": " << e.second.to_json() << std::endl; + } + + return 0; +} +