Poco::JWT

class Token

Library: JWT
Package: JWT
Header: Poco/JWT/Token.h

Description

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);

Member Summary

Member Functions: assign, getAlgorithm, getAudience, getContentType, getExpiration, getId, getIssuedAt, getIssuer, getNotBefore, getSubject, getTimestamp, getType, header, operator =, payload, setAlgorithm, setAudience, setContentType, setExpiration, setId, setIssuedAt, setIssuer, setNotBefore, setSubject, setTimestamp, setType, sign, signature, toString

Constructors

Token

Token();

Creates an empty JSON Web Token.

Token

explicit Token(
    const std::string & 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.

Token protected

Token(
    const std::string & header,
    const std::string & payload,
    const std::string & signature
);

Destructor

~Token

~Token();

Destroys the Token.

Member Functions

getAlgorithm inline

std::string getAlgorithm() const;

Returns the JWT signature algorithm, or an empty string if no algorithm has been specified.

getAudience

std::vector < std::string > getAudience() const;

Returns the audience.

getContentType inline

std::string getContentType() const;

Returns the JWT content type, or an empty string if no content type has been specified.

getExpiration inline

Poco::Timestamp getExpiration() const;

Returns the expiration, or a zero Poco::Timestamp if no expiration has been specified.

getId inline

std::string getId() const;

Returns the JWT ID, or an empty string if no JWT ID has been specified.

getIssuedAt inline

Poco::Timestamp getIssuedAt() const;

Returns the issued-at time, or a zero Poco::Timestamp if no issued-at time has been specified.

getIssuer inline

std::string getIssuer() const;

Returns the issuer, or an empty string if no issuer has been specified.

getNotBefore inline

Poco::Timestamp getNotBefore() const;

Returns the not-before time, or a zero Poco::Timestamp if no not-before time has been specified.

getSubject inline

std::string getSubject() const;

Returns the subject, or an empty string if no issuer has been specified.

getType inline

std::string getType() const;

Returns the JWT type or an empty string if no type has been specified.

header inline

const Poco::JSON::Object & header() const;

Returns the header JSON object.

header

Poco::JSON::Object & header();

Returns the header JSON object.

operator =

Token & operator = (
    const Token & token
);

Assignment operator.

operator =

Token & operator = (
    Token && token
) noexcept;

Move assignment operator.

operator =

Token & operator = (
    const std::string & token
);

Parses and assigns serialized JWT.

payload inline

const Poco::JSON::Object & payload() const;

Returns the payload JSON object.

payload

Poco::JSON::Object & payload();

Returns the payload JSON object.

setAlgorithm inline

void setAlgorithm(
    const std::string & algorithm
);

Sets the JWT signature algorithm ("alg" claim in header).

setAudience inline

void setAudience(
    const std::string & audience
);

Sets the audience ("aud" claim in payload) to a single value.

setAudience

void setAudience(
    const std::vector < std::string > & audience
);

Sets the audience ("aud" claim in payload).

setContentType inline

void setContentType(
    const std::string & contentType
);

Sets the JWT content type ("cty" claim in header").

setExpiration inline

void setExpiration(
    const Poco::Timestamp & expiration
);

Sets the expiration ("exp" claim in payload).

setId inline

void setId(
    const std::string & id
);

Sets the JWT ID ("jti" claim in payload).

setIssuedAt inline

void setIssuedAt(
    const Poco::Timestamp & issuedAt
);

Sets the issued-at time ("iat" claim in payload).

setIssuer inline

void setIssuer(
    const std::string & issuer
);

Sets the issuer ("iss" claim in payload).

setNotBefore inline

void setNotBefore(
    const Poco::Timestamp & notBefore
);

Sets the not-before time ("nbf" claim in payload).

setSubject inline

void setSubject(
    const std::string & subject
);

Sets the subject ("sub" claim in paylod).

setType inline

void setType(
    const std::string & type
);

Sets the JWT type ("typ" claim in payload).

signature inline

const std::string & signature() const;

Returns the signature string.

toString

std::string toString() const;

Returns the serialized string representation of the JSON Web Token.

assign protected

void assign(
    const std::string & header,
    const std::string & payload,
    const std::string & signature
);

getTimestamp protected

Poco::Timestamp getTimestamp(
    const std::string & claim
) const;

setTimestamp protected

void setTimestamp(
    const std::string & claim,
    const Poco::Timestamp & ts
);

sign protected

void sign(
    const std::string & signature
);

Variables

CLAIM_ALGORITHM static

static const std::string CLAIM_ALGORITHM;

CLAIM_AUDIENCE static

static const std::string CLAIM_AUDIENCE;

CLAIM_CONTENT_TYPE static

static const std::string CLAIM_CONTENT_TYPE;

CLAIM_EXPIRATION static

static const std::string CLAIM_EXPIRATION;

CLAIM_ISSUED_AT static

static const std::string CLAIM_ISSUED_AT;

CLAIM_ISSUER static

static const std::string CLAIM_ISSUER;

CLAIM_JWT_ID static

static const std::string CLAIM_JWT_ID;

CLAIM_NOT_BEFORE static

static const std::string CLAIM_NOT_BEFORE;

CLAIM_SUBJECT static

static const std::string CLAIM_SUBJECT;

CLAIM_TYPE static

static const std::string CLAIM_TYPE;