Files
CasSsoService/docker/live_login_test.py
T

61 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import urllib.request, json, base64, ssl
from cryptography.hazmat.primitives.asymmetric import padding as asym_padding
from cryptography.hazmat.primitives.serialization import load_pem_public_key
HOST_HDR = "sso.yunhaixingtong.com"
BASE = "http://192.168.10.157:80"
def req(method, path, data=None, headers=None):
h = {"Host": HOST_HDR}
if headers: h.update(headers)
if data is not None:
data = urllib.parse.urlencode(data).encode()
h["Content-Type"] = "application/x-www-form-urlencoded"
r = urllib.request.Request(BASE+path, data=data, headers=h, method=method)
try:
resp = urllib.request.urlopen(r, timeout=20)
body = resp.read().decode("utf-8","replace")
return resp.status, dict(resp.getheaders()), body
except urllib.error.HTTPError as e:
return e.code, dict(e.headers), e.read().decode("utf-8","replace")
# 1) 取公钥
st, hd, body = req("GET", "/api/public-key")
print("=== /api/public-key ===")
print("status", st)
print("body", body[:600])
pk = json.loads(body)["data"]["publicKey"]
print("publicKey len", len(pk))
print("publicKey head:", pk[:60])
# 重组为合法 PEM(端点把空白去掉了,补回换行)
b64 = pk.replace("-----BEGIN PUBLIC KEY-----","").replace("-----END PUBLIC KEY-----","").replace("-----BEGIN RSA PUBLIC KEY-----","").replace("-----END RSA PUBLIC KEY-----","").replace("\n","").replace(" ","")
pem = "-----BEGIN PUBLIC KEY-----\n" + "\n".join(b64[i:i+64] for i in range(0,len(b64),64)) + "\n-----END PUBLIC KEY-----"
pub = load_pem_public_key(pem.encode())
def enc(pt):
ct = pub.encrypt(pt.encode("utf-8"), asym_padding.PKCS1v15())
return base64.b64encode(ct).decode()
# 用户名/密码:用已知可登录的 yunhaisch(密码需正确才能成功;若不知道正确密码,可先看错误分支)
USERNAME = "yunhaisch"
PASSWORD = input("请输入 yunhaisch 的密码(仅本地测试用,不会外发): ").strip()
euser = enc(USERNAME)
epwd = enc(PASSWORD)
# 2) 登录(不带 service)
st, hd, body = req("POST", "/api/login", data={
"loginname": euser, "pwd": epwd
})
print("\n=== POST /api/login (无 service) ===")
print("status", st)
print("Set-Cookie:", hd.get("Set-Cookie"))
print("body", body[:800])
try:
j = json.loads(body)
print(">>> code=", j.get("code"), "msg=", j.get("msg"))
print(">>> data.url=", (j.get("data") or {}).get("url"))
except Exception as e:
print("parse err", e)