#!/usr/bin/env bash # End-to-end TOTP test: setup, enable, login challenge, recovery, disable. set -uo pipefail ENTRY="${1:?usage: smoke-2fa.sh }" BASE="http://127.0.0.1:8888/$ENTRY" PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt) totp() { python3 - "$1" <<'PY' import base64, hmac, hashlib, struct, sys, time secret = sys.argv[1].strip().upper() pad = '=' * ((8 - len(secret) % 8) % 8) key = base64.b32decode(secret + pad) counter = int(time.time()) // 30 digest = hmac.new(key, struct.pack('>Q', counter), hashlib.sha1).digest() offset = digest[-1] & 0x0F code = (struct.unpack('>I', digest[offset:offset + 4])[0] & 0x7FFFFFFF) % 1000000 print(str(code).zfill(6)) PY } json_get() { python3 -c "import json,sys;d=json.load(sys.stdin);print(eval('d'+sys.argv[1]))" "$1"; } echo "=== 登录 ===" curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H 'Origin: http://127.0.0.1:8888' \ -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/tmp/l1.json CSRF=$(json_get "['data']['csrf']" /tmp/setup.json SECRET=$(json_get "['data']['secret']" /dev/null curl -s -c /tmp/ck2.txt -H 'Content-Type: application/json' -H 'Origin: http://127.0.0.1:8888' \ -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/tmp/l2.json cat /tmp/l2.json | head -c 160; echo TICKET=$(json_get "['data']['ticket']" /tmp/l3.json head -c 120 /tmp/l3.json; echo CSRF2=$(json_get "['data']['csrf']"