www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
Fix after removing jwt.header()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Mar 19, 2015
1 parent 3cc2e99 commit bd57b02
Showing 1 changed file with 61 additions and 28 deletions.
89 changes: 61 additions & 28 deletions bin/jwt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from __future__ import print_function

import optparse
import jwt
import sys
import json
import time
import jwt

__prog__ = 'jwt'
__version__ = '0.1'
__version__ = '1.0.0'


def fix_optionparser_whitespace(input):
"""Hacks around whitespace hypersensitivity in OptionParser"""
"""
Hacks around whitespace hypersensitivity in OptionParser
"""
newline = ' ' * 80
doublespace = '\033[8m.\033[0m' * 2
return input.replace(' ', doublespace).replace('\n', newline)
Expand All @@ -35,50 +37,72 @@ separated by equals (=) as input. Examples:
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__,
version='%s %s' % (__prog__, __version__),
usage='%prog [options] input')
p.add_option('-n', '--no-verify', action='store_false', dest='verify', default=True,
help='ignore signature verification on decode')
p.add_option('--key', dest='key', metavar='KEY', default=None,
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')
p = optparse.OptionParser(
description=fix_optionparser_whitespace(main.__doc__),
prog=__prog__,
version='%s %s' % (__prog__, __version__),
usage='%prog [options] input'
)

p.add_option(
'-n', '--no-verify',
action='store_false',
dest='verify',
default=True,
help='ignore signature verification on decode'
)

p.add_option(
'--key',
dest='key',
metavar='KEY',
default=None,
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
try:
if not sys.stdin.isatty():
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)))
sys.exit(0)
except jwt.DecodeError as e:
print(e)
sys.exit(1)
except jwt.DecodeError:
pass
data = jwt.decode(token, key=options.key, verify=options.verify)

print(json.dumps(data))
sys.exit(0)
except jwt.DecodeError as e:
print(e)
sys.exit(1)

This comment has been minimized.

Copy link
@kubek2k

kubek2k Apr 11, 2015

this line causes encode not to work at all - or its maybe only me - I am not that proficient in python


# 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:
try:
k,v = arg.split('=', 1)
k, v = arg.split('=', 1)

# exp +offset special case?
if k == 'exp' and v[0] == '+' and len(v) > 1:
v = str(int(time.time()+int(v[1:])))

# Cast to integer?
if v.isdigit():
v = int(v)
Expand All @@ -88,17 +112,26 @@ The exp key is special and can take an offset to current Unix time.
v = float(v)
except ValueError:
pass

# Cast to true, false, or null?
constants = {'true': True, 'false': False, 'null': None}

if v in constants:
v = constants[v]

payload[k] = v
except ValueError:
print("Invalid encoding input at {}".format(arg))
print('Invalid encoding input at {}'.format(arg))
sys.exit(1)

try:
print(jwt.encode(payload, key=options.key, algorithm=options.algorithm))
token = jwt.encode(
payload,
key=options.key,
algorithm=options.algorithm
)

print(token)
sys.exit(0)
except Exception as e:
print(e)
Expand All @@ -107,4 +140,4 @@ The exp key is special and can take an offset to current Unix time.
p.print_help()

if __name__ == '__main__':
main()
main()

0 comments on commit bd57b02

Please sign in to comment.