10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
AbstractLicenseTokenAuthzApiRequest.h
1#ifndef TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
2#define TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
3
4#include "./AbstractAuthzApiRequest.h"
5#include "../../InvalidLicenseToken.h"
6#include "../../LicenseTokenParser.h"
7
8#include "http/HTTPClient.h"
9#include "http/HTTPRequestAuthenticator.h"
10#include "http/HTTPResponseToException.h"
11#include "log/log.h"
12
13#include <memory>
14#include <string>
15
16
17namespace tenduke { namespace ee { namespace licensing { namespace authzapi {
18
19
25template<class T>
27{
28public:
29 virtual ~AbstractLicenseTokenAuthzApiRequest() = default;
30
39 const std::shared_ptr<const ::tenduke::http::HTTPClient> &httpClient,
40 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> &httpRequestAuthenticator,
41 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> &throwException,
42 const std::shared_ptr<const ::tenduke::ee::licensing::impl::LicenseTokenParser> &licenseTokenParser,
43 const std::shared_ptr<::tenduke::log::Logger> logger
44 ) : ::tenduke::ee::licensing::authzapi::AbstractAuthzApiRequest<T>(httpClient, httpRequestAuthenticator, throwException, logger)
45 , parseLicenseToken(licenseTokenParser)
46 {}
47
48protected:
59 T parseResponsePayload(const std::string &payload) const override
60 {
61 std::map<std::string, ::tenduke::ee::licensing::Lease> items;
62 std::map<std::string, ::tenduke::ee::licensing::LicensingError> failedItems;
63 int startOfToken = 0;
64
65 // If the response contains multiple licensed items, each of them are
66 // encoded separately as JWT and the JWTs are concatenated together. This parses those out.
67 std::size_t payloadLength = payload.length();
68 for (int i = 0; i < payloadLength; i++) {
69 if (payload.at(i) == '&') {
70 handleToken(payload.substr(startOfToken, i - startOfToken), items, failedItems);
71
72 startOfToken = i + 1;
73 }
74 }
75 // Handle last token:
76 if (startOfToken < payloadLength) {
77 handleToken(payload.substr(startOfToken), items, failedItems);
78 }
79 return {items, failedItems};
80 }
81
82private:
83 void handleToken(
84 const std::string &token,
85 std::map<std::string, ::tenduke::ee::licensing::Lease> &items,
86 std::map<std::string, ::tenduke::ee::licensing::LicensingError> &failedItems
87 ) const
88 {
89 try {
90 const auto leaseOrError = parseLicenseToken->from(token);
91 if (leaseOrError.isLease()) {
92 items.emplace(leaseOrError.lease.name, leaseOrError.lease);
93 }
94 else {
95 failedItems.emplace(leaseOrError.error.getItemName(), leaseOrError.error);
96 }
97 }
98 catch (const ::tenduke::ee::licensing::InvalidLicenseToken &e) {
100 std::string("AbstractLicenseTokenAuthzApiRequest Invalid license token in the response: ") + e.what()
101 );
103 error.setItemName(e.getLicensedItemName());
104 error.setErrorCode("invalidLicenseToken");
105 error.setErrorMessage(e.what());
106 failedItems.emplace(error.getItemName(), error);
107 }
108 }
109
110private:
111 const std::shared_ptr<const ::tenduke::ee::licensing::impl::LicenseTokenParser> parseLicenseToken;
112};
113
114
115}}}}
116
117#endif // TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
AbstractLicenseTokenAuthzApiRequest(const std::shared_ptr< const ::tenduke::http::HTTPClient > &httpClient, const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > &httpRequestAuthenticator, const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > &throwException, const std::shared_ptr< const ::tenduke::ee::licensing::impl::LicenseTokenParser > &licenseTokenParser, const std::shared_ptr<::tenduke::log::Logger > logger)
Constructs new instance.g.
Definition AbstractLicenseTokenAuthzApiRequest.h:38
Describes an error returned from the API.
Definition LicensingError.h:18
void setItemName(const std::string &pItemName)
Sets the "item" name.
Definition LicensingError.h:80
Base class for authz-API HTTP requests.
Definition AbstractAuthzApiRequest.h:24
AbstractAuthzApiRequest(const std::shared_ptr< const ::tenduke::http::HTTPClient > &httpClient, const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > &httpRequestAuthenticator, const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > &throwException, const std::shared_ptr<::tenduke::log::Logger > &logger)
Constructs new instance.
Definition AbstractAuthzApiRequest.h:34
T parseResponsePayload(const std::string &payload) const override
Parses the response payload.
Definition AbstractLicenseTokenAuthzApiRequest.h:59
AbstractLicenseTokenAuthzApiRequest(const std::shared_ptr< const ::tenduke::http::HTTPClient > &httpClient, const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > &httpRequestAuthenticator, const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > &throwException, const std::shared_ptr< const ::tenduke::ee::licensing::impl::LicenseTokenParser > &licenseTokenParser, const std::shared_ptr<::tenduke::log::Logger > logger)
Constructs new instance.g.
Definition AbstractLicenseTokenAuthzApiRequest.h:38
Low-level client for licensing operations using 10Duke authz-API.
Definition AuthzLicensingClient.h:13
Classes, functions and globals related to 10Duke Enterprise licensing.
Definition AuthzLicensingClient.h:13
Classes, functions and globals of 10Duke Enterprise C++ Client.
Definition APIRequest.h:4
void warning(const char *message)
Write message to global logger at WARNING-level.
Definition log.cpp:106
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4