sptk2 logo
SPTK Home Page
JWT.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25┌──────────────────────────────────────────────────────────────────────────────┐
26│ The code in this module is based JWT C Library, developed by Ben Collins. │
27│ Please see http://github.com/benmcollins/libjwt for more information. │
28└──────────────────────────────────────────────────────────────────────────────┘
29*/
30
31#pragma once
32
33#include <sptk5/cutils>
34#include <sptk5/xdoc/Document.h>
35#include <sstream>
36
37namespace sptk {
38
42class SP_EXPORT JWT
43{
44public:
46 enum class Algorithm : uint8_t
47 {
48 NONE = 0,
49 HS256,
50 HS384,
51 HS512,
52 RS256,
53 RS384,
54 RS512,
55 ES256,
56 ES384,
57 ES512,
58 INVAL
59 };
60
61 Algorithm alg {Algorithm::NONE};
64
68 JWT() = default;
69
77 const Variant& get(const String& name) const
78 {
79 return grants.root()->findOrCreate(name)->getValue();
80 }
81
89 void set(const String& name, const Variant& data) const
90 {
91 grants.root()->set(name, data);
92 }
93
94 /*
95 * Get signature encryption algorithm
96 * @return signature encryption algorithm
97 */
98 Algorithm get_alg() const;
99
105 void set_alg(Algorithm _alg, const String& _key);
106
112 static const char* alg_str(Algorithm _alg);
113
119 static Algorithm str_alg(const char* alg);
120
126 void sign(Buffer& token, const char* str) const;
127
132 void encode(std::ostream& out) const;
133
139 void decode(const char* token, const String& _key = "");
140
146 void exportTo(std::ostream& output, bool pretty) const;
147
155 static String get_js_string(const xdoc::SNode& node, const String& key, bool* found = nullptr);
156
164 static long get_js_int(const xdoc::SNode& node, const String& key, bool* found = nullptr);
165
173 static bool get_js_bool(const xdoc::SNode& node, const String& key, bool* found = nullptr);
174
180 void write_head(std::ostream& output, bool pretty) const;
181
187 void write_body(std::ostream& output, bool pretty) const;
188
194 void verify(const Buffer& head, const Buffer& sig) const;
195
201 void sign_sha_hmac(Buffer& out, const char* str) const;
202
208 void verify_sha_hmac(const char* head, const char* sig) const;
209
215 void sign_sha_pem(Buffer& out, const char* str) const;
216
222 void verify_sha_pem(const char* head, const char* sig_b64) const;
223
224private:
231 static xdoc::SNode find_grant(const xdoc::SNode& node, const String& key);
232};
233
238void jwt_base64uri_encode(Buffer& buffer);
239
245void jwt_b64_decode(Buffer& destination, const char* src);
246
247} // namespace sptk
Definition: Buffer.h:51
Definition: JWT.h:43
static String get_js_string(const xdoc::SNode &node, const String &key, bool *found=nullptr)
void decode(const char *token, const String &_key="")
void exportTo(std::ostream &output, bool pretty) const
static const char * alg_str(Algorithm _alg)
void sign_sha_pem(Buffer &out, const char *str) const
void write_body(std::ostream &output, bool pretty) const
void set(const String &name, const Variant &data) const
Definition: JWT.h:89
static Algorithm str_alg(const char *alg)
void encode(std::ostream &out) const
void verify_sha_hmac(const char *head, const char *sig) const
void verify(const Buffer &head, const Buffer &sig) const
xdoc::Document grants
Token content.
Definition: JWT.h:63
static bool get_js_bool(const xdoc::SNode &node, const String &key, bool *found=nullptr)
const Variant & get(const String &name) const
Definition: JWT.h:77
void verify_sha_pem(const char *head, const char *sig_b64) const
void write_head(std::ostream &output, bool pretty) const
JWT()=default
void set_alg(Algorithm _alg, const String &_key)
Algorithm
Definition: JWT.h:47
String key
Signature encryption key.
Definition: JWT.h:62
void sign(Buffer &token, const char *str) const
static long get_js_int(const xdoc::SNode &node, const String &key, bool *found=nullptr)
void sign_sha_hmac(Buffer &out, const char *str) const
Definition: String.h:49
Definition: Variant.h:372
Definition: Document.h:34

Fri Oct 14 2022 09:58:31: SPTK 5.4.1