Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
JWT.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_JSON_JWT_h_
5#define _Stroika_Foundation_DataExchange_JSON_JWT_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Containers/Mapping.h"
14
15/**
16 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
17 */
19
20 using namespace Stroika::Foundation;
21
24 using Time::DateTime;
25 using Traversal::Range;
26
27 /**
28 * Claim names from https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
29 */
30 namespace JWTRegisteredClaims {
31 /**
32 * \brief The "iss" (issuer) claim identifies the principal that issued the JWT.
33 */
34 constexpr string_view kIssuer = "iss"sv;
35
36 /**
37 * \brief The "sub" (subject) claim identifies the principal that is the subject of the JWT.
38 */
39 constexpr string_view kSubject = "sub"sv;
40
41 /**
42 * \brief The "aud" (audience) claim identifies the recipients that the JWT is intended for.
43 */
44 constexpr string_view kAudience = "aud"sv;
45
46 /**
47 * \brief The "exp" (expiration time) claim identifies the expiration time on or after which
48 * the JWT MUST NOT be accepted for processing
49 */
50 constexpr string_view kExpirationTime = "exp"sv;
51
52 /**
53 * \brief The "nbf" (not before) claim identifies the time before which the JWT
54 * MUST NOT be accepted for processing.
55 */
56 constexpr string_view kNotBefore = "nbf"sv;
57
58 /**
59 * \brief The "iat" (issued at) claim identifies the time at which the JWT was issued
60 */
61 constexpr string_view kIssuedAt = "iat"sv;
62
63 /**
64 * \brief The "jti" (JWT ID) claim provides a unique identifier for the JWT
65 */
66 constexpr string_view kJWTID = "jti"sv;
67 }
68
69 /**
70 * \brief JSON Web Token - a cryptographically signed set of claims - see https://datatracker.ietf.org/doc/html/rfc7519
71 *
72 * \todo consider using/wrapping https://github.com/Thalhammer/jwt-cpp
73 * seems much more complete than what I need right now. What I need for now is quite simple (I THINK) - though more complex to do the validation stuff
74 *
75 * \par Example Usage:
76 * \code
77 * auto encodedJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCIsInNhbXBsZSI6InRlc3QifQ.lQm3N2bVlqt2-1L-FsOjtR6uE-L4E9zJutMWKIe1v1M";
78 * JWT jwt{encodedJWT};
79 * if (auto audience = jwt.GetPayloadClaims ().Lookup (JWTRegisteredClaims::kAudience)) {
80 * cout << "Audience is {}"_f (*audience) << endl;
81 * }
82 * if (auto validFor = jwt.GetValidFor ()) {
83 * cout << "Valid-For is {}"_f (*validFor) << endl;
84 * }
85 * for (auto& claim : jwt.GetPayloadClaims ()) {
86 * DbgTrace ("claim: {}"_f, claim);
87 * }
88 * \endcode
89 *
90 * \todo new CTOR taking set of claims , and producing signature etc..
91 *
92 * \note WARNING: signature validation NYI
93 */
94 class JWT {
95 public:
96 /**
97 * \brief construct a JWT from an encoded string. If fullyValidate true, also validate the signature (NYI)
98 */
99 JWT (const JWT&) = default;
100 JWT (const String& encodedString, bool fullyValidate = true);
101
102 public:
103 /**
104 */
105 nonvirtual Mapping<String, String> GetHeaderClaims () const;
106
107 public:
108 /**
109 * \brief this is probably what you want to look at
110 */
111 nonvirtual Mapping<String, String> GetPayloadClaims () const;
112
113 public:
114 /**
115 */
116 nonvirtual optional<String> GetIssuer () const;
117
118 public:
119 /**
120 */
121 nonvirtual optional<String> GetSubject () const;
122
123 public:
124 /**
125 */
126 nonvirtual optional<String> GetAudience () const;
127
128 public:
129 /**
130 */
131 nonvirtual optional<DateTime> GetExpirationTime () const;
132
133 public:
134 /**
135 */
136 nonvirtual optional<DateTime> GetNotBefore () const;
137
138 public:
139 /**
140 * \brief combined exp and nbf, to produce a datetime range the token is to be considered valid
141 */
142 nonvirtual optional<Range<DateTime>> GetValidFor () const;
143
144 public:
145 /**
146 */
147 nonvirtual optional<DateTime> GetIssuedAt () const;
148
149 public:
150 /**
151 */
152 nonvirtual optional<String> GetJWTID () const;
153
154 public:
155 /**
156 */
157 nonvirtual String ToString () const;
158
159 private:
160 Mapping<String, String> fHeaderClaims_;
161 Mapping<String, String> fPayloadClaims_;
162 };
163
164}
165
166/*
167 ********************************************************************************
168 ***************************** Implementation Details ***************************
169 ********************************************************************************
170 */
171#include "JWT.inl"
172
173#endif /*_Stroika_Foundation_DataExchange_JSON_JWT_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
JSON Web Token - a cryptographically signed set of claims - see https://datatracker....
Definition JWT.h:94
JWT(const JWT &)=default
construct a JWT from an encoded string. If fullyValidate true, also validate the signature (NYI)
nonvirtual Mapping< String, String > GetPayloadClaims() const
this is probably what you want to look at
Definition JWT.inl:18
nonvirtual optional< Range< DateTime > > GetValidFor() const
combined exp and nbf, to produce a datetime range the token is to be considered valid
Definition JWT.inl:48
constexpr string_view kIssuedAt
The "iat" (issued at) claim identifies the time at which the JWT was issued.
Definition JWT.h:61
constexpr string_view kSubject
The "sub" (subject) claim identifies the principal that is the subject of the JWT.
Definition JWT.h:39
constexpr string_view kExpirationTime
The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT b...
Definition JWT.h:50
constexpr string_view kAudience
The "aud" (audience) claim identifies the recipients that the JWT is intended for.
Definition JWT.h:44
constexpr string_view kIssuer
The "iss" (issuer) claim identifies the principal that issued the JWT.
Definition JWT.h:34
constexpr string_view kJWTID
The "jti" (JWT ID) claim provides a unique identifier for the JWT.
Definition JWT.h:66
constexpr string_view kNotBefore
The "nbf" (not before) claim identifies the time before which the JWT MUST NOT be accepted for proces...
Definition JWT.h:56