Skip to content

Commit 89257bb

Browse files
authored
Merge pull request #159 from djw8605/verify-stdin
Read token for scitokens-verify from stdin
2 parents eb58753 + 4a62c0e commit 89257bb

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
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: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
21
#include "scitokens.h"
32

43
#include <fstream>
54
#include <getopt.h>
65
#include <iostream>
6+
#include <string>
77

88
namespace {
99

1010
const 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

2324
const 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

8984
int 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

Comments
 (0)