1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-03-04 03:07:29 +01:00
SqMod/vendor/POCO/JWT/src/Serializer.cpp
Sandu Liviu Catalin 4a6bfc086c Major plugin refactor and cleanup.
Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
2021-01-30 08:51:39 +02:00

82 lines
1.8 KiB
C++

//
// Serializer.cpp
//
// Library: JWT
// Package: JWT
// Module: Serializer
//
// Copyright (c) 2019, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JWT/Serializer.h"
#include "Poco/JWT/JWTException.h"
#include "Poco/JSON/Parser.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Base64Decoder.h"
#include "Poco/MemoryStream.h"
#include "Poco/StringTokenizer.h"
#include <sstream>
namespace Poco {
namespace JWT {
std::string Serializer::serialize(const Poco::JSON::Object& object)
{
std::ostringstream stream;
serialize(object, stream);
return stream.str();
}
void Serializer::serialize(const Poco::JSON::Object& object, std::ostream& stream)
{
Poco::Base64Encoder encoder(stream, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
object.stringify(encoder);
encoder.close();
}
Poco::JSON::Object::Ptr Serializer::deserialize(const std::string& serialized)
{
Poco::MemoryInputStream stream(serialized.data(), serialized.size());
return deserialize(stream);
}
Poco::JSON::Object::Ptr Serializer::deserialize(std::istream& stream)
{
Poco::Base64Decoder decoder(stream, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
Poco::JSON::Parser parser;
try
{
Poco::Dynamic::Var json = parser.parse(decoder);
Poco::JSON::Object::Ptr pObject = json.extract<Poco::JSON::Object::Ptr>();
return pObject;
}
catch (Poco::BadCastException&)
{
throw ParseException("String does not deserialize to a JSON object");
}
catch (Poco::Exception& exc)
{
throw ParseException("Failed to deserialize JWT component", exc.displayText());
}
}
std::vector<std::string> Serializer::split(const std::string& token)
{
Poco::StringTokenizer tokenizer(token, ".");
std::vector<std::string> result(tokenizer.begin(), tokenizer.end());
return result;
}
} } // namespace Poco::JWT