-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (81 loc) · 2.35 KB
/
main.cpp
File metadata and controls
99 lines (81 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/base_uri.h>
using namespace std;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;
using namespace utility;
// Verify a license key using the validate-key action
pplx::task<http_response> validate_license_key(http_client client, const string license_key)
{
http_request req;
value meta;
meta["key"] = value::string(license_key);
value body;
body["meta"] = meta;
req.headers().add("Content-Type", "application/vnd.api+json");
req.headers().add("Accept", "application/json");
req.set_request_uri(uri("/licenses/actions/validate-key"));
req.set_method(methods::POST);
req.set_body(body.serialize());
return client.request(req);
}
int main(int argc, char* argv[])
{
if (!getenv("KEYGEN_ACCOUNT_ID"))
{
cerr << "[ERROR] "
<< "No KEYGEN_ACCOUNT_ID environment variable specified"
<< endl;
return 1;
}
if (!argv[1])
{
cerr << "[ERROR] "
<< "No license key argument specified"
<< endl;
return 1;
}
string account_id = getenv("KEYGEN_ACCOUNT_ID");
string license_key = argv[1];
http_client client(uri("https://api.keygen.sh/v1/accounts/" + account_id));
validate_license_key(client, license_key)
.then([](http_response res)
{
auto json = res.extract_json().get();
if (json.has_field("errors"))
{
auto errors = json.at("errors").as_array();
auto err = errors[0];
cerr << "[ERROR] "
<< "API request failed: "
<< "status=" << res.status_code() << " "
<< "title='" << err.at("title").as_string() << "' "
<< "detail='" << err.at("detail").as_string() << "'"
<< endl;
exit(1);
}
auto data = json.at("data");
auto meta = json.at("meta");
if (meta.at("valid").as_bool())
{
cout << "[OK] "
<< "License key is valid: "
<< "code=" << meta.at("code").as_string() << " "
<< "id=" << data.at("id").as_string()
<< endl;
}
else
{
cerr << "[ERROR] "
<< "License key is not valid: "
<< "code=" << meta.at("code").as_string()
<< endl;
}
})
.wait();
}