diff options
author | José Padilla <jpadilla@webapplicate.com> | 2014-03-21 13:55:52 -0400 |
---|---|---|
committer | José Padilla <jpadilla@webapplicate.com> | 2014-03-21 13:55:52 -0400 |
commit | f01d965e3cd35bb44727ac282153d330b6e770e5 (patch) | |
tree | 94eb715b203a501162bd0ee8e9c75bfca0eff7c0 | |
parent | 0894d64c648266c289cb9123a1787be388f86a85 (diff) | |
parent | 3fb19005e6cf85c7b4cc8cdcd892c42efb8e32ae (diff) | |
download | pyjwt-f01d965e3cd35bb44727ac282153d330b6e770e5.tar.gz |
Merge pull request #30 from ajkavanagh/master
Update bin/jwt file to be Python 3 compatible.
-rwxr-xr-x | bin/jwt | 47 |
1 files changed, 27 insertions, 20 deletions
@@ -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() |