summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Kavanagh <alex@ajkavanagh.co.uk>2014-03-21 15:35:19 +0000
committerAlex Kavanagh <alex@ajkavanagh.co.uk>2014-03-21 15:35:19 +0000
commit3fb19005e6cf85c7b4cc8cdcd892c42efb8e32ae (patch)
tree94eb715b203a501162bd0ee8e9c75bfca0eff7c0
parent0894d64c648266c289cb9123a1787be388f86a85 (diff)
downloadpyjwt-3fb19005e6cf85c7b4cc8cdcd892c42efb8e32ae.tar.gz
Update bin/jwt file to be Python 3 compatible.
Only tested on Python 2.7.5 and Python 3.3.3 to generate and verify tokens.
-rwxr-xr-xbin/jwt47
1 files changed, 27 insertions, 20 deletions
diff --git a/bin/jwt b/bin/jwt
index 8139ab0..07f2dcb 100755
--- a/bin/jwt
+++ b/bin/jwt
@@ -1,4 +1,8 @@
#!/usr/bin/env python
+
+from __future__ import print_function
+from __future__ import unicode_literals
+
import optparse
import jwt
import sys
@@ -8,27 +12,29 @@ import time
__prog__ = 'jwt'
__version__ = '0.1'
+
def fix_optionparser_whitespace(input):
"""Hacks around whitespace Nazi-ism in OptionParser"""
newline = ' ' * 80
doublespace = '\033[8m.\033[0m' * 2
return input.replace(' ', doublespace).replace('\n', newline)
+
def main():
"""Encodes or decodes JSON Web Tokens based on input
-
+
Decoding examples:
-
+
%prog --key=secret json.web.token
%prog --no-verify json.web.token
-
-Encoding requires the key option and takes space separated key/value pairs
+
+Encoding requires the key option and takes space separated key/value pairs
separated by equals (=) as input. Examples:
-
+
%prog --key=secret iss=me exp=1302049071
%prog --key=secret foo=bar exp=+10
-
-The exp key is special and can take an offset to current Unix time.
+
+The exp key is special and can take an offset to current Unix time.
"""
p = optparse.OptionParser(description=fix_optionparser_whitespace(main.__doc__),
prog=__prog__,
@@ -40,7 +46,7 @@ The exp key is special and can take an offset to current Unix time.
help='set the secret key to sign with')
p.add_option('--alg', dest='algorithm', metavar='ALG', default='HS256',
help='set crypto algorithm to sign with. default=HS256')
-
+
options, arguments = p.parse_args()
if len(arguments) > 0 or not sys.stdin.isatty():
# Try to decode
@@ -49,22 +55,23 @@ The exp key is special and can take an offset to current Unix time.
token = sys.stdin.read()
else:
token = arguments[0]
+ token = token.encode('utf-8')
valid_jwt = jwt.header(token)
if valid_jwt:
try:
- print json.dumps(jwt.decode(token, key=options.key, verify=options.verify))
+ print(json.dumps(jwt.decode(token, key=options.key, verify=options.verify)))
sys.exit(0)
- except jwt.DecodeError, e:
- print e
+ except jwt.DecodeError as e:
+ print(e)
sys.exit(1)
except jwt.DecodeError:
pass
-
+
# Try to encode
if options.key is None:
- print "Key is required when encoding. See --help for usage."
+ print("Key is required when encoding. See --help for usage.")
sys.exit(1)
-
+
# Build payload object to encode
payload = {}
for arg in arguments:
@@ -88,17 +95,17 @@ The exp key is special and can take an offset to current Unix time.
v = constants[v]
payload[k] = v
except ValueError:
- print "Invalid encoding input at %s" % arg
+ print("Invalid encoding input at {}".format(arg))
sys.exit(1)
-
+
try:
- print jwt.encode(payload, key=options.key, algorithm=options.algorithm)
+ print(jwt.encode(payload, key=options.key, algorithm=options.algorithm))
sys.exit(0)
- except Exception, e:
- print e
+ except Exception as e:
+ print(e)
sys.exit(1)
else:
p.print_help()
if __name__ == '__main__':
- main() \ No newline at end of file
+ main()