10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
demo_utils.h
1#ifndef TENDUKE_EXAMPLES_UTILS_H
2#define TENDUKE_EXAMPLES_UTILS_H
3
4#include "licensing/checkout/LicenseCheckoutResponse.h"
5#include "licensing/release/LicenseReleaseResponse.h"
6#include "licensing/renew/LicenseRenewResponse.h"
7#include "oidc/OIDCState.h"
8
9#include <cstdint>
10#include <cstdlib>
11#include <iostream>
12#include <map>
13#include <memory>
14#include <string>
15#include <vector>
16
17namespace tenduke {namespace eg {
18
19 const std::string DEMO_DEPLOYMENT_URL = "https://your-deployment-url-here";
20
21 const std::string HTTP_MESSAGE_WHEN_LOGIN_SUCCEEDS =
22 "HTTP/1.1 200 OK\n"
23 "Content-Type: text/html; charset=utf-8\n"
24 "\n"
25 "<html>\n"
26 "<head>\n"
27 " <title>Login successful, you can close the tab</title>\n"
28 " <style>\n"
29 " body {\n"
30 " display: flex; flex-direction: column; justify-content: center;\n"
31 " min-height: 100vh;\n"
32 " }\n"
33 " .center {\n"
34 " text-align: center;\n"
35 " }\n"
36 " </style>\n"
37 "</head>\n"
38 "<body>\n"
39 " <div class=\"center\">\n"
40 " <H1>Login successful</H1>\n"
41 " <p>You can close this tab now</p>\n"
42 " </div>\n"
43 "</body>\n"
44 "</html>"
45 ;
46 inline tenduke::oidc::OIDCState * mutate(const std::shared_ptr<const tenduke::oidc::OIDCState> &state)
47 {
48 return (tenduke::oidc::OIDCState*)((void*)state.get());
49 }
50
63 inline std::string getDeploymentURL(int argc, char **argv)
64 {
65 auto fromEnvironment = std::getenv("XD_DEPLOYMENT_URL");
66 if (fromEnvironment == NULL) {
67 return ::tenduke::eg::DEMO_DEPLOYMENT_URL;
68 }
69 return fromEnvironment;
70 }
71
72 inline void setExpiresAt(
73 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
74 std::int64_t expiresAt
75 ) {
76 mutate(state)->setExpiresAt(expiresAt);
77 }
78
79 inline void setRefreshToken(
80 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
81 const std::string& refreshToken
82 ) {
83 mutate(state)->setRefreshToken(refreshToken);
84 }
85
86 inline void dumpToConsole(const ::tenduke::ee::licensing::Lease &lease)
87 {
88 std::cout << " * "
89 << lease.name
90 << ": id=" << lease.licenseId
91 << ", leaseId = " << lease.leaseId
92 << ", expires = " << lease.leaseExpiresEpochS
93 << (lease.consumedVersion.empty() ? "" : " , version = " + lease.consumedVersion)
94 << std::endl;
95 }
96
97 inline void dumpToConsole(const std::vector<tenduke::ee::licensing::Lease> &leases)
98 {
99 for (auto const &lease : leases) {
100 dumpToConsole(lease);
101 }
102 }
103
104 inline void dumpToConsole(const std::map<std::string, tenduke::ee::licensing::Lease> &leases)
105 {
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);
110 }
111 dumpToConsole(list);
112 }
113
114 inline void dumpToConsole(const tenduke::ee::licensing::LicenseCheckoutResponse &response)
115 {
116 if (response.leases.empty()) {
117 std::cout << "No items successfully checked out" << std::endl;
118 }
119 else {
120 std::cout << "Successfully checked out items:" << std::endl;
121
122 dumpToConsole(response.leases);
123 }
124 if (response.hasErrors()) {
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;
132 }
133 }
134 }
135
136 inline void dumpToConsole(const tenduke::ee::licensing::LicenseRenewResponse &response)
137 {
138 if (response.leases.empty()) {
139 std::cout << "No leases successfully renewed" << std::endl;
140 }
141 else {
142 std::cout << "Successfully renewed leases:" << std::endl;
143
144 dumpToConsole(response.leases);
145 }
146 if (response.hasErrors()) {
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;
154 }
155 }
156 }
157
158
159 inline void dumpToConsole(const tenduke::ee::licensing::LicenseReleaseResponse &response)
160 {
161 if (response.leaseIds.empty()) {
162 std::cout << "No leases successfully released" << std::endl;
163 }
164 else {
165 std::cout << "Successfully released leases:" << std::endl;
166
167 for (auto const &leaseId: response.leaseIds) {
168 std::cout << " * " << leaseId << std::endl;
169 }
170 }
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;
179 }
180 }
181 }
182
190 inline std::string maskToken(const std::string &token)
191 {
192 const std::size_t visibleChars = 6;
193 if (token.empty()) {
194 return "(empty)";
195 }
196 if (token.length() <= visibleChars * 4) {
197 return "(" + std::to_string(token.length()) + " characters)";
198 }
199 return "..." + token.substr(token.length() - visibleChars) + " (" + std::to_string(token.length())
200 + " characters)";
201 }
202
203 inline void dumpToConsole(const tenduke::oidc::OIDCState &state)
204 {
205 std::cout << "OIDC-state:" << std::endl;
206 //std::cout << " - id-token: " << state.getIdToken().getSerialized() << std::endl;
207 std::cout << " - ID-token: " << std::endl;
208 std::cout << " - subject: " << state.getIdToken().getSubject() << std::endl;
209 std::cout << " - issuer: " << state.getIdToken().getIssuer() << std::endl;
210 std::cout << " - access token: " << maskToken(state.getAccessToken()) << std::endl;
211 if (state.isRefreshable()) {
212 std::cout << " - refresh token: " << maskToken(state.getRefreshToken()) << std::endl;
213 }
214 else {
215 std::cout << " - not refreshable" << std::endl;
216 }
217 std::cout << " - expires: " << state.getExpiresAt() << std::endl;
218 }
219
220 inline void dumpToConsole(const std::shared_ptr<const tenduke::oidc::OIDCState> &state)
221 {
222 if (state == nullptr) {
223 std::cout << "No OIDC state" << std::endl;
224 return;
225 }
226 dumpToConsole(*state);
227 }
228}}
229
230namespace demo = tenduke::eg;
231
232#endif
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