1#ifndef TENDUKE_EXAMPLES_UTILS_H
2#define TENDUKE_EXAMPLES_UTILS_H
4#include "licensing/checkout/LicenseCheckoutResponse.h"
5#include "licensing/release/LicenseReleaseResponse.h"
6#include "licensing/renew/LicenseRenewResponse.h"
7#include "oidc/OIDCState.h"
19 const std::string DEMO_DEPLOYMENT_URL =
"https://your-deployment-url-here";
21 const std::string HTTP_MESSAGE_WHEN_LOGIN_SUCCEEDS =
23 "Content-Type: text/html; charset=utf-8\n"
27 " <title>Login successful, you can close the tab</title>\n"
30 " display: flex; flex-direction: column; justify-content: center;\n"
31 " min-height: 100vh;\n"
34 " text-align: center;\n"
39 " <div class=\"center\">\n"
40 " <H1>Login successful</H1>\n"
41 " <p>You can close this tab now</p>\n"
65 auto fromEnvironment = std::getenv(
"XD_DEPLOYMENT_URL");
66 if (fromEnvironment == NULL) {
67 return ::tenduke::eg::DEMO_DEPLOYMENT_URL;
69 return fromEnvironment;
72 inline void setExpiresAt(
73 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
74 std::int64_t expiresAt
79 inline void setRefreshToken(
80 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
81 const std::string& refreshToken
86 inline void dumpToConsole(const ::tenduke::ee::licensing::Lease &lease)
90 <<
": id=" << lease.licenseId
91 <<
", leaseId = " << lease.leaseId
92 <<
", expires = " << lease.leaseExpiresEpochS
93 << (lease.consumedVersion.empty() ?
"" :
" , version = " + lease.consumedVersion)
97 inline void dumpToConsole(
const std::vector<tenduke::ee::licensing::Lease> &leases)
99 for (
auto const &lease : leases) {
100 dumpToConsole(lease);
104 inline void dumpToConsole(
const std::map<std::string, tenduke::ee::licensing::Lease> &leases)
106 std::vector<tenduke::ee::licensing::Lease> list;
107 list.reserve(leases.size());
108 for (
auto const &lease : leases) {
109 list.push_back(lease.second);
114 inline void dumpToConsole(
const tenduke::ee::licensing::LicenseCheckoutResponse &response)
116 if (response.
leases.empty()) {
117 std::cout <<
"No items successfully checked out" << std::endl;
120 std::cout <<
"Successfully checked out items:" << std::endl;
122 dumpToConsole(response.
leases);
125 std::cout <<
"Failed items:" << std::endl;
126 for (
auto const &failedItem: response.
errors) {
127 std::cout <<
" - " << failedItem.first <<
":" << std::endl;
128 std::cout <<
" error code: " << failedItem.second.getErrorCode() << std::endl;
129 std::cout <<
" error key: " << failedItem.second.getErrorKey() << std::endl;
130 std::cout <<
" error message: " << failedItem.second.getErrorMessage() << std::endl;
131 std::cout <<
" technical: " << failedItem.second.getErrorTechnical() << std::endl;
136 inline void dumpToConsole(
const tenduke::ee::licensing::LicenseRenewResponse &response)
138 if (response.
leases.empty()) {
139 std::cout <<
"No leases successfully renewed" << std::endl;
142 std::cout <<
"Successfully renewed leases:" << std::endl;
144 dumpToConsole(response.
leases);
147 std::cout <<
"Failed items:" << std::endl;
148 for (
auto const &failedItem: response.
errors) {
149 std::cout <<
" - " << failedItem.first <<
":" << std::endl;
150 std::cout <<
" error code: " << failedItem.second.getErrorCode() << std::endl;
151 std::cout <<
" error key: " << failedItem.second.getErrorKey() << std::endl;
152 std::cout <<
" error message: " << failedItem.second.getErrorMessage() << std::endl;
153 std::cout <<
" technical: " << failedItem.second.getErrorTechnical() << std::endl;
161 if (response.leaseIds.empty()) {
162 std::cout <<
"No leases successfully released" << std::endl;
165 std::cout <<
"Successfully released leases:" << std::endl;
167 for (
auto const &leaseId: response.leaseIds) {
168 std::cout <<
" * " << leaseId << std::endl;
171 if (response.hasErrors()) {
172 std::cout <<
"Failed leases:" << std::endl;
173 for (
auto const &failedItem: response.errors) {
174 std::cout <<
" - " << failedItem.first <<
":" << std::endl;
175 std::cout <<
" error code: " << failedItem.second.getErrorCode() << std::endl;
176 std::cout <<
" error key: " << failedItem.second.getErrorKey() << std::endl;
177 std::cout <<
" error message: " << failedItem.second.getErrorMessage() << std::endl;
178 std::cout <<
" technical: " << failedItem.second.getErrorTechnical() << std::endl;
192 const std::size_t visibleChars = 6;
196 if (token.length() <= visibleChars * 4) {
197 return "(" + std::to_string(token.length()) +
" characters)";
199 return "..." + token.substr(token.length() - visibleChars) +
" (" + std::to_string(token.length())
205 std::cout <<
"OIDC-state:" << std::endl;
207 std::cout <<
" - ID-token: " << std::endl;
215 std::cout <<
" - not refreshable" << std::endl;
217 std::cout <<
" - expires: " << state.
getExpiresAt() << std::endl;
220 inline void dumpToConsole(
const std::shared_ptr<const tenduke::oidc::OIDCState> &state)
222 if (state ==
nullptr) {
223 std::cout <<
"No OIDC state" << std::endl;
226 dumpToConsole(*state);
const std::map< std::string, tenduke::ee::licensing::Lease > leases
Leases, that is, successfully checked out licenses.
Definition LicenseCheckoutResponse.h:50
const std::map< std::string, tenduke::ee::licensing::LicensingError > errors
Errors of this response.
Definition LicensingResponse.h:50
bool hasErrors() const
Checks if there are any errors.
Definition LicensingResponse.h:42
const std::string & getIssuer() const
Returns "Issuer", "iss"-claim.
Definition JWT.cpp:123
const std::string & getSubject() const
Returns "Subject", "sub"-claim.
Definition JWT.cpp:129
const std::string & getRefreshToken() const override
Returns the refresh token.
Definition OAuthStateImpl.h:55
bool isRefreshable() const override
Returns true if the state is refreshable (i.e.
Definition OAuthStateImpl.h:98
std::int64_t getExpiresAt() const override
Returns timestamp (as epoch seconds) when the current access token expires.
Definition OAuthStateImpl.h:60
const std::string & getAccessToken() const override
Returns the access token.
Definition OAuthStateImpl.h:50
void setExpiresAt(std::int64_t newExpiresAt) override
Set the "expires at".
Definition OAuthStateImpl.h:108
void setRefreshToken(const std::string &newRefreshToken) override
Set the refresh token.
Definition OAuthStateImpl.h:114
Container of OIDC state, describing the user session.
Definition OIDCState.h:20
const tenduke::oidc::IdToken & getIdToken() const
Returns the contained ID-token.
Definition OIDCState.h:67
LicenseReleaseResponse(const std::vector< std::string > &leaseIds, const std::map< std::string, tenduke::ee::licensing::LicensingError > &errors)
Response from tenduke::ee::licensing::LicenseReleaseRequest.
Definition LicenseReleaseResponse.h:25
Examples.
Definition demo_utils.h:17
std::string maskToken(const std::string &token)
Masks a token for printing: shows only the length and the last characters, so that the tokens can be ...
Definition demo_utils.h:190
std::string getDeploymentURL(int argc, char **argv)
Returns deployment URL.
Definition demo_utils.h:63
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4