Skip to content

Commit b605732

Browse files
committed
Add support for feature templates
1 parent d994f64 commit b605732

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

include/cryptolens/ResponseParser_ArduinoJson5.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ResponseParser_ArduinoJson5 {
3636

3737
std::vector<Message> parse_get_messages_response(basic_Error & e, std::string const& server_response) const;
3838
std::string parse_last_message_response(basic_Error & e, std::string const& server_response) const;
39+
40+
bool has_template_feature(basic_Error & e, std::string const& features_json, std::string const& feature) const;
3941
};
4042

4143
} // namespace latest

include/cryptolens/ResponseParser_ArduinoJson7.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ResponseParser_ArduinoJson7 {
2929
void parse_deactivate_response(basic_Error & e, std::string const& server_response) const;
3030
std::string parse_create_trial_key_response(basic_Error & e, std::string const& server_response) const;
3131
std::string parse_last_message_response(basic_Error & e, std::string const& server_response) const;
32+
33+
bool has_template_feature(basic_Error & e, std::string const& features_json, std::string const& feature) const;
3234
};
3335

3436
} // namespace v20190401

include/cryptolens/basic_Cryptolens.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ class basic_Cryptolens
343343
optional<LicenseKey>
344344
make_license_key(basic_Error & e, std::string const& s);
345345

346+
bool
347+
license_key_has_template_feature
348+
( basic_Error & e
349+
, LicenseKey const& license_key
350+
, std::string const& feature
351+
);
352+
346353
typename Configuration::ResponseParser response_parser;
347354
typename Configuration::RequestHandler request_handler;
348355
typename Configuration::SignatureVerifier signature_verifier;
@@ -612,6 +619,29 @@ basic_Cryptolens<Configuration>::create_trial_key
612619
return key;
613620
}
614621

622+
template<typename Configuration>
623+
bool
624+
basic_Cryptolens<Configuration>::license_key_has_template_feature
625+
( basic_Error & e
626+
, LicenseKey const& license_key
627+
, std::string const& feature
628+
)
629+
{
630+
if (e) { return false; }
631+
632+
optional<std::vector<DataObject>> const& data_objects = license_key.get_data_objects();
633+
634+
if (!data_objects) { return false; }
635+
636+
for (DataObject const& data_object : *data_objects) {
637+
if (data_object.get_name() == "cryptolens_features") {
638+
return this->response_parser.has_template_feature(e, data_object.get_string_value(), feature);
639+
}
640+
}
641+
642+
return false;
643+
}
644+
615645
template<typename Configuration>
616646
optional<RawLicenseKey>
617647
basic_Cryptolens<Configuration>::activate_

src/ResponseParser_ArduinoJson5.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "imports/ArduinoJson5/ArduinoJson.hpp"
22

3+
#include <algorithm>
4+
35
#include "api.hpp"
46
#include "cryptolens_internals.hpp"
57
#include "LicenseKeyInformation.hpp"
@@ -395,6 +397,71 @@ ResponseParser_ArduinoJson5::parse_last_message_response(basic_Error & e, std::s
395397
return "";
396398
}
397399

