-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelper.py
More file actions
61 lines (55 loc) · 1.21 KB
/
Copy pathhelper.py
File metadata and controls
61 lines (55 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import json
import shutil
from datetime import datetime
from jwcrypto.jwt import JWT
from jwcrypto.jwk import JWK
import base64
from math import floor
import uuid
import sys
def rmDir(path):
if os.path.exists(path):
shutil.rmtree(path)
def copyDir(src, dst):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copyDir(s, d)
else:
shutil.copy2(s, d)
def readJson(path):
with open(path, 'r') as f:
return json.load(f)
def saveJson(path, data):
with open(path, 'w') as f:
json.dump(data, f, indent=3)
def generateToken(issuer, key):
timestamp = floor(datetime.utcnow().timestamp())
data = {
'iss': issuer,
'iat': timestamp,
'exp': timestamp + 300,
'jti': str(uuid.uuid4())
}
key_b64 = (
base64
.urlsafe_b64encode(key.encode('ascii'))
.decode()
)
jwk = JWK(
k=key_b64,
kty='oct'
)
token = JWT(header={'alg': 'HS256'}, claims=data)
token.make_signed_token(jwk)
return token.serialize()
def checkResponse(response):
try:
response.raise_for_status()
except:
print(response.json(), file=sys.stderr)
raise