diff options
author | José Padilla <jpadilla@webapplicate.com> | 2017-11-05 19:05:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-05 19:05:14 -0500 |
commit | c1253ec82f88bc810884735d68c60f1f6cbd4c1e (patch) | |
tree | 71b12e7297d1782cb46c44a0bff38023480935ff | |
parent | e1e4d02c5d9499bcd59403fff2ad73d67cdc8af4 (diff) | |
download | pyjwt-c1253ec82f88bc810884735d68c60f1f6cbd4c1e.tar.gz |
Fix over-eager fallback to stdin (#304)
* Fix over-eager fallback to stdin
* Ignore .eggs dir
* Add test to cover when terminal is not a TTY
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | jwt/__main__.py | 14 | ||||
-rw-r--r-- | tests/test_cli.py | 31 |
3 files changed, 42 insertions, 4 deletions
@@ -16,6 +16,7 @@ develop-eggs/ dist/ downloads/ eggs/ +.eggs/ lib/ lib64/ parts/ diff --git a/jwt/__main__.py b/jwt/__main__.py index 52e7abf..396dc0d 100644 --- a/jwt/__main__.py +++ b/jwt/__main__.py @@ -54,10 +54,13 @@ def encode_payload(args): def decode_payload(args):
try:
- if sys.stdin.isatty():
- token = sys.stdin.read()
- else:
+ if args.token:
token = args.token
+ else:
+ if sys.stdin.isatty():
+ token = sys.stdin.readline().strip()
+ else:
+ raise IOError('Cannot read from stdin: terminal not a TTY')
token = token.encode('utf-8')
data = decode(token, key=args.key, verify=args.verify)
@@ -133,7 +136,10 @@ def build_argparser(): # Decode subcommand
decode_parser = subparsers.add_parser('decode', help='use to decode a supplied JSON web token')
- decode_parser.add_argument('token', help='JSON web token to decode.')
+ decode_parser.add_argument(
+ 'token',
+ help='JSON web token to decode.',
+ nargs='?')
decode_parser.add_argument(
'-n', '--no-verify',
diff --git a/tests/test_cli.py b/tests/test_cli.py index f08cf6b..715c2f1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -57,6 +57,37 @@ class TestCli: assert 'There was an error decoding the token' in str(excinfo.value) + def test_decode_payload_terminal_tty(self, monkeypatch): + encode_args = [ + '--key=secret-key', + 'encode', + 'name=hello-world', + ] + parser = build_argparser() + parsed_encode_args = parser.parse_args(encode_args) + token = encode_payload(parsed_encode_args) + + decode_args = ['--key=secret-key', 'decode'] + parsed_decode_args = parser.parse_args(decode_args) + + monkeypatch.setattr(sys.stdin, 'isatty', lambda: True) + monkeypatch.setattr(sys.stdin, 'readline', lambda: token) + + actual = json.loads(decode_payload(parsed_decode_args)) + assert actual['name'] == 'hello-world' + + def test_decode_payload_raises_terminal_not_a_tty(self, monkeypatch): + decode_args = ['--key', '1234', 'decode'] + parser = build_argparser() + args = parser.parse_args(decode_args) + + monkeypatch.setattr(sys.stdin, 'isatty', lambda: False) + + with pytest.raises(IOError) as excinfo: + decode_payload(args) + assert 'Cannot read from stdin: terminal not a TTY' \ + in str(excinfo.value) + @pytest.mark.parametrize('key,name,job,exp,verify', [ ('1234', 'Vader', 'Sith', None, None), ('4567', 'Anakin', 'Jedi', '+1', None), |