Back to Zoneminder

Signing Tokens

dep/jwt-cpp/docs/signing.md

1.38.11.2 KB
Original Source

Signing Tokens

Custom Signature Algorithms

The libraries design is open so you can implement your own algorithms, see existing examples for ideas.

cpp
struct your_algorithm{
	std::string sign(const std::string& /*unused*/, std::error_code& ec) const {
		ec.clear();
        // CALL YOUR METHOD HERE
		return {};
	}
	void verify(const std::string& /*unused*/, const std::string& signature, std::error_code& ec) const {
		ec.clear();
		if (!signature.empty()) { ec = error::signature_verification_error::invalid_signature; }
        
        // CALL YOUR METHOD HERE
	}
	std::string name() const { return "your_algorithm"; }
};

Then everything else is the same, just pass in your implementation such as:

cpp
auto token = jwt::create()
                .set_id("custom-algo-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(your_algorithm);