Simple wrapper for jwt-cpp to save more time, default is basic HS256 encoding
- C++17
- CMake
- jwt-cpp
- nlohmann/json (3.11)
Example using json payload inside your jwt token. Don't forget that the more payload in your token, the more it weighs
// jwt tokens
jwt::token jwt_access("access", "secret", "quarkpunk", 5);
jwt::token jwt_refresh("refresh", "secret", "quarkpunk", 1500);
// your json payload data
const json data = {
{ "id", "68b1a86e5bfc86a7fc048623" },
{ "session", "ab8214302e" },
{ "role", "admin" }
};
// export tokens to string
jwt_access.to_string("data", data);
jwt_refresh.to_string("data", data);Token verification, everything is simple, verification returns true or false, as well as json payload in case of successful validation
// this is for example your http request
const std::string token_string = req.body();
// result validate json payload
json json_data;
// validate tokens
bool valid_access = jwt_access.validate(token_string, "data", json_data);
bool valid_refresh = jwt_refresh.validate(token_string, "data", json_data);There is also an easier way if you only need the id in your token
// export tokens to string
// insert only id field
std::string access = jwt_access.to_string("68b1a86e5bfc86a7fc048623");
std::string refesh = jwt_refresh.to_string("68b1a86e5bfc86a7fc048623");
// validate tokens
// check only id field
std::string return_id;
jwt_access.validate(access, return_id);
jwt_access.validate(refesh, return_id);