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