400+
bool
401+
ResponseParser_ArduinoJson5::has_template_feature(basic_Error & err, std::string const& features_json, std::string const& feature) const
402+
{
403+
if (err) { return false; }
404+
405+
using namespace ::cryptolens_io::latest::errors;
406+
using namespace ::ArduinoJson;
407+
api::main api;
408+
DynamicJsonBuffer jsonBuffer;
409+
JsonArray & j_ = jsonBuffer.parseArray(features_json);
410+
411+
if (!j_.success()) { err.set(api, Subsystem::Json); return false; }
412+
413+
JsonArray * j = &j_;
414+
415+
using string_const_iterator = std::string::const_iterator;
416+
417+
string_const_iterator p = feature.cbegin();
418+
string_const_iterator const e = feature.cend();
419+
while (p != e && j != nullptr) {
420+
string_const_iterator q = std::find(p, e, '.');
421+
422+
bool found = false;
423+
for (JsonVariant const elem : ((JsonArray const&)*j)) {
424+
char const* cc = nullptr;
425+
JsonArray * jj = nullptr;
426+
if (elem.is<char const*>()) {
427+
cc = elem.as<char const*>();
428+
} else if (elem.is<JsonArray &>()) {
429+
JsonArray & a = elem.as<JsonArray &>();
430+
431+
if (a.is<char const*>(0) && a.is<JsonArray &>(1)) {
432+
cc = a.get<char const*>(0);
433+
jj = &a.get<JsonArray &>(1);;
434+
}
435+
}
436+
437+
if (cc == nullptr) { continue; }
438+
439+
string_const_iterator pp = p;
440+
while (pp != q && *cc != '\0') {
441+
if (*pp != *cc) {
442+
break;
443+
}
444+
445+
++pp;
446+
++cc;
447+
}
448+
449+
if (pp == q && *cc == '\0') {
450+
if (pp != e) { ++pp; }
451+
452+
found = true;
453+
p = pp;
454+
j = jj;
455+
break;
456+
}
457+
}
458+
459+
if (!found) { break; }
460+
}
461+
462+
return p == e;
463+
}
464+
398465
} // namespace v20190401
399466

400467
} // namespace cryptolens_io

src/ResponseParser_ArduinoJson7.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "imports/ArduinoJson7/ArduinoJson.hpp"
22

3+
#include <algorithm>
4+
35
#include "api.hpp"
46
#include "cryptolens_internals.hpp"
57
#include "LicenseKeyInformation.hpp"
@@ -351,6 +353,73 @@ ResponseParser_ArduinoJson7::parse_last_message_response(basic_Error & e, std::s
351353
return "";
352354
}
353355

356+
bool
357+
ResponseParser_ArduinoJson7::has_template_feature(basic_Error & err, std::string const& features_json, std::string const& feature) const
358+
{
359+
if (err) { return false; }
360+
361+
using namespace ::cryptolens_io::latest::errors;
362+
using namespace ::ArduinoJson;
363+
api::main api;
364+
365+
JsonDocument doc;
366+
DeserializationError json_error = deserializeJson(doc, features_json);
367+
if (json_error) { err.set(api, Subsystem::Json); return false; }
368+
369+
if (!doc.is<JsonArray>()) { err.set(api, Subsystem::Json); return false; }
370+
371+
JsonArray j = doc.as<JsonArray>();
372+
373+
using string_const_iterator = std::string::const_iterator;
374+
375+
string_const_iterator p = feature.cbegin();
376+
string_const_iterator const e = feature.cend();
377+
while (p != e && !j.isNull()) {
378+
string_const_iterator q = std::find(p, e, '.');
379+
380+
bool found = false;
381+
for (JsonVariant const elem : j) {
382+
char const* cc = nullptr;
383+
JsonArray jj;
384+
if (elem.is<char const*>()) {
385+
cc = elem.as<char const*>();
386+
} else if (elem.is<JsonArray>()) {
387+
JsonArray a = elem.as<JsonArray>();
388+
389+
if (a.size() >= 2 && a[0].is<char const*>() && a[1].is<JsonArray>()) {
390+
cc = a[0].as<char const*>();
391+
jj = a[1].as<JsonArray>();;
392+
}
393+
}
394+
395+
if (cc == nullptr) { continue; }
396+
397+
string_const_iterator pp = p;
398+
while (pp != q && *cc != '\0') {
399+
if (*pp != *cc) {
400+
break;
401+
}
402+
403+
++pp;
404+
++cc;
405+
}
406+
407+
if (pp == q && *cc == '\0') {
408+
if (pp != e) { ++pp; }
409+
410+
found = true;
411+
p = pp;
412+
j = jj;
413+
break;
414+
}
415+
}
416+
417+
if (!found) { break; }
418+
}
419+
420+
return p == e;
421+
}
422+
354423
} // namespace v20190401
355424

356425
} // namespace cryptolens_io

0 commit comments

Comments
 (0)