Skip to content

Commit cb304f1

Browse files
committed
Improve verification routine to support offline mode.
1 parent ae02941 commit cb304f1

File tree

1 file changed

+109
-5
lines changed

1 file changed

+109
-5
lines changed

src/verify.cpp

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,117 @@
1-
#include <iostream>
21

32
#include "scitokens.h"
43

5-
int main(int argc, const char** argv) {
4+
#include <fstream>
5+
#include <iostream>
6+
#include <getopt.h>
7+
8+
namespace {
9+
10+
const char usage[] = \
11+
"\n"
12+
"Syntax: %s [--cred cred_file] TOKEN\n"
13+
"\n"
14+
" Options\n"
15+
" -h | --help Display usage\n"
16+
" -c | --cred <cred_file> File containing the signing credential.\n"
17+
" -i | --issuer <issuer> Issuer of the token to verify.\n"
18+
" -K | --keyid <kid> Name of the token key.\n"
19+
" -p | --profile <profile> Profile to enforce (wlcg, scitokens1, scitokens2).\n"
20+
"\n";
21+
22+
const struct option long_options[] =
23+
{
24+
{"help", no_argument, NULL, 'h'},
25+
{"cred", required_argument, NULL, 'c'},
26+
{"issuer", required_argument, NULL, 'i'},
27+
{"keyid", required_argument, NULL, 'K'},
28+
{"profile", required_argument, NULL, 'p'},
29+
{0, 0, 0, 0}
30+
};
31+
32+
const char short_options[] = "hc:i:K:p:";
33+
34+
std::string g_cred, g_issuer, g_keyid, g_profile;
35+
36+
int init_arguments(int argc, char * const argv[])
37+
{
38+
int arg;
39+
while((arg = getopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
40+
switch (arg)
41+
{
42+
case 'h':
43+
printf(usage, argv[0]);
44+
exit(0);
45+
break;
46+
case 'c':
47+
g_cred = optarg;
48+
break;
49+
case 'i':
50+
g_issuer = optarg;
51+
break;
52+
case 'K':
53+
g_keyid = optarg;
54+
break;
55+
case 'p':
56+
g_profile = optarg;
57+
break;
58+
default:
59+
fprintf(stderr, usage, argv[0]);
60+
exit(1);
61+
break;
62+
}
63+
}
64+
65+
if (optind < argc - 1) {
66+
fprintf(stderr, "%s: invalid option -- %s\n", argv[0], argv[optind]);
67+
fprintf(stderr, usage, argv[0]);
68+
exit(1);
69+
}
70+
71+
if (optind == argc) {
72+
fprintf(stderr, "%s: Must provide a token as a requirement\n", argv[0]);
73+
fprintf(stderr, usage, argv[0]);
74+
exit(1);
75+
}
76+
77+
if ((!g_cred.empty() || !g_issuer.empty() || !g_keyid.empty()) &&
78+
(g_cred.empty() || g_issuer.empty() || g_keyid.empty()))
79+
{
80+
fprintf(stderr, "%s: If --cred, --keyid, or --issuer are set, then all must be set.\n", argv[0]);
81+
fprintf(stderr, usage, argv[0]);
82+
exit(1);
83+
}
84+
85+
return 0;
86+
}
87+
88+
}
89+
90+
int main(int argc, char* const* argv) {
691
if (argc < 2) {
7-
std::cerr << "Usage: " << argv[0] << " (TOKEN)" << std::endl;
92+
fprintf(stderr, "%s: Insufficient arguments; must at least provide a token.\n", argv[0]);
93+
fprintf(stderr, usage, argv[0]);
894
return 1;
995
}
10-
std::string token(argv[1]);
96+
if (init_arguments(argc, argv)) {return 1;}
97+
98+
std::string token(argv[argc-1]);
99+
100+
if (!g_issuer.empty()) {
101+
char *err_msg;
102+
103+
std::ifstream pub_ifs(g_cred);
104+
std::string public_contents( (std::istreambuf_iterator<char>(pub_ifs)),
105+
(std::istreambuf_iterator<char>())
106+
);
107+
108+
auto rv = scitoken_store_public_ec_key(g_issuer.c_str(), g_keyid.c_str(), public_contents.c_str(), &err_msg);
109+
if (rv) {
110+
fprintf(stderr, "%s: %s\n", argv[0], err_msg);
111+
free(err_msg);
112+
return 1;
113+
}
114+
}
11115

12116
SciToken scitoken;
13117
char *err_msg = nullptr;
@@ -16,6 +120,6 @@ int main(int argc, const char** argv) {
16120
return 1;
17121
}
18122
std::cout << "Token deserialization successful." << std::endl;
123+
19124
return 0;
20125
}
21-

0 commit comments

Comments
 (0)