Skip to content

Commit 45896b9

Browse files
committed
Read token for scitokens-verify from stdin
Allow either the command line or argument, for backward capability. But give warning if the token is passed on the command line. Fixes #158
1 parent fcaa3b3 commit 45896b9

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ The easiest way to test `scitokens-cpp` is to head to the [SciTokens Demo app](h
3636
and copy the generated token. Then, from the build directory:
3737

3838
```
39-
./scitokens-verify eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImtleS1yczI1NiJ9.eyJpc3MiOiJodHRwczovL2RlbW8uc2NpdG9rZW5zLm9yZyIsImV4cCI6MTU0NjQ1NjMwOSwiaWF0IjoxNTQ2NDU1NzA5LCJuYmYiOjE1NDY0NTU3MDksImp0aSI6ImRlYmNkZDRjLTU1MzgtNDkxNS1hY2U2LTgyNTg3NGQwZjEzNyJ9.Vu9TRfDi5WJujeAGl-wP-atvNqh31-gteKqqu_IEcxoCfGYdmoIM3xOOY1GmHcmXfclkrl724ldxBBChsDpcdi_8914N9EGwGVApJLQU0SaPPdtcoCrqvVJE3bD9fs6UKooGwuk_e20ml9g0R4100fTdsD7pkIOABYGTbhxioEb1dP1o-17l2t2kUXpd8KhIyZZjmtmnMdmO5bxaY_V60OOWekwelT8ACK8ao39Ocf_wmUiS0VVX21hD1KqO0bgBU9AsVJ5prAL9ytElr_UB2X5KowPODbj6LPFNhpCwXcoG4w4Gw9VueuxCuIPhlcHBhP83i5LPgtk2YOjygdSahA
39+
echo "<your_token_here>" | ./scitokens-verify
4040
```
4141

4242
Replace the given token above with the fresh one you just generated; using the above token should give an expired
43-
token error.
44-
43+
token error. The token must be provided via standard input (stdin).
4544

4645
Instructions for Generating a Release
4746
-------------------------------------

src/verify.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "scitokens.h"
32

43
#include <fstream>
@@ -9,7 +8,7 @@ namespace {
98

109
const char usage[] =
1110
"\n"
12-
"Syntax: %s [--cred cred_file] TOKEN\n"
11+
"Syntax: %s [--cred cred_file] < TOKEN\n"
1312
"\n"
1413
" Options\n"
1514
" -h | --help Display usage\n"
@@ -18,7 +17,8 @@ const char usage[] =
1817
" -K | --keyid <kid> Name of the token key.\n"
1918
" -p | --profile <profile> Profile to enforce (wlcg, scitokens1, "
2019
"scitokens2, atjwt).\n"
21-
"\n";
20+
"\n"
21+
" The token to verify must be provided via standard input (stdin).\n";
2222

2323
const struct option long_options[] = {{"help", no_argument, NULL, 'h'},
2424
{"cred", required_argument, NULL, 'c'},
@@ -87,18 +87,24 @@ int init_arguments(int argc, char *const argv[]) {
8787
} // namespace
8888

8989
int main(int argc, char *const *argv) {
90-
if (argc < 2) {
91-
fprintf(stderr,
92-
"%s: Insufficient arguments; must at least provide a token.\n",
93-
argv[0]);
94-
fprintf(stderr, usage, argv[0]);
95-
return 1;
96-
}
90+
9791
if (init_arguments(argc, argv)) {
9892
return 1;
9993
}
10094

101-
std::string token(argv[argc - 1]);
95+
std::string token;
96+
// If a positional argument is present, treat it as the token (with warning)
97+
if (optind < argc) {
98+
fprintf(stderr, "%s: Warning: Providing the token on the command line is insecure. Please use stdin instead.\n", argv[0]);
99+
token = argv[optind];
100+
} else {
101+
// Read token from stdin
102+
std::getline(std::cin, token);
103+
}
104+
if (token.empty()) {
105+
fprintf(stderr, "%s: No token provided on stdin or command line.\n", argv[0]);
106+
return 1;
107+
}
102108

103109
if (!g_issuer.empty()) {
104110
char *err_msg;

0 commit comments

Comments
 (0)