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();
Token
explicit Token(
const std::string & token
);
Token
Token
Token(
Token && token
) noexcept;
Token
Token(
const std::string & header,
const std::string & payload,
const std::string & signature
);
Destructor
~Token
~Token();
Destroys the Token.
Member Functions
getAlgorithm
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
std::string getContentType() const;
Returns the JWT content type, or an empty string if no content type has been specified.
getExpiration
Poco::Timestamp getExpiration() const;
Returns the expiration, or a zero Poco::Timestamp if no expiration has been specified.
getId
std::string getId() const;
getIssuedAt
Poco::Timestamp getIssuedAt() const;
Returns the issued-at time, or a zero Poco::Timestamp if no issued-at time has been specified.
getIssuer
std::string getIssuer() const;
Returns the issuer, or an empty string if no issuer has been specified.
getNotBefore
Poco::Timestamp getNotBefore() const;
Returns the not-before time, or a zero Poco::Timestamp if no not-before time has been specified.
getSubject
std::string getSubject() const;
Returns the subject, or an empty string if no issuer has been specified.
getType
std::string getType() const;
Returns the JWT type or an empty string if no type has been specified.
header
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
const Poco::JSON::Object & payload() const;
Returns the payload JSON object.
payload
Poco::JSON::Object & payload();
Returns the payload JSON object.
setAlgorithm
void setAlgorithm(
const std::string & algorithm
);
Sets the JWT signature algorithm ("alg" claim in header).
setAudience
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
void setContentType(
const std::string & contentType
);
Sets the JWT content type ("cty" claim in header").
setExpiration
void setExpiration(
const Poco::Timestamp & expiration
);
Sets the expiration ("exp" claim in payload).
setId
void setId(
const std::string & id
);
Sets the JWT ID ("jti" claim in payload).
setIssuedAt
void setIssuedAt(
const Poco::Timestamp & issuedAt
);
Sets the issued-at time ("iat" claim in payload).
setIssuer
void setIssuer(
const std::string & issuer
);
Sets the issuer ("iss" claim in payload).
setNotBefore
void setNotBefore(
const Poco::Timestamp & notBefore
);
Sets the not-before time ("nbf" claim in payload).
setSubject
void setSubject(
const std::string & subject
);
Sets the subject ("sub" claim in paylod).
setType
void setType(
const std::string & type
);
Sets the JWT type ("typ" claim in payload).
signature
const std::string & signature() const;
Returns the signature string.
toString
std::string toString() const;
assign
void assign(
const std::string & header,
const std::string & payload,
const std::string & signature
);
getTimestamp
Poco::Timestamp getTimestamp(
const std::string & claim
) const;
setTimestamp
void setTimestamp(
const std::string & claim,
const Poco::Timestamp & ts
);
sign
void sign(
const std::string & signature
);
Variables
CLAIM_ALGORITHM
static const std::string CLAIM_ALGORITHM;
CLAIM_AUDIENCE
static const std::string CLAIM_AUDIENCE;
CLAIM_CONTENT_TYPE
static const std::string CLAIM_CONTENT_TYPE;
CLAIM_EXPIRATION
static const std::string CLAIM_EXPIRATION;
CLAIM_ISSUED_AT
static const std::string CLAIM_ISSUED_AT;
CLAIM_ISSUER
static const std::string CLAIM_ISSUER;
CLAIM_JWT_ID
static const std::string CLAIM_JWT_ID;
CLAIM_NOT_BEFORE
static const std::string CLAIM_NOT_BEFORE;
CLAIM_SUBJECT
static const std::string CLAIM_SUBJECT;
CLAIM_TYPE
static const std::string CLAIM_TYPE;