10Duke Enterprise C++ Client
Loading...
Searching...
No Matches
StatefulCheckoutRequestTemplate.h
1#ifndef TENDUKE_CLIENT_EE_STATEFULCHECKOUTREQUESTTEMPLATE_H
2#define TENDUKE_CLIENT_EE_STATEFULCHECKOUTREQUESTTEMPLATE_H
3
4#include "../authz/LicensingRequest.h"
5#include "http/Unauthorized.h"
6#include "log/log.h"
7#include "oidc/session/OIDCSession.h"
8
9namespace tenduke { namespace ee { namespace licensing { namespace impl {
10
27template<class T>
29{
30public:
38 const std::shared_ptr<::tenduke::oidc::OIDCSession> &oidc
39 ) : oidc(oidc), request(std::move(request))
40 {}
41
49 {
50 ::tenduke::log::debug("StatefulCheckoutRequest::execute() ensuring valid session...");
51 oidc->ensureValidSession();
52
53 // The response. Wrapped in pointer to workaround assignment issues to const fields
54 std::unique_ptr<T> response = nullptr;
55
56 try {
57 ::tenduke::log::debug("StatefulCheckoutRequest::execute() ...executing the request...");
58 response.reset(new T(request->execute()));
59 }
60 // Since 10Duke Enterprise 5.0: The backend may be configured to throw HTTP 401 when the credentials are
61 // invalid or expired.
62 // Note that the client does not know the backend version nor the configuration, so the old handling below
63 // is retained, even though it might perform duplicate work.
64 catch (const ::tenduke::http::Unauthorized &e) {
65 ::tenduke::log::warning("StatefulCheckoutRequest::execute() ... got HTTP 401.");
66 return reEstablishOIDCSessionAndRetry();
67 }
68
69 // Check if the request failed because credentials were invalid.
70 // On checkout, the /authz-API does not acknowledge missing/expired/removed request-authentication credentials.
71 // To detect this condition, we assume that if the request failed (all the items in the request failed),
72 // we do a heartbeat to the backend. If the heartbeat fails with HTTP 401, we refresh or re-login and
73 // re-try the request.
74 //
75 // This is the old functionality. See above for handling of the ::tenduke::http::Unauthorized.
76 if (response->failed()) {
77 ::tenduke::log::warning("StatefulCheckoutRequest::execute() ... failed. Doing heartbeat to check backend state...");
78
79 if (oidc->heartbeat()) {
80 ::tenduke::log::info("StatefulCheckoutRequest::execute() ... credentials were valid. Returning original response.");
81 }
82 else {
83 ::tenduke::log::warning(" StatefulCheckoutRequest::execute()... credentials were defunct.");
84 return reEstablishOIDCSessionAndRetry();
85 }
86 }
87 else {
88 ::tenduke::log::debug("StatefulCheckoutRequest::execute() ... succeeded");
89 }
90
91 return *response;
92 }
93
94protected:
95 T reEstablishOIDCSessionAndRetry()
96 {
97 ::tenduke::log::info("StatefulCheckoutRequest::reEstablishOIDCSessionAndRetry() Re-estalishing OIDC session...");
98 oidc->reEstablish();
99
100 ::tenduke::log::info("StatefulCheckoutRequest::reEstablishOIDCSessionAndRetry() ... OIDC session re-established. Re-executing original request...");
101 auto response = request->execute();
102
103 ::tenduke::log::info("StatefulCheckoutRequest::reEstablishOIDCSessionAndRetry() ... ready, returning new response as-is.");
104 return response;
105 }
106
107private:
108 const std::shared_ptr<::tenduke::oidc::OIDCSession> oidc;
109 const std::unique_ptr<::tenduke::ee::licensing::authzapi::LicensingRequest<T>> request;
110};
111
112}}}}
113
114
115#endif //TENDUKE_CLIENT_EE_STATEFULCHECKOUTREQUESTTEMPLATE_H
Licensing-related request.
Definition LicensingRequest.h:13
Common base for stateful checkout-like requests.
Definition StatefulCheckoutRequestTemplate.h:29
StatefulCheckoutRequestTemplate(std::unique_ptr<::tenduke::ee::licensing::authzapi::LicensingRequest< T > > request, const std::shared_ptr<::tenduke::oidc::OIDCSession > &oidc)
Constructs new instance.
Definition StatefulCheckoutRequestTemplate.h:36
T execute()
Executes the request.
Definition StatefulCheckoutRequestTemplate.h:48
void debug(const char *message)
Write message to global logger at DEBUG-level.
Definition log.cpp:26
void warning(const char *message)
Write message to global logger at WARNING-level.
Definition log.cpp:66
void info(const char *message)
Write message to global logger at INFO-level.
Definition log.cpp:46
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4