Poco

namespace JWT

Overview

Functions: JWTException, Signer, Token, deserialize, serialize, split

Functions

JWTException

JWTException(
    const std::string & msg,
    int code = 0
);

JWTException

JWTException(
    const std::string & msg,
    const std::string & arg,
    int code = 0
);

JWTException

JWTException(
    const std::string & msg,
    const Poco::Exception & exc,
    int code = 0
);

JWTException

JWTException(
    const JWTException & exc
);

Signer

explicit Signer(
    const std::string & hmacKey
);

This class signs and verifies the signature of JSON Web Tokens.

The following signing algorithms are supported:

  • HS256 (HMAC using SHA256)
  • HS384 (HMAC using SHA384)
  • HS512 (HMAC using SHA512)
  • RS256 (RSA SSA PKCS1 v1.5 using SHA256)
  • RS384 (RSA SSA PKCS1 v1.5 using SHA384)
  • RS512 (RSA SSA PKCS1 v1.5 using SHA512)
  • ES256 (ECDSA using P-256 and SHA-256)
  • ES384 (ECDSA using P-256 and SHA-384)
  • ES512 (ECDSA using P-256 and SHA-512)

Creates a Signer.

For signing and verification, a key must be set using the setHMACKey(), setRSAKey() or setECKey() methods.

Sets HS256 as the only allowed algorithm. Call setAlgorithms() or addAlgorithm() to allow additional algorithms for verification. Creates the Signer using the given secret/key for HMAC-based signing and verification.

Sets HS256 as the only allowed algorithm. Call setAlgorithms() or addAlgorithm() to allow additional algorithms for verification.

Signer

explicit Signer(
    const Poco::SharedPtr < Poco::Crypto::RSAKey > & pRSAKey
);

Creates the Signer using the given secret/key for RSA-based signing and verification.

Sets HS256 as the only allowed algorithm. Call setAlgorithms() or addAlgorithm() to allow additional algorithms for verification.

Signer

explicit Signer(
    const Poco::SharedPtr < Poco::Crypto::ECKey > & pECKey
);

Creates the Signer using the given secret/key for EC-based signing and verification.

Sets HS256 as the only allowed algorithm. Call setAlgorithms() or addAlgorithm() to allow additional algorithms for verification.

Token

explicit Token(
    const std::string & token
);

This class represents a JSON Web Token (JWT) according to RFC 7519.

To create and sign a JWT (using the Signer class):

Token token;
token.setType("JWT");
token.setSubject("1234567890");
token.payload().set("name", std::string("John Doe"));
token.setIssuedAt(Poco::Timestamp()));

Signer signer("0123456789ABCDEF0123456789ABCDEF");
std::string jwt = signer.sign(token, Signer::ALGO_HS256);

To verify a signed token:

std::string jwt(
	"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
	"eyJpYXQiOjE1MTYyMzkwMjIsIm5hbWUiOiJKb2huIERvZSIsInN1YiI6IjEyMzQ1Njc4OTAifQ."
	"qn9G7NwFEOjIh-7hfCUDZA1aJeQmf7I7YvzCBcdenGw");

Signer signer("0123456789ABCDEF0123456789ABCDEF");
Token token = signer.verify(jwt);

Creates an empty JSON Web Token. Creates a JSON Web Token from its serialized string representation.

Token

Token(
    const Token & token
);

Creates a JSON Web Token by copying another one.

Token

Token(
    Token && token
) noexcept;

Creates a JSON Web Token by moving another one.

deserialize static

static Poco::JSON::Object::Ptr deserialize(
    const std::string & serialized
);

Attempts to deserialize a base64-encoded serialized JSON object.

deserialize static

static Poco::JSON::Object::Ptr deserialize(
    std::istream & stream
);

Attempts to deserialize a base64-encoded serialized JSON object.

serialize

class JWT_API Serializer { public :static std::string serialize(
    const Poco::JSON::Object & object
);

A helper class for serializing and deserializing JWTs. Serializes and base64-encodes a JSON object.

serialize static

static void serialize(
    const Poco::JSON::Object & object,
    std::ostream & stream
);

Serializes and base64-encodes a JSON object.

split static

static std::vector < std::string > split(
    const std::string & token
);

Splits a serialized JWT into its components.

Securely control IoT edge devices from anywhere   Connect a Device