Struct jwt_payload

Inheritance Relationships

Base Types

Struct Documentation

struct jwt_payload : public jwt::write_interface, public jwt::base64_enc_dec<jwt_payload>

Component class representing JWT Payload. The payload is nothing but a set of claims which are directly written into a JSON object.

Public Functions

jwt_payload() = default

Default constructor.

inline jwt_payload(const jwt::string_view enc_str)

Construct the payload from an encoded string. TODO: Throw an exception in case of error.

jwt_payload(const jwt_payload&) = default

Default copy and assignment operations.

jwt_payload &operator=(const jwt_payload&) = default
~jwt_payload() = default
template<typename T, typename = typename std::enable_if_t<!std::is_same<system_time_t, std::decay_t<T>>::value || !std::is_same<jwt::string_view, std::decay_t<T>>::value>>
inline bool add_claim(const jwt::string_view cname, T &&cvalue, bool overwrite = false)

Add a claim to the set. Parameters: cname - The name of the claim. cvalue - Value of the claim. overwrite - Over write the value if already present.

Note

: This overload works for all value types which are: a) not an instance of type system_time_t. b) not an instance of type jwt::string_view. c) can be written to json_t object.

inline bool add_claim(const jwt::string_view cname, const jwt::string_view cvalue, bool overwrite = false)

Adds a claim. This overload takes string claim value.

inline bool add_claim(const jwt::string_view cname, system_time_t tp, bool overwrite = false)

Adds a claim. This overload takes system_time_t claim value.

Note

: Useful for providing timestamp as the claim value.

template<typename T, typename = std::enable_if_t<                      !std::is_same<std::decay_t<T>, system_time_t>::value ||                      !std::is_same<std::decay_t<T>, jwt::string_view>::value                     >> inline bool add_claim (SCOPED_ENUM registered_claims cname, T &&cvalue, bool overwrite=false)

Adds a claim. This overload takes registered_claims as the claim name.

inline bool add_claim (SCOPED_ENUM registered_claims cname, system_time_t tp, bool overwrite=false)

Adds a claim. This overload takes registered_claims as the claim name and system_time_t as the claim value type.

inline bool add_claim (SCOPED_ENUM registered_claims cname, jwt::string_view cvalue, bool overwrite=false)

Adds a claim. This overload takes registered_claims as the claim name and jwt::string_view as the claim value type.

template<typename T>
inline decltype(auto) get_claim_value(const jwt::string_view cname) const

Gets the claim value provided the claim value name.

The template type

T is what the user expects the value type to be. If the type provided is incompatible the underlying JSON library will throw an exception.

Note

: The claim name used here is Case Sensitive.

template<typename T> inline decltype(auto) get_claim_value (SCOPED_ENUM registered_claims cname) const

Gets the claim value provided the claim value name. This overload takes the claim name as an instance of type registered_claims.

The template type T is what the user expects the value type to be. If the type provided is incompatible the underlying JSON library will throw an exception.

inline bool remove_claim(const jwt::string_view cname)

Remove a claim identified by a claim name.

inline bool remove_claim (SCOPED_ENUM registered_claims cname)

Remove a claim. Overload which takes the claim name as an instance of registered_claims type.

inline bool has_claim(const jwt::string_view cname) const noexcept

Checks whether a claim is present in the payload or not.

Note

: Claim name is case sensitive for this API.

inline bool has_claim (SCOPED_ENUM registered_claims cname) const noexcept

Checks whether a claim is present in the payload or not. Overload which takes the claim name as an instance of registered_claims type.

template<typename T>
inline bool has_claim_with_value(const jwt::string_view cname, T &&cvalue) const

Checks whether there is claim with a specific value in the payload.

template<typename T> inline bool has_claim_with_value (const SCOPED_ENUM registered_claims cname, T &&value) const

Checks whether there is claim with a specific value in the payload. Overload which takes the claim name as an instance of type registered_claims.

inline std::string encode(bool pprint = false)

Encodes the payload as URL safe Base64 encoded string.

inline void decode(const jwt::string_view enc_str, std::error_code &ec)

Decodes an encoded string and overwrites the payload as per the encoded information.

This version of API reports error via std::error_code.

Note

: Allocation failures are still thrown out as exceptions.

inline void decode(const jwt::string_view enc_str)

Overload of decode API which throws exception on any failure.

Throws DecodeError on failure.

inline const json_t &create_json_obj() const

Creates a JSON object of the payload.

The presence of this API is required for making it work with write_interface.