remove our version of jwt-cpp
This commit is contained in:
parent
1bfc61a5b7
commit
7a533686de
|
@ -1,6 +0,0 @@
|
||||||
add_library(jwt-cpp INTERFACE)
|
|
||||||
add_library(jwt-cpp::jwt-cpp ALIAS jwt-cpp)
|
|
||||||
|
|
||||||
target_include_directories(jwt-cpp
|
|
||||||
INTERFACE
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
2494
dep/jwt-cpp/Doxyfile
2494
dep/jwt-cpp/Doxyfile
File diff suppressed because it is too large
Load Diff
|
@ -1,21 +0,0 @@
|
||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2018 Dominik Thalhammer
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
|
@ -1,208 +0,0 @@
|
||||||
# ![logo](https://raw.githubusercontent.com/Thalhammer/jwt-cpp/master/.github/logo.svg)
|
|
||||||
|
|
||||||
[![License Badge](https://img.shields.io/github/license/Thalhammer/jwt-cpp)](https://github.com/Thalhammer/jwt-cpp/blob/master/LICENSE)
|
|
||||||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/5f7055e294744901991fd0a1620b231d)](https://app.codacy.com/app/Thalhammer/jwt-cpp?utm_source=github.com&utm_medium=referral&utm_content=Thalhammer/jwt-cpp&utm_campaign=Badge_Grade_Settings)
|
|
||||||
[![Linux Badge][Linux]][Cross-Platform]
|
|
||||||
[![MacOS Badge][MacOS]][Cross-Platform]
|
|
||||||
[![Windows Badge][Windows]][Cross-Platform]
|
|
||||||
[![Coverage Status](https://coveralls.io/repos/github/Thalhammer/jwt-cpp/badge.svg?branch=master)](https://coveralls.io/github/Thalhammer/jwt-cpp?branch=master)
|
|
||||||
[![Documentation Badge](https://img.shields.io/badge/Documentation-master-blue)](https://thalhammer.github.io/jwt-cpp/)
|
|
||||||
[![GitHub release (latest SemVer including pre-releases)](https://img.shields.io/github/v/release/Thalhammer/jwt-cpp?include_prereleases)](https://github.com/Thalhammer/jwt-cpp/releases)
|
|
||||||
[![Stars Badge](https://img.shields.io/github/stars/Thalhammer/jwt-cpp)](https://github.com/Thalhammer/jwt-cpp/stargazers)
|
|
||||||
|
|
||||||
[Linux]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/cross-platform/ubuntu-latest/shields.json
|
|
||||||
[MacOS]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/cross-platform/macos-latest/shields.json
|
|
||||||
[Windows]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/cross-platform/windows-latest/shields.json
|
|
||||||
[Cross-Platform]: https://github.com/Thalhammer/jwt-cpp/actions?query=workflow%3A%22Cross-Platform+CI%22
|
|
||||||
|
|
||||||
A header only library for creating and validating [JSON Web Tokens](https://tools.ietf.org/html/rfc7519) in C++11. For a great introduction, [read this](https://jwt.io/introduction/).
|
|
||||||
|
|
||||||
## Signature algorithms
|
|
||||||
|
|
||||||
jwt-cpp supports all the algorithms defined by the specifications. The modular design allows to easily add additional algorithms without any problems. If you need any feel free to create a pull request or [open an issue](https://github.com/Thalhammer/jwt-cpp/issues/new).
|
|
||||||
|
|
||||||
For completeness, here is a list of all supported algorithms:
|
|
||||||
|
|
||||||
| HMSC | RSA | ECDSA | PSS | EdDSA |
|
|
||||||
| ----- | ----- | ----- | ----- | ------- |
|
|
||||||
| HS256 | RS256 | ES256 | PS256 | Ed25519 |
|
|
||||||
| HS384 | RS384 | ES384 | PS384 | Ed448 |
|
|
||||||
| HS512 | RS512 | ES512 | PS512 | |
|
|
||||||
|
|
||||||
## SSL Compatibility
|
|
||||||
|
|
||||||
In the name of flexibility and extensibility, jwt-cpp supports both [OpenSSL](https://github.com/openssl/openssl) and [LibreSSL](https://github.com/libressl-portable/portable). These are the version which are, or have been, tested:
|
|
||||||
|
|
||||||
| OpenSSL | LibreSSL |
|
|
||||||
| -------------- | --------------- |
|
|
||||||
| [1.0.2][1.0.2] | ![3.1.5][3.1] |
|
|
||||||
| 1.1.0 | ![3.2.3][3.2] |
|
|
||||||
| [1.1.1][1.1.1] | ![3.3.1][3.3] |
|
|
||||||
|
|
||||||
[1.0.2]: https://travis-ci.com/github/Thalhammer/jwt-cpp
|
|
||||||
[1.1.1]: https://github.com/Thalhammer/jwt-cpp/actions?query=workflow%3A%22Coverage+CI%22
|
|
||||||
[3.1]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/libressl/3.1.5/shields.json
|
|
||||||
[3.2]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/libressl/3.2.3/shields.json
|
|
||||||
[3.3]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/libressl/3.3.1/shields.json
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
There is no hard dependency on a JSON library. Instead, there's a generic `jwt::basic_claim` which is templated around type traits, which described the semantic [JSON types](https://json-schema.org/understanding-json-schema/reference/type.html) for a value, object, array, string, number, integer and boolean, as well as methods to translate between them.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
jwt::basic_claim<my_favorite_json_library_traits> claim(json::object({{"json", true},{"example", 0}}));
|
|
||||||
```
|
|
||||||
|
|
||||||
This allows for complete freedom when picking which libraries you want to use. For more information, [see below](#providing-your-own-json-traits-your-traits).
|
|
||||||
|
|
||||||
In order to maintain compatibility, [picojson](https://github.com/kazuho/picojson) is still used to provide a specialized `jwt::claim` along with all helpers. Defining `JWT_DISABLE_PICOJSON` will remove this optional dependency.
|
|
||||||
|
|
||||||
As for the base64 requirements of JWTs, this libary provides `base.h` with all the required implentation; However base64 implementations are very common, with varying degrees of performance. When providing your own base64 implementation, you can define `JWT_DISABLE_BASE64` to remove the jwt-cpp implementation.
|
|
||||||
|
|
||||||
### Getting Started
|
|
||||||
|
|
||||||
Simple example of decoding a token and printing all [claims](https://tools.ietf.org/html/rfc7519#section-4) ([try it out](https://github.com/Thalhammer/jwt-cpp/tree/master/example/print-claims.cpp)):
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
#include <jwt-cpp/jwt.h>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
|
|
||||||
auto decoded = jwt::decode(token);
|
|
||||||
|
|
||||||
for(auto& e : decoded.get_payload_claims())
|
|
||||||
std::cout << e.first << " = " << e.second << std::endl;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
In order to verify a token you first build a verifier and use it to verify a decoded token.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
auto verifier = jwt::verify()
|
|
||||||
.allow_algorithm(jwt::algorithm::hs256{ "secret" })
|
|
||||||
.with_issuer("auth0");
|
|
||||||
|
|
||||||
verifier.verify(decoded_token);
|
|
||||||
```
|
|
||||||
|
|
||||||
The created verifier is stateless so you can reuse it for different tokens.
|
|
||||||
|
|
||||||
Creating a token (and signing) is equally as easy.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
auto token = jwt::create()
|
|
||||||
.set_issuer("auth0")
|
|
||||||
.set_type("JWS")
|
|
||||||
.set_payload_claim("sample", jwt::claim(std::string("test")))
|
|
||||||
.sign(jwt::algorithm::hs256{"secret"});
|
|
||||||
```
|
|
||||||
|
|
||||||
Here is a simple example of creating a token that will expire in one hour:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
auto token = jwt::create()
|
|
||||||
.set_issuer("auth0")
|
|
||||||
.set_issued_at(std::chrono::system_clock::now())
|
|
||||||
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{3600})
|
|
||||||
.sign(jwt::algorithm::hs256{"secret"});
|
|
||||||
```
|
|
||||||
|
|
||||||
> To see more examples working with RSA public and private keys, visit our [examples](https://github.com/Thalhammer/jwt-cpp/tree/master/example)!
|
|
||||||
|
|
||||||
### Providing your own JSON Traits
|
|
||||||
|
|
||||||
There are several key items that need to be provided to a `jwt::basic_claim` in order for it to be interoptable with you JSON library of choice.
|
|
||||||
|
|
||||||
* type specifications
|
|
||||||
* conversion from generic "value type" to a specific type
|
|
||||||
* serialization and parsing
|
|
||||||
|
|
||||||
If ever you are not sure, the traits are heavily checked against static asserts to make sure you provide everything that's required.
|
|
||||||
|
|
||||||
> :warning: Not all JSON libraries are a like, you may need to extent certain types such that it can be used by jwt-cpp. See this [example](https://github.com/Thalhammer/jwt-cpp/blob/ac3de9e69bc698a464dacb256a1b50512843f092/tests/jsoncons/JsonconsTest.cpp).
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
struct my_favorite_json_library_traits {
|
|
||||||
// Type Specifications
|
|
||||||
using value_type = json; // The generic "value type" implementation, most libraries have one
|
|
||||||
using object_type = json::object_t; // The "map type" string to value
|
|
||||||
using array_type = json::array_t; // The "list type" array of values
|
|
||||||
using string_type = std::string; // The "list of chars", must be a narrow char
|
|
||||||
using number_type = double; // The "percision type"
|
|
||||||
using integer_type = int64_t; // The "integral type"
|
|
||||||
using boolean_type = bool; // The "boolean type"
|
|
||||||
|
|
||||||
// Translation between the implementation notion of type, to the jwt::json::type equivilant
|
|
||||||
static jwt::json::type get_type(const value_type &val) {
|
|
||||||
using jwt::json::type;
|
|
||||||
|
|
||||||
if (val.type() == json::value_t::object)
|
|
||||||
return type::object;
|
|
||||||
if (val.type() == json::value_t::array)
|
|
||||||
return type::array;
|
|
||||||
if (val.type() == json::value_t::string)
|
|
||||||
return type::string;
|
|
||||||
if (val.type() == json::value_t::number_float)
|
|
||||||
return type::number;
|
|
||||||
if (val.type() == json::value_t::number_integer)
|
|
||||||
return type::integer;
|
|
||||||
if (val.type() == json::value_t::boolean)
|
|
||||||
return type::boolean;
|
|
||||||
|
|
||||||
throw std::logic_error("invalid type");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Conversion from generic value to specific type
|
|
||||||
static object_type as_object(const value_type &val);
|
|
||||||
static array_type as_array(const value_type &val);
|
|
||||||
static string_type as_string(const value_type &val);
|
|
||||||
static number_type as_number(const value_type &val);
|
|
||||||
static integer_type as_int(const value_type &val);
|
|
||||||
static boolean_type as_bool(const value_type &val);
|
|
||||||
|
|
||||||
// serilization and parsing
|
|
||||||
static bool parse(value_type &val, string_type str);
|
|
||||||
static string_type serialize(const value_type &val); // with no extra whitespace, padding or indentation
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
If you have an improvement or found a bug feel free to [open an issue](https://github.com/Thalhammer/jwt-cpp/issues/new) or add the change and create a pull request. If you file a bug please make sure to include as much information about your environment (compiler version, etc.) as possible to help reproduce the issue. If you add a new feature please make sure to also include test cases for it.
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
|
|
||||||
In order to use jwt-cpp you need the following tools.
|
|
||||||
|
|
||||||
* libcrypto (openssl or compatible)
|
|
||||||
* libssl-dev (for the header files)
|
|
||||||
* a compiler supporting at least c++11
|
|
||||||
* basic stl support
|
|
||||||
|
|
||||||
In order to build the test cases you also need
|
|
||||||
|
|
||||||
* gtest
|
|
||||||
* pthread
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Expired tokens
|
|
||||||
|
|
||||||
If you are generating tokens that seem to immediately expire, you are likely not using UTC. Specifically,
|
|
||||||
if you use `get_time` to get the current time, it likely uses localtime, while this library uses UTC,
|
|
||||||
which may be why your token is immediately expiring. Please see example above on the right way to use current time.
|
|
||||||
|
|
||||||
### Missing \_HMAC and \_EVP_sha256 symbols on Mac
|
|
||||||
|
|
||||||
There seems to exists a problem with the included openssl library of MacOS. Make sure you link to one provided by brew.
|
|
||||||
See [here](https://github.com/Thalhammer/jwt-cpp/issues/6) for more details.
|
|
||||||
|
|
||||||
### Building on windows fails with syntax errors
|
|
||||||
|
|
||||||
The header `<Windows.h>`, which is often included in windowsprojects, defines macros for MIN and MAX which screw up std::numeric_limits.
|
|
||||||
See [here](https://github.com/Thalhammer/jwt-cpp/issues/5) for more details. To fix this do one of the following things:
|
|
||||||
|
|
||||||
* define NOMINMAX, which suppresses this behaviour
|
|
||||||
* include this library before you include windows.h
|
|
||||||
* place `#undef max` and `#undef min` before you include this library
|
|
|
@ -1,208 +0,0 @@
|
||||||
#ifndef JWT_CPP_BASE_H
|
|
||||||
#define JWT_CPP_BASE_H
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#ifdef __has_cpp_attribute
|
|
||||||
#if __has_cpp_attribute(fallthrough)
|
|
||||||
#define JWT_FALLTHROUGH [[fallthrough]]
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef JWT_FALLTHROUGH
|
|
||||||
#define JWT_FALLTHROUGH
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace jwt {
|
|
||||||
/**
|
|
||||||
* \brief character maps when encoding and decoding
|
|
||||||
*/
|
|
||||||
namespace alphabet {
|
|
||||||
/**
|
|
||||||
* \brief valid list of characted when working with [Base64](https://tools.ietf.org/html/rfc3548)
|
|
||||||
*/
|
|
||||||
struct base64 {
|
|
||||||
static const std::array<char, 64>& data() {
|
|
||||||
static constexpr std::array<char, 64> data{
|
|
||||||
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
||||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
||||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
||||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}};
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
static const std::string& fill() {
|
|
||||||
static std::string fill{"="};
|
|
||||||
return fill;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* \brief valid list of characted when working with [Base64URL](https://tools.ietf.org/html/rfc4648)
|
|
||||||
*/
|
|
||||||
struct base64url {
|
|
||||||
static const std::array<char, 64>& data() {
|
|
||||||
static constexpr std::array<char, 64> data{
|
|
||||||
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
||||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
||||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
||||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}};
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
static const std::string& fill() {
|
|
||||||
static std::string fill{"%3d"};
|
|
||||||
return fill;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace alphabet
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Alphabet generic methods for working with encoding/decoding the base64 family
|
|
||||||
*/
|
|
||||||
class base {
|
|
||||||
public:
|
|
||||||
template<typename T>
|
|
||||||
static std::string encode(const std::string& bin) {
|
|
||||||
return encode(bin, T::data(), T::fill());
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static std::string decode(const std::string& base) {
|
|
||||||
return decode(base, T::data(), T::fill());
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static std::string pad(const std::string& base) {
|
|
||||||
return pad(base, T::fill());
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static std::string trim(const std::string& base) {
|
|
||||||
return trim(base, T::fill());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static std::string encode(const std::string& bin, const std::array<char, 64>& alphabet,
|
|
||||||
const std::string& fill) {
|
|
||||||
size_t size = bin.size();
|
|
||||||
std::string res;
|
|
||||||
|
|
||||||
// clear incomplete bytes
|
|
||||||
size_t fast_size = size - size % 3;
|
|
||||||
for (size_t i = 0; i < fast_size;) {
|
|
||||||
uint32_t octet_a = static_cast<unsigned char>(bin[i++]);
|
|
||||||
uint32_t octet_b = static_cast<unsigned char>(bin[i++]);
|
|
||||||
uint32_t octet_c = static_cast<unsigned char>(bin[i++]);
|
|
||||||
|
|
||||||
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
|
|
||||||
|
|
||||||
res += alphabet[(triple >> 3 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 2 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 1 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 0 * 6) & 0x3F];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fast_size == size) return res;
|
|
||||||
|
|
||||||
size_t mod = size % 3;
|
|
||||||
|
|
||||||
uint32_t octet_a = fast_size < size ? static_cast<unsigned char>(bin[fast_size++]) : 0;
|
|
||||||
uint32_t octet_b = fast_size < size ? static_cast<unsigned char>(bin[fast_size++]) : 0;
|
|
||||||
uint32_t octet_c = fast_size < size ? static_cast<unsigned char>(bin[fast_size++]) : 0;
|
|
||||||
|
|
||||||
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
|
|
||||||
|
|
||||||
switch (mod) {
|
|
||||||
case 1:
|
|
||||||
res += alphabet[(triple >> 3 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 2 * 6) & 0x3F];
|
|
||||||
res += fill;
|
|
||||||
res += fill;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
res += alphabet[(triple >> 3 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 2 * 6) & 0x3F];
|
|
||||||
res += alphabet[(triple >> 1 * 6) & 0x3F];
|
|
||||||
res += fill;
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string decode(const std::string& base, const std::array<char, 64>& alphabet,
|
|
||||||
const std::string& fill) {
|
|
||||||
size_t size = base.size();
|
|
||||||
|
|
||||||
size_t fill_cnt = 0;
|
|
||||||
while (size > fill.size()) {
|
|
||||||
if (base.substr(size - fill.size(), fill.size()) == fill) {
|
|
||||||
fill_cnt++;
|
|
||||||
size -= fill.size();
|
|
||||||
if (fill_cnt > 2) throw std::runtime_error("Invalid input");
|
|
||||||
} else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((size + fill_cnt) % 4 != 0) throw std::runtime_error("Invalid input");
|
|
||||||
|
|
||||||
size_t out_size = size / 4 * 3;
|
|
||||||
std::string res;
|
|
||||||
res.reserve(out_size);
|
|
||||||
|
|
||||||
auto get_sextet = [&](size_t offset) {
|
|
||||||
for (size_t i = 0; i < alphabet.size(); i++) {
|
|
||||||
if (alphabet[i] == base[offset]) return static_cast<uint32_t>(i);
|
|
||||||
}
|
|
||||||
throw std::runtime_error("Invalid input");
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t fast_size = size - size % 4;
|
|
||||||
for (size_t i = 0; i < fast_size;) {
|
|
||||||
uint32_t sextet_a = get_sextet(i++);
|
|
||||||
uint32_t sextet_b = get_sextet(i++);
|
|
||||||
uint32_t sextet_c = get_sextet(i++);
|
|
||||||
uint32_t sextet_d = get_sextet(i++);
|
|
||||||
|
|
||||||
uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
|
|
||||||
|
|
||||||
res += static_cast<char>((triple >> 2 * 8) & 0xFFU);
|
|
||||||
res += static_cast<char>((triple >> 1 * 8) & 0xFFU);
|
|
||||||
res += static_cast<char>((triple >> 0 * 8) & 0xFFU);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fill_cnt == 0) return res;
|
|
||||||
|
|
||||||
uint32_t triple = (get_sextet(fast_size) << 3 * 6) + (get_sextet(fast_size + 1) << 2 * 6);
|
|
||||||
|
|
||||||
switch (fill_cnt) {
|
|
||||||
case 1:
|
|
||||||
triple |= (get_sextet(fast_size + 2) << 1 * 6);
|
|
||||||
res += static_cast<char>((triple >> 2 * 8) & 0xFFU);
|
|
||||||
res += static_cast<char>((triple >> 1 * 8) & 0xFFU);
|
|
||||||
break;
|
|
||||||
case 2: res += static_cast<char>((triple >> 2 * 8) & 0xFFU); break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string pad(const std::string& base, const std::string& fill) {
|
|
||||||
std::string padding;
|
|
||||||
switch (base.size() % 4) {
|
|
||||||
case 1: padding += fill; JWT_FALLTHROUGH;
|
|
||||||
case 2: padding += fill; JWT_FALLTHROUGH;
|
|
||||||
case 3: padding += fill; JWT_FALLTHROUGH;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return base + padding;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string trim(const std::string& base, const std::string& fill) {
|
|
||||||
auto pos = base.find(fill);
|
|
||||||
return base.substr(0, pos);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace jwt
|
|
||||||
|
|
||||||
#endif
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue