10Duke Enterprise C++ Client
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 <cstdlib>
10#include <iostream>
11#include <memory>
12#include <string>
13
14namespace tenduke {namespace eg {
15
16 const std::string DEMO_DEPLOYMENT_URL = "https://your-deployment-url-here";
17
18 const std::string HTTP_MESSAGE_WHEN_LOGIN_SUCCEEDS =
19 "HTTP/1.1 200 OK\n"
20 "Content-Type: text/html; charset=utf-8\n"
21 "\n"
22 "<html>\n"
23 "<head>\n"
24 " <title>Login successful, you can close the tab</title>\n"
25 " <style>\n"
26 " body {\n"
27 " display: flex; flex-direction: column; justify-content: center;\n"
28 " min-height: 100vh;\n"
29 " }\n"
30 " .center {\n"
31 " text-align: center;\ns"
32 " }\n"
33 " </style>\n"
34 "</head>\n"
35 "<body>\n"
36 " <div class=\"center\">\n"
37 " <H1>Login successful</H1>\n"
38 " <p>You can close this tab now</p>\n"
39 " </div>\n"
40 "</body>\n"
41 "</html>"
42 ;
43 tenduke::oidc::OIDCState * mutate(const std::shared_ptr<const tenduke::oidc::OIDCState> &state)
44 {
45 return (tenduke::oidc::OIDCState*)((void*)state.get());
46 }
47
60 std::string getDeploymentURL(int argc, char **argv)
61 {
62 auto fromEnvironment = std::getenv("XD_DEPLOYMENT_URL");
63 if (fromEnvironment == NULL) {
64 return ::tenduke::eg::DEMO_DEPLOYMENT_URL;
65 }
66 return fromEnvironment;
67 }
68
69 void setExpiresAt(
70 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
71 std::int64_t expiresAt
72 ) {
73 mutate(state)->setExpiresAt(expiresAt);
74 }
75
76 void setRefreshToken(
77 const std::shared_ptr<const tenduke::oidc::OIDCState> &state,
78 const std::string& refreshToken
79 ) {
80 mutate(state)->setRefreshToken(refreshToken);
81 }
82
83 void dumpToConsole(const ::tenduke::ee::licensing::Lease &lease)
84 {
85 std::cout << " * "
86 << lease.name
87 << ": id=" << lease.licenseId
88 << ", leaseId = " << lease.leaseId
89 << ", expires = " << lease.leaseExpiresEpochS
90 << (lease.consumedVersion.empty() ? "" : " , version = " + lease.consumedVersion)
91 << ", licenseId = " << lease.licenseId
92 << std::endl;
93 }
94
95 void dumpToConsole(const std::vector<tenduke::ee::licensing::Lease> &leases)
96 {
97 for (auto const &lease : leases) {
98 dumpToConsole(lease);
99 }
100 }
101
102 void dumpToConsole(const std::map<std::string, tenduke::ee::licensing::Lease> &leases)
103 {
104 std::vector<tenduke::ee::licensing::Lease> list;
105 list.reserve(leases.size());
106 for (auto const &lease : leases) {
107 list.push_back(lease.second);
108 }
109 dumpToConsole(list);
110 }
111
112 void dumpToConsole(const tenduke::ee::licensing::LicenseCheckoutResponse &response)
113 {
114 if (response.leases.empty()) {
115 std::cout << "No items successfully checked out" << std::endl;
116 }
117 else {
118 std::cout << "Successfully checked out items:" << std::endl;
119
120 dumpToConsole(response.leases);
121 }
122 if (response.hasErrors()) {
123 std::cout << "Failed items:" << std::endl;
124 for (auto const &failedItem: response.errors) {
125 std::cout << " - " << failedItem.first << ":" << std::endl;
126 std::cout << " error code: " << failedItem.second.getErrorCode() << std::endl;
127 std::cout << " error key: " << failedItem.second.getErrorKey() << std::endl;
128 std::cout << " error message: " << failedItem.second.getErrorMessage() << std::endl;
129 std::cout << " technical: " << failedItem.second.getErrorTechnical() << std::endl;
130 }
131 }
132 }
133
134 void dumpToConsole(const tenduke::ee::licensing::LicenseRenewResponse &response)
135 {
136 if (response.leases.empty()) {
137 std::cout << "No leases successfully renewed" << std::endl;
138 }
139 else {
140 std::cout << "Successfully renewed leases:" << std::endl;
141
142 dumpToConsole(response.leases);
143 }
144 if (response.hasErrors()) {
145 std::cout << "Failed items:" << std::endl;
146 for (auto const &failedItem: response.errors) {
147 std::cout << " - " << failedItem.first << ":" << std::endl;
148 std::cout << " error code: " << failedItem.second.getErrorCode() << std::endl;
149 std::cout << " error key: " << failedItem.second.getErrorKey() << std::endl;
150 std::cout << " error message: " << failedItem.second.getErrorMessage() << std::endl;
151 std::cout << " technical: " << failedItem.second.getErrorTechnical() << std::endl;
152 }
153 }
154 }
155
156
157 void dumpToConsole(const tenduke::ee::licensing::LicenseReleaseResponse &response)
158 {
159 if (response.leaseIds.empty()) {
160 std::cout << "No leases successfully released" << std::endl;
161 }
162 else {
163 std::cout << "Successfully released leases:" << std::endl;
164
165 for (auto const &leaseId: response.leaseIds) {
166 std::cout << " * " << leaseId << std::endl;
167 }
168 }
169 if (response.hasErrors()) {
170 std::cout << "Failed leases:" << std::endl;
171 for (auto const &failedItem: response.errors) {
172 std::cout << " - " << failedItem.first << ":" << std::endl;
173 std::cout << " error code: " << failedItem.second.getErrorCode() << std::endl;
174 std::cout << " error key: " << failedItem.second.getErrorKey() << std::endl;
175 std::cout << " error message: " << failedItem.second.getErrorMessage() << std::endl;
176 std::cout << " technical: " << failedItem.second.getErrorTechnical() << std::endl;
177 }
178 }
179 }
180
181 void dumpToConsole(const tenduke::oidc::OIDCState &state)
182 {
183 std::cout << "OIDC-state:" << std::endl;
184 //std::cout << " - id-token: " << state.getIdToken().getSerialized() << std::endl;
185 std::cout << " - ID-token: " << std::endl;
186 std::cout << " - subject: " << state.getIdToken().getSubject() << std::endl;
187 std::cout << " - issuer: " << state.getIdToken().getIssuer() << std::endl;
188 std::cout << " - access token: " << state.getAccessToken() << std::endl;
189 if (state.isRefreshable()) {
190 std::cout << " - refresh token: " <<state.getRefreshToken() << std::endl;
191 }
192 else {
193 std::cout << " - not refreshable" << std::endl;
194 }
195 std::cout << " - expires: " << state.getExpiresAt() << std::endl;
196 }
197
198 void dumpToConsole(const std::shared_ptr<const tenduke::oidc::OIDCState> &state)
199 {
200 if (state == nullptr) {
201 std::cout << "No OIDC state" << std::endl;
202 return;
203 }
204 dumpToConsole(*state);
205 }
206}}
207
208namespace demo = tenduke::eg;
209
210#endif
Response from license checkout request.
Definition LicenseCheckoutResponse.h:24
const std::map< std::string, tenduke::ee::licensing::Lease > leases
Leases, that is, successfully checked out licenses.
Definition LicenseCheckoutResponse.h:48
Response from tenduke::ee::licensing::LicenseReleaseRequest.
Definition LicenseReleaseResponse.h:18
const std::vector< std::string > leaseIds
Id's of successfully released leases.
Definition LicenseReleaseResponse.h:44
Response from license renew -request.
Definition LicenseRenewResponse.h:14
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:71
const std::string & getSubject() const
Returns "Subject", "sub"-claim.
Definition JWT.cpp:77
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:17
const tenduke::oidc::IdToken & getIdToken() const
Returns the contained ID-token.
Definition OIDCState.h:64
Examples.
Definition demo_utils.h:14
std::string getDeploymentURL(int argc, char **argv)
Returns deployment URL.
Definition demo_utils.h:60
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4