|
8 | 8 | # "License"); you may not use this file except in compliance |
9 | 9 | # with the License. You may obtain a copy of the License at |
10 | 10 | # |
11 | | -# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
12 | 12 | # |
13 | 13 | # Unless required by applicable law or agreed to in writing, software |
14 | 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
|
25 | 25 | from jose import jwt |
26 | 26 |
|
27 | 27 | def main(): |
28 | | - parser = argparse.ArgumentParser() |
29 | | - parser.add_argument('-c', '--config', |
30 | | - help="Configuration File", |
31 | | - required=True) |
32 | | - parser.add_argument('-u', '--uri', |
33 | | - help="URI to sign", |
34 | | - required=True) |
35 | | - args = parser.parse_args() |
| 28 | + parser = argparse.ArgumentParser() |
| 29 | + parser.add_argument('-c', '--config', |
| 30 | + help="Configuration File", |
| 31 | + required=True) |
| 32 | + parser.add_argument('-u', '--uri', |
| 33 | + help="URI to sign", |
| 34 | + required=True) |
36 | 35 |
|
37 | | - with open(args.config, 'r') as f: |
38 | | - config = json.load(f) |
| 36 | + # helpers |
| 37 | + parser.add_argument('--key_index', type=int, nargs=1) |
| 38 | + parser.add_argument('--token_lifetime', type=int, nargs=1) |
39 | 39 |
|
40 | | - keys = config["keys"] |
| 40 | + # override arguments -- claims |
| 41 | + parser.add_argument('--aud', nargs=1) |
| 42 | + parser.add_argument('--cdniets', type=int, nargs=1) |
| 43 | + parser.add_argument('--cdnistd', type=int, nargs=1) |
| 44 | + parser.add_argument('--cdnistt', type=int, nargs=1) |
| 45 | + parser.add_argument('--exp', type=int, nargs=1) |
| 46 | + parser.add_argument('--iss', nargs=1) |
41 | 47 |
|
42 | | - # Randomly select a key |
| 48 | + # override arguments -- key |
| 49 | + parser.add_argument('--alg', nargs=1) |
| 50 | + parser.add_argument('--k', nargs=1) |
| 51 | + parser.add_argument('--kid', nargs=1) |
| 52 | + parser.add_argument('--kty', nargs=1) |
| 53 | + |
| 54 | + args = parser.parse_args() |
| 55 | + |
| 56 | + with open(args.config, 'r') as f: |
| 57 | + config = json.load(f) |
| 58 | + |
| 59 | + keys = config["keys"] |
| 60 | + |
| 61 | + # Select a key, either explicitly or randomly |
| 62 | + key_index = 0 |
| 63 | + if args.key_index: |
| 64 | + key_index = args.key_index[0] |
| 65 | + print("args key_index " + str(key_index)) |
| 66 | + else: |
43 | 67 | key_index = random.randint(0,len(keys)-1) |
44 | | - print("Using Key: " + str(keys[key_index]["kid"]) + " to sign URI.") |
45 | | - key = keys[key_index] |
| 68 | + print("randomizing key index") |
| 69 | + |
| 70 | + print("Using key_index " + str(key_index)) |
| 71 | + |
| 72 | + print("Using Key: " + str(keys[key_index]["kid"]) + " to sign URI.") |
| 73 | + key = keys[key_index] |
| 74 | + |
| 75 | + # Build Out claimset |
| 76 | + claimset = {} |
| 77 | + if "iss" in config.keys(): |
| 78 | + claimset["iss"] = config["iss"] |
| 79 | + |
| 80 | + if "token_lifetime" in config.keys(): |
| 81 | + claimset["exp"] = int(time.time()) + config["token_lifetime"] |
| 82 | + else: |
| 83 | + claimset["exp"] = int(time.time()) + 30 |
| 84 | + |
| 85 | + if "aud" in config.keys(): |
| 86 | + claimset["aud"] = config["aud"] |
| 87 | + |
| 88 | + if "cdnistt" in config.keys(): |
| 89 | + if config["cdnistt"]: |
| 90 | + claimset["cdnistt"] = 1 |
| 91 | + if "cdniets" in config.keys(): |
| 92 | + claimset["cdniets"] = config["cdniets"] |
| 93 | + else: |
| 94 | + claimset["cdniets"] = 30 |
| 95 | + |
| 96 | + |
| 97 | + # process override args - simple |
| 98 | + if args.iss: |
| 99 | + claimset["iss"] = args.iss[0] |
| 100 | + if args.exp: |
| 101 | + claimset["exp"] = args.exp[0] |
| 102 | + if args.aud: |
| 103 | + claimset["aud"] = args.aud[0] |
46 | 104 |
|
47 | | - # Build Out claimset |
48 | | - claimset = {} |
49 | | - if ("iss" in config.keys()): |
50 | | - claimset["iss"] = config["iss"] |
| 105 | + # process override args - complex |
| 106 | + if args.cdnistt: |
| 107 | + claimset["cdnistt"] = args.cdnistt[0] |
51 | 108 |
|
52 | | - if ("token_lifetime" in config.keys()): |
53 | | - claimset["exp"] = int(time.time()) + config["token_lifetime"] |
54 | | - else: |
55 | | - claimset["exp"] = int(time.time()) + 30 |
| 109 | + if "cdnistt" in config.keys(): |
| 110 | + if args.cdniets: |
| 111 | + claimset["cdniets"] = arg.cdniets[0] |
56 | 112 |
|
57 | | - if("aud" in config.keys()): |
58 | | - claimset["aud"] = config["aud"] |
| 113 | + # specific key overrides |
| 114 | + if args.alg: |
| 115 | + key["alg"] = args.alg[0] |
| 116 | + if args.kid: |
| 117 | + key["kid"] = args.kid[0] |
| 118 | + if args.kty: |
| 119 | + key["kty"] = args.kty[0] |
| 120 | + if args.k: |
| 121 | + key["k"] = args.k[0] |
59 | 122 |
|
60 | | - if("cdnistt" in config.keys()): |
61 | | - if config["cdnistt"]: |
62 | | - claimset["cdnistt"] = 1 |
63 | | - if("cdniets" in config.keys()): |
64 | | - claimset["cdniets"] = config["cdniets"] |
65 | | - else: |
66 | | - claimset["cdniets"] = 30 |
| 123 | + print(claimset) |
| 124 | + print(key) |
67 | 125 |
|
68 | | - Token = jwt.encode(claimset,key,algorithm=key["alg"]) |
| 126 | + Token = jwt.encode(claimset,key,algorithm=key["alg"]) |
69 | 127 |
|
70 | | - print("Signed URL: " + args.uri + "?urisigning=" + Token) |
| 128 | + print("Signed URL: " + args.uri + "?URISigningPackage=" + Token) |
71 | 129 |
|
72 | 130 | if __name__ == "__main__": |
73 | | - main() |
| 131 | + main() |
0 commit comments