10Duke Enterprise C++ Client
Loading...
Searching...
No Matches
JWT.h
1#ifndef TENDUKE_JWT_JWT_H
2#define TENDUKE_JWT_JWT_H
3
4#include <cstdint>
5#include <map>
6#include <string>
7#include <vector>
8
9namespace tenduke { namespace jwt {
10
12const std::string CLAIM_JTI = "jti";
14const std::string CLAIM_ISS = "iss";
16const std::string CLAIM_SUB = "sub";
18const std::string CLAIM_AUD = "aud";
20const std::string CLAIM_EXP = "exp";
22const std::string CLAIM_IAT = "iat";
23
25const std::string STRING_NOT_PRESENT;
26
31class JWT
32{
33public:
35 static const std::int64_t NUMBER_NOT_PRESENT = INT64_MIN;
36
41 explicit JWT(std::map<std::string, std::string> claims);
42
50 JWT(
51 const std::map<std::string, std::string> &claims,
52 const std::map<std::string, std::string> &headerParameters,
53 const std::map<std::string, std::map<std::string, std::string>> &mapClaims = {},
54 const std::map<std::string, std::vector<std::string>> &arrayClaims = {}
55 );
56
61 JWT(const JWT &copy) = default;
62
68 bool hasClaim(const std::string &name) const;
69
75 const std::string * claim(const std::string &name) const;
76
81 const std::map<std::string, std::string> & getClaims() const {return claims;};
82
88 const std::string & getStringClaim(const std::string & claimName) const;
89
95 std::int64_t getIntClaim(const std::string & claimName) const;
96
101 const std::string & getJWTID() const;
102
107 const std::string & getIssuer() const;
108
113 const std::string & getSubject() const;
114
119 const std::string & getAudience() const;
120
125 std::int64_t getExpirationTime() const;
126
131 std::int64_t getIssuedAt() const;
132
137 std::string getHeaderParameterKID() const;
138
143 std::map<std::string, std::string> getHeaderParameters() const;
144
152 std::map<std::string, std::string> getMapClaim(const std::string &name) const;
153
161 std::vector<std::string> getArrayClaim(const std::string &name) const;
162
168 bool isInfinite() const;
169
170protected:
172 std::map<std::string, std::string> claims;
174 std::map<std::string, std::string> headerParameters;
176 std::map<std::string, std::map<std::string, std::string>> mapClaims;
178 std::map<std::string, std::vector<std::string>> arrayClaims;
179};
180
181
182}}
183
184#endif // TENDUKE_JWT_JWT_H
Very simple JWT implementation.
Definition JWT.h:32
const std::string & getJWTID() const
Returns "JWT ID", "jti"-claim.
Definition JWT.cpp:65
const std::string & getAudience() const
Returns "Audience", "aud"-claim.
Definition JWT.cpp:83
std::map< std::string, std::map< std::string, std::string > > mapClaims
Claims with values of maps.
Definition JWT.h:176
const std::map< std::string, std::string > & getClaims() const
Returns all claims as a map.
Definition JWT.h:81
std::map< std::string, std::string > getMapClaim(const std::string &name) const
Returns a claim, which is a map.
Definition JWT.cpp:125
std::vector< std::string > getArrayClaim(const std::string &name) const
Returns a claim, which is a JSON array, as vector of strings.
Definition JWT.cpp:134
static const std::int64_t NUMBER_NOT_PRESENT
Magic value to indicate that numeric claim is not present.
Definition JWT.h:35
std::map< std::string, std::string > headerParameters
Header parameters.
Definition JWT.h:174
const std::string & getIssuer() const
Returns "Issuer", "iss"-claim.
Definition JWT.cpp:71
JWT(const JWT &copy)=default
Copy-constructs new JWT instance.
std::string getHeaderParameterKID() const
Returns header-parameter KeyId ("kid"-parameter).
Definition JWT.cpp:107
std::map< std::string, std::string > claims
Claims.
Definition JWT.h:172
std::int64_t getIntClaim(const std::string &claimName) const
Returns claim value as std::int64_t.
Definition JWT.cpp:53
std::int64_t getIssuedAt() const
Returns "Issued at", "iat"-claim.
Definition JWT.cpp:95
std::map< std::string, std::string > getHeaderParameters() const
Returns header parameters.
Definition JWT.cpp:101
std::map< std::string, std::vector< std::string > > arrayClaims
Claims with values of arrays.
Definition JWT.h:178
const std::string & getSubject() const
Returns "Subject", "sub"-claim.
Definition JWT.cpp:77
const std::string & getStringClaim(const std::string &claimName) const
Returns a claim value as string.
Definition JWT.cpp:41
std::int64_t getExpirationTime() const
Returns "Expiration time", "exp"-claim.
Definition JWT.cpp:89
bool isInfinite() const
Returns true if token does expire.
Definition JWT.cpp:119
const std::string * claim(const std::string &name) const
Returns a JWT-claim by given name.
Definition JWT.cpp:29
bool hasClaim(const std::string &name) const
Checks if the JWT has given claim.
Definition JWT.cpp:23
Support for JSON Web Tokens.
Definition DefaultJWTParser.h:11
const std::string CLAIM_AUD
Claim name for "Audience".
Definition JWT.h:18
const std::string CLAIM_EXP
Claim name for "Expiration time".
Definition JWT.h:20
const std::string CLAIM_JTI
Claim name for "JWT ID".
Definition JWT.h:12
const std::string CLAIM_IAT
Claim name for "Issued at".
Definition JWT.h:22
const std::string CLAIM_SUB
Claim name for "Subject".
Definition JWT.h:16
const std::string STRING_NOT_PRESENT
Magic value to indicate that a string value is not present.
Definition JWT.h:25
const std::string CLAIM_ISS
Claim name for "Issuer".
Definition JWT.h:14
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4