Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
JWT.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Containers/Sequence.h"
8#include "Stroika/Foundation/DataExchange/BadFormatException.h"
10#include "Stroika/Foundation/Execution/Throw.h"
11
12#include "JWT.h"
13
14using namespace std;
15
16using namespace Stroika::Foundation;
20using namespace Stroika::Foundation::Execution;
21
23
24using Memory::BLOB;
25
26/*
27 ********************************************************************************
28 ********************************** JSON::JWT ***********************************
29 ********************************************************************************
30 */
31JWT::JWT (const String& encodedString, bool fullyValidate)
32{
33 // super quick and dirty incomplete impl
34 // @todo track / validate signature
35 // @todo validate numeric assertions (like enddate - only validating we can convert type to string)
36 Sequence<String> parts = encodedString.Tokenize ({'.'});
37 if (parts.size () != 3) {
38 Throw (BadFormatException{"JWT must contain exactly two . characters"});
39 }
40 BLOB joseHeader = Cryptography::Encoding::Algorithm::Base64::Decode (parts[0]);
41 BLOB payload = Cryptography::Encoding::Algorithm::Base64::Decode (parts[1]);
42 [[maybe_unused]] BLOB signature = Cryptography::Encoding::Algorithm::Base64::Decode (parts[2]);
43 // note these reads and transforms CAN fail if there is bad data
44 VariantValue headerObj = DataExchange::Variant::JSON ::Reader{}.Read (joseHeader);
45 VariantValue payloadObj = DataExchange::Variant::JSON ::Reader{}.Read (payload);
46 fHeaderClaims_ = headerObj.As<Mapping<String, VariantValue>> ().Map<Mapping<String, String>> (
47 [] (auto kvp) { return KeyValuePair<String, String>{kvp.fKey, kvp.fValue.template As<String> ()}; });
48 fPayloadClaims_ = payloadObj.As<Mapping<String, VariantValue>> ().Map<Mapping<String, String>> (
49 [] (auto kvp) { return KeyValuePair<String, String>{kvp.fKey, kvp.fValue.template As<String> ()}; });
50 if (fullyValidate) {
51 // sadly NYI - appears a bit of work - maybe use https://github.com/Thalhammer/jwt-cpp
52 }
53}
54
55String JWT::ToString () const
56{
58 sb << "{"sv;
59 sb << "headerClaims: " << fHeaderClaims_;
60 sb << ", payloadClaims_: " << fPayloadClaims_;
61 sb << "}"sv;
62 return sb;
63}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual Containers::Sequence< String > Tokenize() const
Definition String.cpp:1234
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
JWT(const JWT &)=default
construct a JWT from an encoded string. If fullyValidate true, also validate the signature (NYI)
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:300
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
STL namespace.