7 #include "imports/std/optional" 9 #include "ActivateError.hpp" 11 #include "basic_Error.hpp" 12 #include "LicenseKey.hpp" 13 #include "LicenseKeyChecker.hpp" 14 #include "LicenseKeyInformation.hpp" 15 #include "RawLicenseKey.hpp" 16 #include "ResponseParser_ArduinoJson5.hpp" 24 template<
typename ResponseParser,
typename SignatureVerifier>
25 optional<RawLicenseKey>
28 , ResponseParser
const& response_parser
29 , SignatureVerifier
const& signature_verifier
30 , std::string
const& response
34 activate_parse_server_error_message(
char const* server_response);
41 , std::string
const& key
42 , std::string
const& machine_code
43 ,
int fields_to_return
46 : license_key_information_(&license_key_information)
47 , product_id_(product_id), key_(&key), machine_code_(&machine_code)
48 , fields_to_return_(fields_to_return), floating_(floating)
52 get_license_key_information()
const {
53 return *license_key_information_;
57 get_product_id()
const 69 get_machine_code()
const 71 return *machine_code_;
75 get_fields_to_return()
const 77 return fields_to_return_;
89 std::string
const* key_;
90 std::string
const* machine_code_;
91 int fields_to_return_;
97 template<
typename SignatureVerifier>
98 optional<RawLicenseKey>
101 , SignatureVerifier
const& signature_verifier
102 , std::string
const& response
105 if (e) {
return nullopt; }
108 if (e) {
return nullopt; }
110 auto x = internal::handle_activate(e, response_parser, signature_verifier, response);
111 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_HANDLE_ACTIVATE_RAW); }
115 template<
typename SignatureVerifier>
117 handle_activate_raw_exn
119 , SignatureVerifier
const& signature_verifier
120 , std::string
const& response
124 optional<RawLicenseKey> raw_license_key =
125 handle_activate(e, signature_verifier, response);
127 if (raw_license_key && !e) {
return *raw_license_key; }
129 throw ActivateError::from_server_response(NULL);
132 template<
typename SignatureVerifier>
136 , SignatureVerifier
const& signature_verifier
137 , std::string
const& response
140 if (e) {
return nullopt; }
143 if (e) {
return nullopt; }
145 optional<RawLicenseKey> x = internal::handle_activate(e, response_parser, signature_verifier, response);
146 optional<LicenseKeyInformation> y = response_parser.make_license_key_information(e, x);
147 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_HANDLE_ACTIVATE); }
149 return LicenseKey(std::move(*y), std::move(*x));
163 template<
typename Configuration>
167 #ifndef CRYPTOLENS_20190701_ALLOW_IMPLICIT_CONSTRUCTORS 171 : response_parser(e), request_handler(e), signature_verifier(e), machine_code_computer(e)
172 , activate_validator(e)
181 ,
int fields_to_return = 0
184 optional<RawLicenseKey>
190 ,
int fields_to_return = 0
199 ,
long floating_time_interval
200 ,
int fields_to_return = 0
209 ,
int fields_to_return = 0
223 , std::string channel
224 ,
int since_unix_timestamp
228 make_license_key(
basic_Error & e, std::string
const& s);
230 typename Configuration::ResponseParser response_parser;
231 typename Configuration::RequestHandler request_handler;
232 typename Configuration::SignatureVerifier signature_verifier;
233 typename Configuration::MachineCodeComputer machine_code_computer;
234 typename Configuration::template ActivateValidator<internal::ActivateEnvironment> activate_validator;
237 optional<RawLicenseKey>
243 , std::string machine_code
244 ,
int fields_to_return = 0
247 optional<RawLicenseKey>
253 , std::string machine_code
254 ,
long floating_time_interval
255 ,
int fields_to_return = 0
269 , std::string channel
270 ,
int since_unix_timestamp
286 template<
typename Configuration>
293 ,
int fields_to_return
296 if (e) {
return nullopt; }
298 std::string machine_code = machine_code_computer.get_machine_code(e);
300 optional<RawLicenseKey> x = this->activate_
308 optional<LicenseKeyInformation> y = response_parser.make_license_key_information(e, x);
309 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_ACTIVATE);
return nullopt; }
312 activate_validator.validate(e, env);
313 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_ACTIVATE);
return nullopt; }
315 return LicenseKey(std::move(*y), std::move(*x));
330 template<
typename Configuration>
331 optional<RawLicenseKey>
337 ,
int fields_to_return
340 if (e) {
return nullopt; }
342 std::string machine_code = machine_code_computer.get_machine_code(e);
344 auto x = this->activate_( e
346 , std::move(product_id)
348 , std::move(machine_code)
351 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_ACTIVATE_RAW); }
377 template<
typename Configuration>
384 ,
long floating_time_interval
385 ,
int fields_to_return
388 if (e) {
return nullopt; }
390 std::string machine_code = machine_code_computer.get_machine_code(e);
392 optional<RawLicenseKey> x = this->activate_floating_
395 , std::move(product_id)
398 , floating_time_interval
401 optional<LicenseKeyInformation> y = response_parser.make_license_key_information(e, x);
402 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_ACTIVATE_FLOATING);
return nullopt; }
405 activate_validator.validate(e, env);
406 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_ACTIVATE_FLOATING);
return nullopt; }
408 return LicenseKey(std::move(*y), std::move(*x));
411 template<
typename Configuration>
419 if (e) {
return ""; }
421 std::string key = create_trial_key_(e, token, product_id);
422 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_CREATE_TRIAL_KEY);
return ""; }
426 template<
typename Configuration>
427 optional<RawLicenseKey>
433 , std::string machine_code
434 ,
int fields_to_return
437 if (e) {
return nullopt; }
439 auto request = request_handler.post_request(e,
"app.cryptolens.io",
"/api/key/Activate");
441 std::ostringstream product_id_; product_id_ << product_id;
442 std::ostringstream fields_to_return_; fields_to_return_ << fields_to_return;
444 std::string response =
445 request.add_argument(e,
"token" , token.c_str())
446 .add_argument(e,
"ProductId" , product_id_.str().c_str())
447 .add_argument(e,
"Key" , key.c_str())
448 .add_argument(e,
"Sign" ,
"true")
449 .add_argument(e,
"MachineCode" , machine_code.c_str())
450 .add_argument(e,
"FieldsToReturn", fields_to_return_.str().c_str())
451 .add_argument(e,
"SignMethod" ,
"1")
452 .add_argument(e,
"v" ,
"1")
455 return handle_activate_raw(e, this->signature_verifier, response);
458 template<
typename Configuration>
459 optional<RawLicenseKey>
465 , std::string machine_code
466 ,
long floating_time_interval
467 ,
int fields_to_return
470 if (e) {
return nullopt; }
472 auto request = request_handler.post_request(e,
"app.cryptolens.io",
"/api/key/Activate");
474 std::ostringstream product_id_; product_id_ << product_id;
475 std::ostringstream fields_to_return_; fields_to_return_ << fields_to_return;
476 std::ostringstream floating_time_interval_; floating_time_interval_ << floating_time_interval;
478 std::string response =
479 request.add_argument(e,
"token" , token.c_str())
480 .add_argument(e,
"ProductId" , product_id_.str().c_str())
481 .add_argument(e,
"Key" , key.c_str())
482 .add_argument(e,
"Sign" ,
"true")
483 .add_argument(e,
"MachineCode" , machine_code.c_str())
484 .add_argument(e,
"FieldsToReturn" , fields_to_return_.str().c_str())
485 .add_argument(e,
"SignMethod" ,
"1")
486 .add_argument(e,
"v" ,
"1")
487 .add_argument(e,
"FloatingTimeInterval", floating_time_interval_.str().c_str())
490 return handle_activate_raw(e, this->signature_verifier, response);
506 template<
typename Configuration>
513 ,
int fields_to_return
518 std::string machine_code = machine_code_computer.get_machine_code(e);
520 optional<RawLicenseKey> raw_license_key =
521 activate_raw( e, std::move(token), std::move(product_id), std::move(key)
522 , std::move(machine_code), fields_to_return);
524 if (!e) {
return *raw_license_key; }
525 throw ActivateError::from_server_response(NULL);
528 template<
typename Configuration>
533 , std::string channel
534 ,
int since_unix_timestamp
537 if (e) {
return ""; }
539 std::string message = last_message_(e, token, channel, since_unix_timestamp);
540 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_LAST_MESSAGE);
return ""; }
544 template<
typename Configuration>
552 if (e) {
return ""; }
554 std::string machine_code = machine_code_computer.get_machine_code(e);
556 auto request = request_handler.post_request(e,
"app.cryptolens.io",
"/api/Key/CreateTrialKey");
558 std::ostringstream product_id_; product_id_ << product_id;
560 std::string response =
561 request.add_argument(e,
"token" , token.c_str())
562 .add_argument(e,
"ProductId" , product_id_.str().c_str())
563 .add_argument(e,
"MachineCode", machine_code.c_str())
566 if (e) {
return ""; }
568 return response_parser.parse_create_trial_key_response(e, response);
571 template<
typename Configuration>
576 , std::string channel
577 ,
int since_unix_timestamp
580 if (e) {
return ""; }
582 auto request = request_handler.post_request(e,
"app.cryptolens.io",
"/api/message/GetMessages");
584 std::ostringstream stm; stm << since_unix_timestamp;
586 std::string response =
587 request.add_argument(e,
"token" , token.c_str())
588 .add_argument(e,
"Channel", channel.c_str())
589 .add_argument(e,
"Time" , stm.str().c_str())
592 if (e) {
return ""; }
594 return response_parser.parse_last_message_response(e, response);
597 template<
typename Configuration>
601 if (e) {
return nullopt; }
603 optional<RawLicenseKey> raw_license_key;
606 ::cryptolens_io::v20190401::internal::handle_activate(e, this->response_parser, this->signature_verifier, s);
611 size_t k = s.find(
'-');
612 if (k == std::string::npos) { e.set(
api::main(), errors::Subsystem::Main, errors::Main::UNKNOWN_SERVER_REPLY);
return nullopt; }
614 std::string version = s.substr(0, k);
615 std::string rem = s.substr(k+1, std::string::npos);
619 if (k == std::string::npos) { e.set(
api::main(), errors::Subsystem::Main, errors::Main::UNKNOWN_SERVER_REPLY);
return nullopt; }
621 std::string license = rem.substr(0, k);
622 std::string signature = rem.substr(k+1, std::string::npos);
633 optional<LicenseKeyInformation> license_key_information = response_parser.make_license_key_information(e, raw_license_key);
634 if (e) { e.set_call(
api::main(), errors::Call::BASIC_SKM_MAKE_LICENSE_KEY);
return nullopt; }
635 return LicenseKey(std::move(*license_key_information), std::move(*raw_license_key));
640 template<
typename ResponseParser,
typename SignatureVerifier>
641 optional<RawLicenseKey>
644 , ResponseParser
const& response_parser
645 , SignatureVerifier
const& signature_verifier
646 , std::string
const& response
649 if (e) {
return nullopt; }
651 optional<std::pair<std::string, std::string>> x = response_parser.parse_activate_response(e, response);
652 if (e) {
return nullopt; }
654 return RawLicenseKey::make
668 template<
typename Configuration>
virtual void reset(api::main api)
Definition: basic_Error.hpp:119
Definition: basic_Cryptolens.hpp:164
RawLicenseKey activate_raw_exn(api::experimental_v1 experimental, std::string token, int product_id, std::string key, int fields_to_return=0)
Definition: basic_Cryptolens.hpp:509
Definition: ActivateError.hpp:5
Definition: RawLicenseKey.hpp:24
Definition: basic_Error.hpp:90
optional< LicenseKey > activate(basic_Error &e, std::string token, int product_id, std::string key, int fields_to_return=0)
Definition: basic_Cryptolens.hpp:289
optional< RawLicenseKey > activate_raw(basic_Error &e, std::string token, int product_id, std::string key, int fields_to_return=0)
Definition: basic_Cryptolens.hpp:333
optional< LicenseKey > activate_floating(basic_Error &e, std::string token, int product_id, std::string key, long floating_time_interval, int fields_to_return=0)
Definition: basic_Cryptolens.hpp:380
Definition: ResponseParser_ArduinoJson5.hpp:15
Definition: basic_Cryptolens.hpp:36
Definition: LicenseKey.hpp:34