Skip to content

Commit bd57b02

Browse files
committed
Fix after removing jwt.header()
1 parent 3cc2e99 commit bd57b02

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

bin/jwt

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
from __future__ import print_function
44

55
import optparse
6-
import jwt
76
import sys
87
import json
98
import time
9+
import jwt
1010

1111
__prog__ = 'jwt'
12-
__version__ = '0.1'
12+
__version__ = '1.0.0'
1313

1414

1515
def fix_optionparser_whitespace(input):
16-
"""Hacks around whitespace hypersensitivity in OptionParser"""
16+
"""
17+
Hacks around whitespace hypersensitivity in OptionParser
18+
"""
1719
newline = ' ' * 80
1820
doublespace = '\033[8m.\033[0m' * 2
1921
return input.replace(' ', doublespace).replace('\n', newline)
@@ -35,50 +37,72 @@ separated by equals (=) as input. Examples:
3537
3638
The exp key is special and can take an offset to current Unix time.
3739
"""
38-
p = optparse.OptionParser(description=fix_optionparser_whitespace(main.__doc__),
39-
prog=__prog__,
40-
version='%s %s' % (__prog__, __version__),
41-
usage='%prog [options] input')
42-
p.add_option('-n', '--no-verify', action='store_false', dest='verify', default=True,
43-
help='ignore signature verification on decode')
44-
p.add_option('--key', dest='key', metavar='KEY', default=None,
45-
help='set the secret key to sign with')
46-
p.add_option('--alg', dest='algorithm', metavar='ALG', default='HS256',
47-
help='set crypto algorithm to sign with. default=HS256')
40+
p = optparse.OptionParser(
41+
description=fix_optionparser_whitespace(main.__doc__),
42+
prog=__prog__,
43+
version='%s %s' % (__prog__, __version__),
44+
usage='%prog [options] input'
45+
)
46+
47+
p.add_option(
48+
'-n', '--no-verify',
49+
action='store_false',
50+
dest='verify',
51+
default=True,
52+
help='ignore signature verification on decode'
53+
)
54+
55+
p.add_option(
56+
'--key',
57+
dest='key',
58+
metavar='KEY',
59+
default=None,
60+
help='set the secret key to sign with'
61+
)
62+
63+
p.add_option(
64+
'--alg',
65+
dest='algorithm',
66+
metavar='ALG',
67+
default='HS256',
68+
help='set crypto algorithm to sign with. default=HS256'
69+
)
4870

4971
options, arguments = p.parse_args()
72+
5073
if len(arguments) > 0 or not sys.stdin.isatty():
5174
# Try to decode
5275
try:
5376
if not sys.stdin.isatty():
5477
token = sys.stdin.read()
5578
else:
5679
token = arguments[0]
80+
5781
token = token.encode('utf-8')
58-
valid_jwt = jwt.header(token)
59-
if valid_jwt:
60-
try:
61-
print(json.dumps(jwt.decode(token, key=options.key, verify=options.verify)))
62-
sys.exit(0)
63-
except jwt.DecodeError as e:
64-
print(e)
65-
sys.exit(1)
66-
except jwt.DecodeError:
67-
pass
82+
data = jwt.decode(token, key=options.key, verify=options.verify)
83+
84+
print(json.dumps(data))
85+
sys.exit(0)
86+
except jwt.DecodeError as e:
87+
print(e)
88+
sys.exit(1)
6889

6990
# Try to encode
7091
if options.key is None:
71-
print("Key is required when encoding. See --help for usage.")
92+
print('Key is required when encoding. See --help for usage.')
7293
sys.exit(1)
7394

7495
# Build payload object to encode
7596
payload = {}
97+
7698
for arg in arguments:
7799
try:
78-
k,v = arg.split('=', 1)
100+
k, v = arg.split('=', 1)
101+
79102
# exp +offset special case?
80103
if k == 'exp' and v[0] == '+' and len(v) > 1:
81104
v = str(int(time.time()+int(v[1:])))
105+
82106
# Cast to integer?
83107
if v.isdigit():
84108
v = int(v)
@@ -88,17 +112,26 @@ The exp key is special and can take an offset to current Unix time.
88112
v = float(v)
89113
except ValueError:
90114
pass
115+
91116
# Cast to true, false, or null?
92117
constants = {'true': True, 'false': False, 'null': None}
118+
93119
if v in constants:
94120
v = constants[v]
121+
95122
payload[k] = v
96123
except ValueError:
97-
print("Invalid encoding input at {}".format(arg))
124+
print('Invalid encoding input at {}'.format(arg))
98125
sys.exit(1)
99126

100127
try:
101-
print(jwt.encode(payload, key=options.key, algorithm=options.algorithm))
128+
token = jwt.encode(
129+
payload,
130+
key=options.key,
131+
algorithm=options.algorithm
132+
)
133+
134+
print(token)
102135
sys.exit(0)
103136
except Exception as e:
104137
print(e)
@@ -107,4 +140,4 @@ The exp key is special and can take an offset to current Unix time.
107140
p.print_help()
108141

109142
if __name__ == '__main__':
110-
main()
143+
main()

0 commit comments

Comments
 (0)