1-
21#include " scitokens.h"
32
43#include < fstream>
54#include < getopt.h>
65#include < iostream>
6+ #include < string>
77
88namespace {
99
1010const char usage[] =
1111 " \n "
12- " Syntax: %s [--cred cred_file] TOKEN\n "
12+ " Syntax: %s [--cred cred_file] < TOKEN\n "
1313 " \n "
1414 " Options\n "
1515 " -h | --help Display usage\n "
@@ -18,7 +18,8 @@ const char usage[] =
1818 " -K | --keyid <kid> Name of the token key.\n "
1919 " -p | --profile <profile> Profile to enforce (wlcg, scitokens1, "
2020 " scitokens2, atjwt).\n "
21- " \n " ;
21+ " \n "
22+ " The token to verify must be provided via standard input (stdin).\n " ;
2223
2324const struct option long_options[] = {{" help" , no_argument, NULL , ' h' },
2425 {" cred" , required_argument, NULL , ' c' },
@@ -65,12 +66,6 @@ int init_arguments(int argc, char *const argv[]) {
6566 exit (1 );
6667 }
6768
68- if (optind == argc) {
69- fprintf (stderr, " %s: Must provide a token as a requirement\n " , argv[0 ]);
70- fprintf (stderr, usage, argv[0 ]);
71- exit (1 );
72- }
73-
7469 if ((!g_cred.empty () || !g_issuer.empty () || !g_keyid.empty ()) &&
7570 (g_cred.empty () || g_issuer.empty () || g_keyid.empty ())) {
7671 fprintf (stderr,
@@ -87,19 +82,29 @@ int init_arguments(int argc, char *const argv[]) {
8782} // namespace
8883
8984int main (int argc, char *const *argv) {
90- if (argc < 2 ) {
85+
86+ if (init_arguments (argc, argv)) {
87+ return 1 ;
88+ }
89+
90+ std::string token;
91+ // If a positional argument is present, treat it as the token (with warning)
92+ if (optind < argc) {
9193 fprintf (stderr,
92- " %s: Insufficient arguments; must at least provide a token.\n " ,
94+ " %s: Warning: Providing the token on the command line is "
95+ " insecure. Please use stdin instead.\n " ,
9396 argv[0 ]);
94- fprintf (stderr, usage, argv[0 ]);
95- return 1 ;
97+ token = argv[optind];
98+ } else {
99+ // Read token from stdin
100+ std::getline (std::cin, token);
96101 }
97- if (init_arguments (argc, argv)) {
102+ if (token.empty ()) {
103+ fprintf (stderr, " %s: No token provided on stdin or command line.\n " , argv[0 ]);
104+ fprintf (stderr, usage, argv[0 ]);
98105 return 1 ;
99106 }
100107
101- std::string token (argv[argc - 1 ]);
102-
103108 if (!g_issuer.empty ()) {
104109 char *err_msg;
105110
0 commit comments