JWTXX
C++ library for JWT
Loading...
Searching...
No Matches
jwt.h
Go to the documentation of this file.
1#pragma once
2
6
7#include "value.h"
8#include "error.h"
9
10#include <functional>
11#include <string>
12#include <vector>
13#include <memory>
14
15#include <ctime>
16
20namespace JWTXX
21{
22
27void enableOpenSSLErrors() noexcept;
28
44
50std::string algToString(Algorithm alg) noexcept;
51
59Algorithm stringToAlg(const std::string& value);
60
65class Key
66{
67 public:
71 using PasswordCallback = std::function<std::string ()>;
72
77 {
79 explicit Error(const std::string& message) : JWTXX::Error(message) {}
80 };
81
86 static std::string noPasswordCallback();
87
93 Key(Algorithm alg, const std::string& keyData, const PasswordCallback& cb = noPasswordCallback);
96
98 Key(Key&&) noexcept;
100 Key& operator=(Key&&) noexcept;
101
103 Algorithm alg() const noexcept { return m_alg; }
104
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;
117
119 struct Impl;
120 private:
121 Algorithm m_alg;
122 std::unique_ptr<Impl> m_impl;
123};
124
125
129class ValidationResult
130{
131 public:
133 static ValidationResult ok() noexcept { return ValidationResult(); }
137 static ValidationResult failure(const std::string& message) noexcept { return ValidationResult(message); }
138
140 explicit operator bool() const noexcept { return m_message.empty(); }
141
143 const std::string& message() const noexcept { return m_message; }
144
145 private:
146 std::string m_message;
147
148 ValidationResult() noexcept {}
149 explicit ValidationResult(const std::string& message) noexcept : m_message(message) {}
150};
151
155using Validator = std::function<ValidationResult (const Value::Object&)>;
156
160using Validators = std::vector<Validator>;
161
165namespace Validate
166{
167
172Validator exp(std::time_t now = std::time(nullptr)) noexcept;
177Validator nbf(std::time_t now = std::time(nullptr)) noexcept;
182Validator iat(std::time_t now = std::time(nullptr)) noexcept;
187Validator iss(std::string issuer) noexcept;
192Validator aud(std::string audience) noexcept;
197Validator sub(std::string subject) noexcept;
198
199}
200
204class JWT
205{
206 public:
211 {
215 explicit Error(const std::string& message) noexcept : JWTXX::Error(message) {}
216 };
217
222 {
226 explicit ParseError(const std::string& message) noexcept : Error(message) {}
227 };
228
233 {
237 explicit ValidationError(const std::string& message) noexcept : Error(message) {}
238 };
239
245 JWT(const std::string& token, Key key, Validators validators = {Validate::exp()});
246
253
257 static JWT parse(const std::string& token);
258
264 static ValidationResult verify(const std::string& token, Key key, Validators validators = {Validate::exp()}) noexcept;
265
267 Algorithm alg() const noexcept { return m_alg; }
268
270 const Value::Object& claims() const noexcept { return m_claims; }
271
273 const Value::Object& header() const noexcept { return m_header; }
274
279 Value claim(const std::string& name) const noexcept;
280
286 std::string token(const std::string& keyData, const Key::PasswordCallback& cb = Key::noPasswordCallback) const;
287
294 std::string token(const Key& key) const;
295
296 private:
297 Algorithm m_alg;
298 Value::Object m_header;
299 Value::Object m_claims;
300};
301
302}
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()
Destructor.
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