52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
|
|
"""自签 HS256 JWT 并调用测试环境导出接口(只读探测,不写业务数据)"""
|
||
|
|
import base64, hashlib, hmac, json, ssl, sys, time, urllib.request, pymysql
|
||
|
|
|
||
|
|
JWT_SECRET = "32e8708743c42fe764aff1d40737a63803a46815a9501f4bcb50c41fe3d27108"
|
||
|
|
BASE = "https://test.anzhizhichong.com/api/pms"
|
||
|
|
|
||
|
|
|
||
|
|
def b64(b: bytes) -> str:
|
||
|
|
return base64.urlsafe_b64encode(b).rstrip(b"=").decode()
|
||
|
|
|
||
|
|
|
||
|
|
def sign(sub: int, phone: str) -> str:
|
||
|
|
now = int(time.time())
|
||
|
|
header = {"alg": "HS256", "typ": "JWT"}
|
||
|
|
payload = {"sub": sub, "phone": phone, "exp": now + 3600, "iat": now}
|
||
|
|
seg = b64(json.dumps(header, separators=(",", ":")).encode()) + "." + \
|
||
|
|
b64(json.dumps(payload, separators=(",", ":")).encode())
|
||
|
|
sig = hmac.new(JWT_SECRET.encode(), seg.encode(), hashlib.sha256).digest()
|
||
|
|
return seg + "." + b64(sig)
|
||
|
|
|
||
|
|
|
||
|
|
def call(path: str, token: str):
|
||
|
|
req = urllib.request.Request(BASE + path, headers={"Authorization": "Bearer " + token})
|
||
|
|
ctx = ssl.create_default_context()
|
||
|
|
ctx.check_hostname = False
|
||
|
|
ctx.verify_mode = ssl.CERT_NONE
|
||
|
|
try:
|
||
|
|
with urllib.request.urlopen(req, timeout=20, context=ctx) as r:
|
||
|
|
return r.status, r.read().decode("utf-8", "replace")
|
||
|
|
except urllib.error.HTTPError as e:
|
||
|
|
return e.code, e.read().decode("utf-8", "replace")
|
||
|
|
|
||
|
|
|
||
|
|
conn = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database="pms_dev")
|
||
|
|
cur = conn.cursor()
|
||
|
|
cur.execute("SELECT id, phone, role FROM users WHERE role >= 2 ORDER BY id LIMIT 5")
|
||
|
|
admins = cur.fetchall()
|
||
|
|
print("super admins:", admins)
|
||
|
|
conn.close()
|
||
|
|
if not admins:
|
||
|
|
sys.exit("no super admin")
|
||
|
|
|
||
|
|
uid, phone, _ = admins[0]
|
||
|
|
tok = sign(uid, phone or "13800000000")
|
||
|
|
print("token(sub=%s) len=%d" % (uid, len(tok)))
|
||
|
|
|
||
|
|
if len(sys.argv) > 1 and sys.argv[1] == "orders":
|
||
|
|
print("charge-orders:", call("/charge-orders?page=1&page_size=5", tok))
|
||
|
|
else:
|
||
|
|
print("charge-records list:", call("/charge-records?page=1&page_size=5", tok))
|
||
|
|
print("charge-records export:", call("/charge-records/export", tok))
|