11
22#include " scitokens.h"
33
4- #include < stdlib.h>
54#include < getopt.h>
5+ #include < stdlib.h>
66
77#include < cstdio>
8- #include < string>
9- #include < vector>
108#include < fstream>
119#include < memory>
10+ #include < string>
11+ #include < vector>
1212
1313namespace {
1414
15- const char usage[] = \
16- " \n "
17- " Syntax: %s [--cred cred_file] [--key key_file] [--keyid kid]\n "
18- " [--claim key=val] ...\n "
19- " \n "
20- " Options\n "
21- " -h | --help Display usage\n "
22- " -c | --cred <cred_file> File containing signing credential. \n "
23- " -k | --key <key_file> File containing the signing private key .\n "
24- " -K | --keyid <kid > Name of the token key. \n "
25- " -i | --issuer <issuer> Issuer for the token .\n "
26- " -p | --profile <profile > Token profile (wlcg, scitokens1, scitokens2, atjwt) .\n "
27- " \n " ;
28-
29- const struct option long_options[] =
30- {
31- { " help " , no_argument, NULL , ' h ' },
32- { " cred " , required_argument , NULL , ' c ' },
33- { " key " , required_argument, NULL , ' k ' },
34- { " keyid " , required_argument, NULL , ' K ' },
35- { " issuer " , required_argument, NULL , ' i ' },
36- { " claim " , required_argument, NULL , ' C ' },
37- { " profile " , required_argument, NULL , ' p ' },
38- { 0 , 0 , 0 , 0 }
39- };
15+ const char usage[] =
16+ " \n "
17+ " Syntax: %s [--cred cred_file] [--key key_file] [--keyid kid]\n "
18+ " [--claim key=val] ...\n "
19+ " \n "
20+ " Options\n "
21+ " -h | --help Display usage\n "
22+ " -c | --cred <cred_file> File containing signing "
23+ " credential .\n "
24+ " -k | --key <key_file > File containing the signing "
25+ " private key .\n "
26+ " -K | --keyid <kid > Name of the token key .\n "
27+ " -i | --issuer <issuer> Issuer for the token. \n "
28+ " -p | --profile <profile> Token profile (wlcg, scitokens1, "
29+ " scitokens2, atjwt). \n "
30+ " \n " ;
31+
32+ const struct option long_options[] = {{ " help " , no_argument , NULL , ' h ' },
33+ { " cred " , required_argument, NULL , ' c ' },
34+ { " key " , required_argument, NULL , ' k ' },
35+ { " keyid " , required_argument, NULL , ' K ' },
36+ { " issuer " , required_argument, NULL , ' i ' },
37+ { " claim " , required_argument, NULL , ' C ' },
38+ { " profile " , required_argument, NULL , ' p ' },
39+ { 0 , 0 , 0 , 0 } };
4040
4141const char short_options[] = " hc:k:K:i:C:p:" ;
4242
@@ -46,10 +46,9 @@ std::vector<std::string> g_claims;
4646int init_arguments (int argc, char *argv[]) {
4747
4848 int arg;
49- while ((arg = getopt_long (argc, argv, short_options, long_options, nullptr )) != -1 )
50- {
51- switch (arg)
52- {
49+ while ((arg = getopt_long (argc, argv, short_options, long_options,
50+ nullptr )) != -1 ) {
51+ switch (arg) {
5352 case ' h' :
5453 printf (usage, argv[0 ]);
5554 exit (0 );
@@ -112,7 +111,7 @@ int init_arguments(int argc, char *argv[]) {
112111 return 0 ;
113112}
114113
115- }
114+ } // namespace
116115
117116int main (int argc, char *argv[]) {
118117
@@ -122,33 +121,33 @@ int main(int argc, char *argv[]) {
122121 }
123122
124123 std::ifstream priv_ifs (g_key);
125- std::string private_contents ( (std::istreambuf_iterator<char >(priv_ifs)),
126- (std::istreambuf_iterator<char >())
127- );
124+ std::string private_contents ((std::istreambuf_iterator<char >(priv_ifs)),
125+ (std::istreambuf_iterator<char >()));
128126 std::ifstream pub_ifs (g_cred);
129- std::string public_contents ( (std::istreambuf_iterator<char >(pub_ifs)),
130- (std::istreambuf_iterator<char >())
131- );
127+ std::string public_contents ((std::istreambuf_iterator<char >(pub_ifs)),
128+ (std::istreambuf_iterator<char >()));
132129
133130 char *err_msg;
134- auto key_raw = scitoken_key_create (g_kid.c_str (), " ES256" , public_contents.c_str (),
135- private_contents.c_str (), &err_msg);
136- std::unique_ptr<void , decltype (&scitoken_key_destroy)>
137- key (key_raw, scitoken_key_destroy);
131+ auto key_raw =
132+ scitoken_key_create (g_kid.c_str (), " ES256" , public_contents.c_str (),
133+ private_contents.c_str (), &err_msg);
134+ std::unique_ptr<void , decltype (&scitoken_key_destroy)> key (
135+ key_raw, scitoken_key_destroy);
138136 if (key_raw == nullptr ) {
139137 fprintf (stderr, " Failed to generate a key: %s\n " , err_msg);
140138 free (err_msg);
141139 return 1 ;
142140 }
143141
144- std::unique_ptr<void , decltype (&scitoken_destroy)>
145- token ( scitoken_create (key_raw), scitoken_destroy);
142+ std::unique_ptr<void , decltype (&scitoken_destroy)> token (
143+ scitoken_create (key_raw), scitoken_destroy);
146144 if (token.get () == nullptr ) {
147145 fprintf (stderr, " Failed to generate a new token.\n " );
148146 return 1 ;
149147 }
150148
151- rv = scitoken_set_claim_string (token.get (), " iss" , g_issuer.c_str (), &err_msg);
149+ rv = scitoken_set_claim_string (token.get (), " iss" , g_issuer.c_str (),
150+ &err_msg);
152151 if (rv) {
153152 fprintf (stderr, " Failed to set issuer: %s\n " , err_msg);
154153 free (err_msg);
@@ -158,15 +157,18 @@ int main(int argc, char *argv[]) {
158157 for (const auto &claim : g_claims) {
159158 auto pos = claim.find (" =" );
160159 if (pos == std::string::npos) {
161- fprintf (stderr, " Claim must contain a '=' character: %s\n " , claim.c_str ());
160+ fprintf (stderr, " Claim must contain a '=' character: %s\n " ,
161+ claim.c_str ());
162162 return 1 ;
163163 }
164164 auto key = claim.substr (0 , pos);
165165 auto val = claim.substr (pos + 1 );
166166
167- rv = scitoken_set_claim_string (token.get (), key.c_str (), val.c_str (), &err_msg);
167+ rv = scitoken_set_claim_string (token.get (), key.c_str (), val.c_str (),
168+ &err_msg);
168169 if (rv) {
169- fprintf (stderr, " Failed to set claim (%s=%s): %s\n " , key.c_str (), val.c_str (), err_msg);
170+ fprintf (stderr, " Failed to set claim (%s=%s): %s\n " , key.c_str (),
171+ val.c_str (), err_msg);
170172 free (err_msg);
171173 return 1 ;
172174 }
0 commit comments