Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tox, Travis, and Appveyor improvements #157

Merged
merged 7 commits into from
May 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ omit =
.tox/*
setup.py
*.egg/*
*/__main__.py
17 changes: 9 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
language: python
sudo: false
env:
- TOXENV=pep8
- TOXENV=py26
- TOXENV=py27
- TOXENV=py33
- TOXENV=py34
- TOXENV=flake8
- TOXENV=py26-crypto
- TOXENV=py27-crypto
- TOXENV=py33-crypto
- TOXENV=py34-crypto
- TOXENV=py34-nocrypto
- TOXENV=py27-nocrypto
- TOXENV=py34-contrib-crypto
- TOXENV=py27-contrib-crypto
- TOXENV=py34-contrib_crypto
- TOXENV=py27-contrib_crypto
install:
- pip install tox coveralls
- pip install -U pip
- pip install -U tox coveralls
script:
- tox
after_success:
Expand Down
35 changes: 35 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
environment:
matrix:
- PYTHON: "C:\\Python27-x64"
TOX_ENV: "py27-crypto"

- PYTHON: "C:\\Python34-x64"
TOX_ENV: "py34-crypto"

init:
- SET PATH=%PYTHON%;%PATH%
- python -c "import sys;sys.stdout.write(sys.version)"
- ECHO .
- python -m pip list

# pip & virtualenv are pre-installed
install:
- python -m pip install -U setuptools
- python -m pip install -U pip
- python -m pip install -U wheel
- python -m pip install -U tox

build: false # Not a C# project, build stuff at the test step instead.

test_script:
- python -m tox -e %TOX_ENV%

after_test:
- python setup.py bdist_wheel
- ps: "ls dist"

artifacts:
- path: dist\*

#on_success:
# - TODO: upload the content of dist/*.whl to a public wheelhouse
279 changes: 135 additions & 144 deletions bin/jwt → jwt/__main__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,144 +1,135 @@
#!/usr/bin/env python

from __future__ import print_function

import optparse
import sys
import json
import time
import jwt

__prog__ = 'jwt'
__version__ = '1.0.0'


def fix_optionparser_whitespace(input):
"""
Hacks around whitespace hypersensitivity 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
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.
"""
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():
if len(arguments) == 1 and ( options.verify is False or options.key is not None ):
# Try to decode
try:
if not sys.stdin.isatty():
token = sys.stdin.read()
else:
token = arguments[0]

token = token.encode('utf-8')
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)

# Try to encode
if options.key is None:
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)

# 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)
else:
# Cast to float?
try:
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))
sys.exit(1)

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

print(token)
sys.exit(0)
except Exception as e:
print(e)
sys.exit(1)
else:
p.print_help()

if __name__ == '__main__':
main()
#!/usr/bin/env python

from __future__ import absolute_import, print_function

import json
import optparse
import sys
import time

from . import DecodeError, __package__, __version__, decode, encode


def main():

usage = '''Encodes or decodes JSON Web Tokens based on input.

%prog [options] 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
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.\
'''
p = optparse.OptionParser(
usage=usage,
prog=__package__,
version='%s %s' % (__package__, __version__),
)

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():
if len(arguments) == 1 and (not options.verify or options.key):
# Try to decode
try:
if not sys.stdin.isatty():
token = sys.stdin.read()
else:
token = arguments[0]

token = token.encode('utf-8')
data = decode(token, key=options.key, verify=options.verify)

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

# Try to encode
if options.key is None:
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)

# 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)
else:
# Cast to float?
try:
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))
sys.exit(1)

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

print(token)
sys.exit(0)
except Exception as e:
print(e)
sys.exit(1)
else:
p.print_help()

if __name__ == '__main__':
main()
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ max-line-length = 119
universal = 1

[pytest]
addopts = --cov-report term-missing --cov-config=.coveragerc --cov
addopts = --cov-report term-missing --cov-config=.coveragerc --cov .

[aliases]
test = pytest
Loading