Spaces:
Running
Running
Download fix_auth.py from Nrighton233j/Api-cache: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/spaces/Nrighton233j/Api-cache/resolve/main/fix_auth.py
- Command line
-
hf download hf://spaces/Nrighton233j/Api-cache/fix_auth.py
-
curl -L -o fix_auth.py https://huggingface.co/spaces/Nrighton233j/Api-cache/resolve/main/fix_auth.py
1.3 kB
| import re | |
| p = "downloads.py" | |
| s = open(p).read() | |
| new = '''def _clean(v): | |
| return (v or "").strip() | |
| def _valid_tokens(): | |
| names = ("B24_INTERNAL_AUTH_TOKEN", "INTERNAL_AUTH_TOKEN", "INTERNAL_SECRET") | |
| toks = {_clean(os.environ.get(n)) for n in names} | |
| toks.add(_clean(GATEWAY_AUTH_TOKEN)) | |
| toks.add(_clean(INTERNAL_SECRET)) | |
| toks.discard("") | |
| toks.discard("changeme") | |
| return toks | |
| def require_internal(f): | |
| @wraps(f) | |
| def wrapper(*args, **kwargs): | |
| toks = _valid_tokens() | |
| got = { | |
| _clean(request.headers.get("X-Internal-Auth")), | |
| _clean(request.headers.get("X-Internal-Secret")), | |
| } | |
| got.discard("") | |
| if not (toks & got): | |
| print("[auth-debug] 401 on", request.path, | |
| "| tokens configured:", len(toks), | |
| "| header lens:", sorted(len(g) for g in got), | |
| "| configured lens:", sorted(len(t) for t in toks), flush=True) | |
| return jsonify({"ok": False, "error": "unauthorized"}), 401 | |
| return f(*args, **kwargs) | |
| return wrapper | |
| ''' | |
| pat = re.compile(r"def require_internal\(f\):.*?return wrapper\n", re.S) | |
| assert pat.search(s), "require_internal not found" | |
| s = pat.sub(lambda m: new, s, count=1) | |
| open(p, "w").write(s) | |
| print("patched") | |