109 std::string
sign(
const void* data,
size_t size)
const;
116 bool verify(
const void* data,
size_t size,
const std::string& signature)
const;
122 std::unique_ptr<Impl> m_impl;
129class ValidationResult
133 static ValidationResult
ok() noexcept {
return ValidationResult(); }
140 explicit operator bool() const noexcept {
return m_message.empty(); }
143 const std::string&
message() const noexcept {
return m_message; }
146 std::string m_message;
149 explicit ValidationResult(
const std::string&
message) noexcept : m_message(
message) {}
JWT(const std::string &token, Key key, Validators validators={Validate::exp()})
Constructs a JWT from a token.
std::string token(const Key &key) const
Returns a signed token using a pre-constructed key.
std::string token(const std::string &keyData, const Key::PasswordCallback &cb=Key::noPasswordCallback) const
Returns a signed token.
const Value::Object & header() const noexcept
Returns a list of header fields.
Definition jwt.h:273
const Value::Object & claims() const noexcept
Returns a list of claims.
Definition jwt.h:270
static JWT parse(const std::string &token)
Returns a JWT for a token without validation.
Algorithm alg() const noexcept
Returns an algorithm.
Definition jwt.h:267
Value claim(const std::string &name) const noexcept
Returns a value of a specific claim.
JWT(Algorithm alg, Value::Object claims, Value::Object header=Value::Object{}) noexcept
Constructs a JWT from scratch.
static ValidationResult verify(const std::string &token, Key key, Validators validators={Validate::exp()}) noexcept
Validates a token without constructing a JWT.
Represents signature algorithm Signs tokens and verifies token signatures.
Definition jwt.h:66
bool verify(const void *data, size_t size, const std::string &signature) const
Verifies a signature of a chunk of memory.
std::function< std::string()> PasswordCallback
Callback function for password-protected keys. Should return password in plain text.
Definition jwt.h:71
Algorithm alg() const noexcept
Returns algorithm code used by the key.
Definition jwt.h:103
static std::string noPasswordCallback()
Always throws exception, reports about missing callback.
Key(Algorithm alg, const std::string &keyData, const PasswordCallback &cb=noPasswordCallback)
Constructs key using the specified algorithm and data.
std::string sign(const void *data, size_t size) const
Signs a chunk of memory.
Key(Key &&) noexcept
Move constructor.
Represents the result of validation. If validation is successfull an object of this class is equivale...
Definition jwt.h:130
static ValidationResult failure(const std::string &message) noexcept
'Failure' constructor.
Definition jwt.h:137
const std::string & message() const noexcept
Error message accessor.
Definition jwt.h:143
static ValidationResult ok() noexcept
'Success' constructor.
Definition jwt.h:133
Represents a JSON value that can hold any JSON type.
Definition value.h:24
std::unordered_map< std::string, Value > Object
Represents a JSON object (string to Value map).
Definition value.h:50
Validation functions are here.
Validator iat(std::time_t now=std::time(nullptr)) noexcept
Constructs validator for 'iat' claim.
Validator sub(std::string subject) noexcept
Constructs validator for 'sub' claim.
Validator nbf(std::time_t now=std::time(nullptr)) noexcept
Constructs validator for 'nbf' claim.
Validator iss(std::string issuer) noexcept
Constructs validator for 'iss' claim.
Validator aud(std::string audience) noexcept
Constructs validator for 'aud' claim.
Validator exp(std::time_t now=std::time(nullptr)) noexcept
Constructs validator for 'exp' claim.
All classes, functions and constants are here.
Definition error.h:6
Algorithm
JWT signature algorithms.
Definition jwt.h:32
@ ES384
Definition jwt.h:40
@ none
Definition jwt.h:42
@ RS512
Definition jwt.h:38
@ ES512
Definition jwt.h:41
@ RS384
Definition jwt.h:37
@ RS256
Definition jwt.h:36
@ HS512
Definition jwt.h:35
@ HS256
Definition jwt.h:33
@ HS384
Definition jwt.h:34
@ ES256
Definition jwt.h:39
Algorithm stringToAlg(const std::string &value)
Converts algorithm name into algorithm code.
void enableOpenSSLErrors() noexcept
Enable OpenSSL human-readable error messages. You only need to call it once, in the beginning of your...
std::string algToString(Algorithm alg) noexcept
Converts algorithm code into a string representation.
std::vector< Validator > Validators
A list of validators.
Definition jwt.h:160
std::function< ValidationResult(const Value::Object &)> Validator
Validation function for claims.
Definition jwt.h:155
Base class for all exceptions in the library.
Definition error.h:12
Error(const std::string &message) noexcept
Constructor.
Definition jwt.h:215
ParseError(const std::string &message) noexcept
Constructor.
Definition jwt.h:226
ValidationError(const std::string &message) noexcept
Constructor.
Definition jwt.h:237
Error(const std::string &message)
Constructor.
Definition jwt.h:79