|
| 1 | +# coding: utf-8 |
| 2 | +# Modified Work: Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# Original Work: Copyright (c) 2015 José Padilla |
| 4 | + |
| 5 | +#!/usr/bin/env python |
| 6 | + |
| 7 | +from __future__ import absolute_import, print_function |
| 8 | + |
| 9 | +import argparse |
| 10 | +import json |
| 11 | +import sys |
| 12 | +import time |
| 13 | + |
| 14 | +from . import DecodeError, __version__, decode, encode |
| 15 | + |
| 16 | + |
| 17 | +def encode_payload(args): |
| 18 | + # Try to encode |
| 19 | + if args.key is None: |
| 20 | + raise ValueError('Key is required when encoding. See --help for usage.') |
| 21 | + |
| 22 | + # Build payload object to encode |
| 23 | + payload = {} |
| 24 | + |
| 25 | + for arg in args.payload: |
| 26 | + k, v = arg.split('=', 1) |
| 27 | + |
| 28 | + # exp +offset special case? |
| 29 | + if k == 'exp' and v[0] == '+' and len(v) > 1: |
| 30 | + v = str(int(time.time()+int(v[1:]))) |
| 31 | + |
| 32 | + # Cast to integer? |
| 33 | + if v.isdigit(): |
| 34 | + v = int(v) |
| 35 | + else: |
| 36 | + # Cast to float? |
| 37 | + try: |
| 38 | + v = float(v) |
| 39 | + except ValueError: |
| 40 | + pass |
| 41 | + |
| 42 | + # Cast to true, false, or null? |
| 43 | + constants = {'true': True, 'false': False, 'null': None} |
| 44 | + |
| 45 | + if v in constants: |
| 46 | + v = constants[v] |
| 47 | + |
| 48 | + payload[k] = v |
| 49 | + |
| 50 | + token = encode( |
| 51 | + payload, |
| 52 | + key=args.key, |
| 53 | + algorithm=args.algorithm |
| 54 | + ) |
| 55 | + |
| 56 | + return token.decode('utf-8') |
| 57 | + |
| 58 | + |
| 59 | +def decode_payload(args): |
| 60 | + try: |
| 61 | + if sys.stdin.isatty(): |
| 62 | + token = sys.stdin.read() |
| 63 | + else: |
| 64 | + token = args.token |
| 65 | + |
| 66 | + token = token.encode('utf-8') |
| 67 | + data = decode(token, key=args.key, verify=args.verify) |
| 68 | + |
| 69 | + return json.dumps(data) |
| 70 | + |
| 71 | + except DecodeError as e: |
| 72 | + raise DecodeError('There was an error decoding the token: %s' % e) |
| 73 | + |
| 74 | + |
| 75 | +def build_argparser(): |
| 76 | + |
| 77 | + usage = ''' |
| 78 | + Encodes or decodes JSON Web Tokens based on input. |
| 79 | +
|
| 80 | + %(prog)s [options] <command> [options] input |
| 81 | +
|
| 82 | + Decoding examples: |
| 83 | +
|
| 84 | + %(prog)s --key=secret decode json.web.token |
| 85 | + %(prog)s decode --no-verify json.web.token |
| 86 | +
|
| 87 | + Encoding requires the key option and takes space separated key/value pairs |
| 88 | + separated by equals (=) as input. Examples: |
| 89 | +
|
| 90 | + %(prog)s --key=secret encode iss=me exp=1302049071 |
| 91 | + %(prog)s --key=secret encode foo=bar exp=+10 |
| 92 | +
|
| 93 | + The exp key is special and can take an offset to current Unix time. |
| 94 | + ''' |
| 95 | + |
| 96 | + arg_parser = argparse.ArgumentParser( |
| 97 | + prog='pyjwt', |
| 98 | + usage=usage |
| 99 | + ) |
| 100 | + |
| 101 | + arg_parser.add_argument( |
| 102 | + '-v', '--version', |
| 103 | + action='version', |
| 104 | + version='%(prog)s ' + __version__ |
| 105 | + ) |
| 106 | + |
| 107 | + arg_parser.add_argument( |
| 108 | + '--key', |
| 109 | + dest='key', |
| 110 | + metavar='KEY', |
| 111 | + default=None, |
| 112 | + help='set the secret key to sign with' |
| 113 | + ) |
| 114 | + |
| 115 | + arg_parser.add_argument( |
| 116 | + '--alg', |
| 117 | + dest='algorithm', |
| 118 | + metavar='ALG', |
| 119 | + default='HS256', |
| 120 | + help='set crypto algorithm to sign with. default=HS256' |
| 121 | + ) |
| 122 | + |
| 123 | + subparsers = arg_parser.add_subparsers( |
| 124 | + title='PyJWT subcommands', |
| 125 | + description='valid subcommands', |
| 126 | + help='additional help' |
| 127 | + ) |
| 128 | + |
| 129 | + # Encode subcommand |
| 130 | + encode_parser = subparsers.add_parser('encode', help='use to encode a supplied payload') |
| 131 | + |
| 132 | + payload_help = """Payload to encode. Must be a space separated list of key/value |
| 133 | + pairs separated by equals (=) sign.""" |
| 134 | + |
| 135 | + encode_parser.add_argument('payload', nargs='+', help=payload_help) |
| 136 | + encode_parser.set_defaults(func=encode_payload) |
| 137 | + |
| 138 | + # Decode subcommand |
| 139 | + decode_parser = subparsers.add_parser('decode', help='use to decode a supplied JSON web token') |
| 140 | + decode_parser.add_argument('token', help='JSON web token to decode.') |
| 141 | + |
| 142 | + decode_parser.add_argument( |
| 143 | + '-n', '--no-verify', |
| 144 | + action='store_false', |
| 145 | + dest='verify', |
| 146 | + default=True, |
| 147 | + help='ignore signature and claims verification on decode' |
| 148 | + ) |
| 149 | + |
| 150 | + decode_parser.set_defaults(func=decode_payload) |
| 151 | + |
| 152 | + return arg_parser |
| 153 | + |
| 154 | + |
| 155 | +def main(): |
| 156 | + arg_parser = build_argparser() |
| 157 | + |
| 158 | + try: |
| 159 | + arguments = arg_parser.parse_args(sys.argv[1:]) |
| 160 | + |
| 161 | + output = arguments.func(arguments) |
| 162 | + |
| 163 | + print(output) |
| 164 | + except Exception as e: |
| 165 | + print('There was an unforseen error: ', e) |
| 166 | + arg_parser.print_help() |
0 commit comments