Add jwt example.
This commit is contained in:
parent
3ccb929687
commit
15ddaa1cb8
|
|
@ -1,4 +1,10 @@
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
project(jwp-mosquitto-plugin)
|
project(jwp-mosquitto-plugin)
|
||||||
|
|
||||||
|
add_subdirectory(ext/jwt-cpp)
|
||||||
|
|
||||||
add_library(jwp-plugin SHARED src/jwp-plugin.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)
|
||||||
|
|
|
||||||
|
|
@ -1 +1,7 @@
|
||||||
sudo apt install mosquitto-dev g++ cmake libmosquitto-dev mosquitto-clients
|
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
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
#include <jwt-cpp/jwt.h>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue