This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rl-keygen.cc
189 lines (162 loc) · 5.52 KB
/
rl-keygen.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/algorithm/string.hpp>
#include "keygen.hh"
namespace po = boost::program_options;
struct options {
po::options_description generic;
po::options_description configuration;
po::options_description hidden;
po::positional_options_description pdesc;
po::options_description cmdline_options;
po::options_description config_file_options;
po::options_description visible;
options() :
generic("Generic options"),
configuration("Configuration"),
hidden("Hidden options"),
visible("Allowed options")
{
generic.add_options()
("help", "Show help message")
;
}
void setup() {
cmdline_options.add(generic).add(configuration).add(hidden);
config_file_options.add(configuration).add(hidden);
visible.add(generic).add(configuration);
}
};
static void showhelp(options &opts, std::ostream &os = std::cerr) {
std::cerr << opts.visible << std::endl;
}
static void parse_args(options &opts, int argc, char *argv[]) {
po::variables_map vm;
try {
opts.setup();
po::store(po::command_line_parser(argc, argv)
.options(opts.cmdline_options).positional(opts.pdesc).run(), vm);
po::notify(vm);
if (vm.count("help")) {
showhelp(opts);
exit(1);
}
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl << std::endl;
showhelp(opts);
exit(1);
}
}
static boost::posix_time::ptime expire_string_to_time(const std::string &expire) {
using namespace boost::gregorian;
using namespace boost::posix_time;
if (expire.size() < 2) {
throw ten::errorx("invalid expire time: %s", expire.c_str());
}
std::vector<std::string> splits;
boost::split(splits, expire, boost::is_any_of("/-"));
if (splits.size() == 3) {
date expire_date(
boost::lexical_cast<unsigned int>(splits[0]),
boost::lexical_cast<unsigned int>(splits[1]),
boost::lexical_cast<unsigned int>(splits[2])
);
return ptime(expire_date);
} else {
unsigned int count = boost::lexical_cast<unsigned int>(expire.substr(0, expire.size()-1));
char scale = expire[expire.size()-1];
ptime now(second_clock::local_time());
switch (scale) {
case 'd':
{
ptime expire_time(now.date());
expire_time += days(count);
return expire_time;
}
case 'm':
{
ptime expire_time(now.date());
expire_time += months(count);
return expire_time;
}
case 'y':
{
ptime expire_time(now.date());
expire_time += years(count);
return expire_time;
}
case 'H':
{
ptime expire_time(now);
expire_time += hours(count);
return expire_time;
}
case 'M':
{
ptime expire_time(now);
expire_time += minutes(count);
return expire_time;
}
case 'S':
{
ptime expire_time(now);
expire_time += seconds(count);
return expire_time;
}
default:
throw ten::errorx("invalid expire time: %s", expire.c_str());
break;
}
}
throw ten::errorx("invalid expire time: %s", expire.c_str());
}
struct config {
std::string secret;
std::string expire;
uint64_t org_id;
uint16_t app_id;
uint32_t credits;
};
static config conf;
int main(int argc, char *argv[]) {
options opts;
opts.configuration.add_options()
("secret", po::value<std::string>(&conf.secret), "hmac secret key")
("expire", po::value<std::string>(&conf.expire)->default_value("1y"), "expire time (1d, 1m, 1y)")
("org_id", po::value<uint64_t>(&conf.org_id)->default_value(0), "organization id the key is issued to")
("app_id", po::value<uint16_t>(&conf.app_id)->default_value(0), "app id within org the key is issued to")
("credits", po::value<uint32_t>(&conf.credits)->default_value(0), "credits given to the key for the reset duration")
;
parse_args(opts, argc, argv);
if (conf.secret.empty()) {
std::cerr << "Error: secret key is required\n\n";
showhelp(opts);
exit(1);
}
if (conf.org_id == 0) {
std::cerr << "Error: org_id is required\n\n";
showhelp(opts);
exit(1);
}
try {
uint64_t expires = 0;
if (!conf.expire.empty()) {
boost::posix_time::ptime expire_ptime = expire_string_to_time(conf.expire);
expires = to_time_t(expire_ptime);
}
key_engine eng(conf.secret);
std::string key = eng.generate(conf.org_id, conf.app_id, conf.credits, expires);
apikey akey;
if (eng.verify(key, akey)) {
std::cout << key << "\n";
std::cerr << akey.data << "\n";
} else {
exit(1);
}
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
}
return 0;
}