MySQL 9.3.0
Source Code Documentation
jwt.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_TOKEN_JWT_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_TOKEN_JWT_H_
28
29#include <string>
30#include <vector>
31
32#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
33#include "my_rapidjson_size_t.h"
34#endif
35
36#include <rapidjson/document.h>
37
39
40namespace helper {
41
42class Jwt {
43 public:
44 using Value = rapidjson::Value;
45 using Document = rapidjson::Document;
46
47 public:
48 Jwt() {}
49
50 static void parse(const std::string &token, JwtHolder *out);
51 static Jwt create(const JwtHolder &holder);
52 static Jwt create(const std::string &algoritym, Document &payload);
53
54 bool is_valid() const;
55 bool verify(const std::string &secret) const;
56 std::string sign(const std::string &secret) const;
57
58 std::string get_header_claim_algorithm() const;
59 std::string get_header_claim_type() const;
60
61 std::vector<std::string> get_payload_claim_names() const;
62 const Value *get_payload_claim_custom(const std::string &name) const;
63
64 std::string get_token() const {
65 auto result = holder_.parts[0] + "." + holder_.parts[1];
66 if (!holder_.parts[2].empty()) result += "." + holder_.parts[2];
67 return result;
68 }
69
70 private:
71 static std::vector<std::string> get_payload_names(const Value &v);
72
73 bool valid_{false};
77 std::string signature_;
78};
79
80} // namespace helper
81
82#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_TOKEN_JWT_H_
Definition: jwt_holder.h:31
std::string parts[3]
Definition: jwt_holder.h:33
Definition: jwt.h:42
std::vector< std::string > get_payload_claim_names() const
Definition: jwt.cc:161
static void parse(const std::string &token, JwtHolder *out)
Definition: jwt.cc:61
const Value * get_payload_claim_custom(const std::string &name) const
Definition: jwt.cc:165
rapidjson::Value Value
Definition: jwt.h:44
Document header_
Definition: jwt.h:75
std::string signature_
Definition: jwt.h:77
Document payload_
Definition: jwt.h:76
JwtHolder holder_
Definition: jwt.h:74
std::string sign(const std::string &secret) const
Definition: jwt.cc:206
std::string get_header_claim_algorithm() const
Definition: jwt.cc:132
static Jwt create(const JwtHolder &holder)
Definition: jwt.cc:86
std::string get_token() const
Definition: jwt.h:64
Jwt()
Definition: jwt.h:48
bool verify(const std::string &secret) const
Definition: jwt.cc:187
bool valid_
Definition: jwt.h:73
static std::vector< std::string > get_payload_names(const Value &v)
Definition: jwt.cc:150
std::string get_header_claim_type() const
Definition: jwt.cc:141
rapidjson::Document Document
Definition: jwt.h:45
bool is_valid() const
Definition: jwt.cc:172
Define rapidjson::SizeType to be std::uint64_t.
Definition: cache.h:33
struct result result
Definition: result.h:34
Definition: result.h:30