-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmain.cc
102 lines (94 loc) · 3.54 KB
/
main.cc
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
100
101
102
//
// License++
//
// Copyright © 2018-present @abumq (Majid Q.)
//
// See https://github.com/abumq/licensepp/blob/master/LICENSE
//
#include <cstring>
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include "licensing/license-manager.h"
void displayUsage() {
std::cout << "USAGE: license-manager [--validate <file> --signature <signature>] [--issue --licensee <licensee> --signature <licensee_signature> --period <validation_period> --authority <issuing_authority> --passphrase <passphrase_for_issuing_authority> [--additional-payload <additional data>]]" << std::endl;
}
void displayVersion() {
std::cout << "License Manager v1.1.0" << std::endl;
}
int main(int argc, char* argv[])
{
if (argc > 1 && strcmp(argv[1], "--version") == 0) {
displayVersion();
return 0;
}
if (argc > 1 && strcmp(argv[1], "--help") == 0) {
displayUsage();
return 0;
}
std::string licenseFile;
std::string signature;
std::string licensee;
std::string secret;
std::string authority = "default";
std::string additionalPayload;
unsigned int period = 0U;
bool doIssue = false;
bool doValidate = false;
for (int i = 0; i < argc; i++) {
std::string arg(argv[i]);
if (arg == "--validate" && i < argc) {
licenseFile = argv[++i];
doValidate = true;
} else if (arg == "--signature" && i < argc) {
signature = argv[++i];
} else if (arg == "--issue" && i < argc) {
doIssue = true;
} else if (arg == "--licensee" && i < argc) {
licensee = argv[++i];
} else if (arg == "--period" && i < argc) {
period = static_cast<unsigned int>(atoi(argv[++i]));
} else if (arg == "--authority" && i < argc) {
authority = argv[++i];
} else if (arg == "--passphrase" && i < argc) {
secret = argv[++i];
} else if (arg == "--additional-payload" && i < argc) {
additionalPayload = argv[++i];
}
}
LicenseManager licenseManager;
if (doValidate && !licenseFile.empty()) {
License license;
try {
if (license.loadFromFile(licenseFile)) {
if (!licenseManager.validate(&license, true, signature)) {
std::cout << "License is not valid" << std::endl;
} else {
std::cout << "Licensed to " << license.licensee() << std::endl;
std::cout << "Subscription is active until " << license.formattedExpiry() << std::endl << std::endl;
}
}
} catch (LicenseException& e) {
std::cerr << "Exception thrown " << e.what() << std::endl;
}
} else if (doIssue) {
const licensepp::IssuingAuthority* issuingAuthority = nullptr;
for (const auto& a : LicenseManagerKeyRegister::LICENSE_ISSUING_AUTHORITIES) {
if (a.id() == authority) {
issuingAuthority = &(a);
}
}
if (issuingAuthority == nullptr) {
std::cout << "Invalid issuing authority." << std::endl;
return 1;
}
licensepp::License license = licenseManager.issue(licensee, period, issuingAuthority, secret, signature, additionalPayload);
std::cout << license.toString() << std::endl;
std::cout << "Licensed to " << license.licensee() << std::endl;
std::cout << "Subscription is active until " << license.formattedExpiry() << std::endl << std::endl;
} else {
displayUsage();
}
return 0;
}