https://github.com/systelab/cpp-jwt-utils
C++ JSON Web Token Utilities
https://github.com/systelab/cpp-jwt-utils
cpp-library
Last synced: 3 months ago
JSON representation
C++ JSON Web Token Utilities
- Host: GitHub
- URL: https://github.com/systelab/cpp-jwt-utils
- Owner: systelab
- License: mit
- Created: 2019-02-15T07:18:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-09-17T10:28:38.000Z (9 months ago)
- Last Synced: 2025-09-17T12:33:18.109Z (9 months ago)
- Topics: cpp-library
- Language: C++
- Homepage:
- Size: 152 KB
- Stars: 3
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/systelab/cpp-jwt-utils)
[](https://ci.appveyor.com/project/systelab/cpp-jwt-utils)
[](https://codecov.io/gh/systelab/cpp-jwt-utils)
[](https://www.codacy.com/gh/systelab/cpp-jwt-utils/dashboard?utm_source=github.com&utm_medium=referral&utm_content=systelab/cpp-jwt-utils&utm_campaign=Badge_Grade)
# C++ JSON Web Token Utilities
This library provides utilities to generate, parse and validate JWTs (JSON Web Tokens) used to implement an authentication mechanism over a REST API.
## Setup
### Download using Conan
This library is designed to be installed by making use of [Conan](https://conan.io/) package manager. So, you just need to add the following requirement into your Conan recipe:
```python
def requirements(self):
self.requires("JWTUtils/1.0.0@systelab/stable")
```
> Version number of this code snipped is set just as an example. Replace it for the desired package to retrieve.
As this package is not available on the conan-center, you will also need to configure a remote repository before installing dependencies:
```bash
conan remote add systelab-public https://csw.jfrog.io/artifactory/api/conan/cpp-conan-production-local
```
See Conan [documentation](https://docs.conan.io/en/latest/) for further details on how to integrate this package with your build system.
### Build from sources
See [BUILD.md](BUILD.md) document for details.
## Usage
### Build a token
Use the `systelab::jwt::TokenBuilderService` class to generate a valid token containing a vector of claims:
```cpp
#include "RapidJSONAdapter/JSONAdapter.h"
#include "JWTUtils/Services/TokenBuilderService.h"
systelab::json::rapidjson::JSONAdapter jsonAdapter;
systelab::jwt::TokenBuilderService tokenBuilderService(jsonAdapter);
std::string secretKey = "Here goes your secret key";
std::vector< std::pair > claims =
{ {"sub", "1234567890"}, {"name", "John Doe"}, {"iat", "1516239022"} };
std::string token = tokenBuilderService.buildJWT(secretKey, claims);
```
### Parse a token
Use the `systelab::jwt::TokenParserService` class to parse a JWT token and retrieve the claims contained on it. It will return false when the provided JWT is not valid.
```cpp
#include "RapidJSONAdapter/JSONAdapter.h"
#include "JWTUtils/Services/TokenParserService.h"
systelab::json::rapidjson::JSONAdapter jsonAdapter;
systelab::jwt::TokenParserService tokenParserService(jsonAdapter);
std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoiMTUxNjIzOTAyMiJ9."
"5ODOvx-citIP2b7EbnG1TlSUcoAKmCeOyE-_Kw3-dLo";
std::string secretKey = "Here goes your secret key";
std::vector< std::pair > claims;
bool valid = tokenParserService.validateJWT(token, secretKey, claims);
```