11#!/usr/bin/env python
2+
3+ from __future__ import print_function
4+ from __future__ import unicode_literals
5+
26import optparse
37import jwt
48import sys
@@ -8,27 +12,29 @@ import time
812__prog__ = 'jwt'
913__version__ = '0.1'
1014
15+
1116def fix_optionparser_whitespace (input ):
1217 """Hacks around whitespace Nazi-ism in OptionParser"""
1318 newline = ' ' * 80
1419 doublespace = '\033 [8m.\033 [0m' * 2
1520 return input .replace (' ' , doublespace ).replace ('\n ' , newline )
1621
22+
1723def main ():
1824 """Encodes or decodes JSON Web Tokens based on input
19-
25+
2026Decoding examples:
21-
27+
2228 %prog --key=secret json.web.token
2329 %prog --no-verify json.web.token
24-
25- Encoding requires the key option and takes space separated key/value pairs
30+
31+ Encoding requires the key option and takes space separated key/value pairs
2632separated by equals (=) as input. Examples:
27-
33+
2834 %prog --key=secret iss=me exp=1302049071
2935 %prog --key=secret foo=bar exp=+10
30-
31- The exp key is special and can take an offset to current Unix time.
36+
37+ The exp key is special and can take an offset to current Unix time.
3238"""
3339 p = optparse .OptionParser (description = fix_optionparser_whitespace (main .__doc__ ),
3440 prog = __prog__ ,
@@ -40,7 +46,7 @@ The exp key is special and can take an offset to current Unix time.
4046 help = 'set the secret key to sign with' )
4147 p .add_option ('--alg' , dest = 'algorithm' , metavar = 'ALG' , default = 'HS256' ,
4248 help = 'set crypto algorithm to sign with. default=HS256' )
43-
49+
4450 options , arguments = p .parse_args ()
4551 if len (arguments ) > 0 or not sys .stdin .isatty ():
4652 # Try to decode
@@ -49,22 +55,23 @@ The exp key is special and can take an offset to current Unix time.
4955 token = sys .stdin .read ()
5056 else :
5157 token = arguments [0 ]
58+ token = token .encode ('utf-8' )
5259 valid_jwt = jwt .header (token )
5360 if valid_jwt :
5461 try :
55- print json .dumps (jwt .decode (token , key = options .key , verify = options .verify ))
62+ print ( json .dumps (jwt .decode (token , key = options .key , verify = options .verify ) ))
5663 sys .exit (0 )
57- except jwt .DecodeError , e :
58- print e
64+ except jwt .DecodeError as e :
65+ print ( e )
5966 sys .exit (1 )
6067 except jwt .DecodeError :
6168 pass
62-
69+
6370 # Try to encode
6471 if options .key is None :
65- print "Key is required when encoding. See --help for usage."
72+ print ( "Key is required when encoding. See --help for usage." )
6673 sys .exit (1 )
67-
74+
6875 # Build payload object to encode
6976 payload = {}
7077 for arg in arguments :
@@ -88,17 +95,17 @@ The exp key is special and can take an offset to current Unix time.
8895 v = constants [v ]
8996 payload [k ] = v
9097 except ValueError :
91- print "Invalid encoding input at %s" % arg
98+ print ( "Invalid encoding input at {}" . format ( arg ))
9299 sys .exit (1 )
93-
100+
94101 try :
95- print jwt .encode (payload , key = options .key , algorithm = options .algorithm )
102+ print ( jwt .encode (payload , key = options .key , algorithm = options .algorithm ) )
96103 sys .exit (0 )
97- except Exception , e :
98- print e
104+ except Exception as e :
105+ print ( e )
99106 sys .exit (1 )
100107 else :
101108 p .print_help ()
102109
103110if __name__ == '__main__' :
104- main ()
111+ main ()
0 commit comments