Spaces:
Running
Running
0.2.3: sign-in state survives restarts, prefilled example, clear endpoint and credit errors
Browse files- auth.py +20 -5
- index.html +4 -3
- scanner/engine.py +1 -1
- server.py +3 -1
auth.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
The access token is kept in server memory for the session only. It is sent to the ViKernel master so the master
|
| 4 |
can verify the account; it is never written to disk or logs.
|
| 5 |
"""
|
| 6 |
-
import base64, json, os, secrets, time, urllib.parse, urllib.request
|
| 7 |
|
| 8 |
PROVIDER = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co")
|
| 9 |
CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
|
@@ -17,10 +17,25 @@ def available():
|
|
| 17 |
return bool(CLIENT_ID and CLIENT_SECRET and HOST)
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def login_url():
|
| 21 |
-
st =
|
| 22 |
-
for k in [k for k, t in STATES.items() if time.time() - t > 900]:
|
| 23 |
-
STATES.pop(k, None)
|
| 24 |
return PROVIDER + "/oauth/authorize?" + urllib.parse.urlencode({
|
| 25 |
"client_id": CLIENT_ID, "redirect_uri": "https://%s/oauth/callback" % HOST, "scope": "openid profile email inference-api",
|
| 26 |
"state": st, "response_type": "code"})
|
|
@@ -30,7 +45,7 @@ def callback(query):
|
|
| 30 |
"""Returns (session id, error)."""
|
| 31 |
q = urllib.parse.parse_qs(query)
|
| 32 |
code, st = (q.get("code") or [""])[0], (q.get("state") or [""])[0]
|
| 33 |
-
if not code or
|
| 34 |
return None, "state"
|
| 35 |
try:
|
| 36 |
body = urllib.parse.urlencode({"grant_type": "authorization_code", "code": code, "redirect_uri": "https://%s/oauth/callback" % HOST,
|
|
|
|
| 3 |
The access token is kept in server memory for the session only. It is sent to the ViKernel master so the master
|
| 4 |
can verify the account; it is never written to disk or logs.
|
| 5 |
"""
|
| 6 |
+
import base64, hashlib, hmac, json, os, secrets, time, urllib.parse, urllib.request
|
| 7 |
|
| 8 |
PROVIDER = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co")
|
| 9 |
CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
|
|
|
| 17 |
return bool(CLIENT_ID and CLIENT_SECRET and HOST)
|
| 18 |
|
| 19 |
|
| 20 |
+
_KEY = (os.environ.get("BADGE_KEY") or CLIENT_SECRET or secrets.token_hex(16)).encode()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _state():
|
| 24 |
+
t = str(int(time.time())) + "." + secrets.token_hex(6)
|
| 25 |
+
return t + "." + hmac.new(_KEY, t.encode(), hashlib.sha256).hexdigest()[:20]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _state_ok(st):
|
| 29 |
+
try:
|
| 30 |
+
ts, nonce, sig = st.split(".")
|
| 31 |
+
except ValueError:
|
| 32 |
+
return False
|
| 33 |
+
good = hmac.new(_KEY, (ts + "." + nonce).encode(), hashlib.sha256).hexdigest()[:20]
|
| 34 |
+
return hmac.compare_digest(good, sig) and 0 <= time.time() - int(ts) < 900
|
| 35 |
+
|
| 36 |
+
|
| 37 |
def login_url():
|
| 38 |
+
st = _state()
|
|
|
|
|
|
|
| 39 |
return PROVIDER + "/oauth/authorize?" + urllib.parse.urlencode({
|
| 40 |
"client_id": CLIENT_ID, "redirect_uri": "https://%s/oauth/callback" % HOST, "scope": "openid profile email inference-api",
|
| 41 |
"state": st, "response_type": "code"})
|
|
|
|
| 45 |
"""Returns (session id, error)."""
|
| 46 |
q = urllib.parse.parse_qs(query)
|
| 47 |
code, st = (q.get("code") or [""])[0], (q.get("state") or [""])[0]
|
| 48 |
+
if not code or not _state_ok(st):
|
| 49 |
return None, "state"
|
| 50 |
try:
|
| 51 |
body = urllib.parse.urlencode({"grant_type": "authorization_code", "code": code, "redirect_uri": "https://%s/oauth/callback" % HOST,
|
index.html
CHANGED
|
@@ -96,8 +96,8 @@ a{color:var(--ink)}
|
|
| 96 |
<div style="display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap"><h2 data-i="scan_h">Scan a model</h2><div id="who"></div></div>
|
| 97 |
<p class="small" data-i="signnote">Sign in with Hugging Face to scan. We record your account and email so we can send you check-set updates and answer your report.</p>
|
| 98 |
<div class="grid">
|
| 99 |
-
<div><label data-i="l_url">Endpoint (OpenAI-compatible base URL)</label><input type="text" id="url"
|
| 100 |
-
<div><label data-i="l_model">Model name</label><input type="text" id="model"
|
| 101 |
<div><label data-i="l_key">API key</label><input type="password" id="key" placeholder="HF router: leave empty" autocomplete="off"></div>
|
| 102 |
</div>
|
| 103 |
<div class="row">
|
|
@@ -242,6 +242,7 @@ $("go").onclick=async()=>{
|
|
| 242 |
$("err").classList.add("hide");
|
| 243 |
const body={base_url:$("url").value.trim(),model:$("model").value.trim(),api_key:$("key").value.trim(),consent:$("ok").checked};
|
| 244 |
if(!body.base_url||!body.model){return fail(T("Enter the endpoint and the model name.","์ ์ ์ฃผ์์ ๋ชจ๋ธ ์ด๋ฆ์ ๋ฃ์ด ์ฃผ์ธ์."))}
|
|
|
|
| 245 |
if(!body.consent){return fail(T("Please confirm that you may test this model.","์ ๊ฒํด๋ ๋๋ ๋ชจ๋ธ์ธ์ง ํ์ธํด ์ฃผ์ธ์."))}
|
| 246 |
$("go").disabled=true;$("prog").classList.remove("hide");$("pb").style.width="2%";$("log").textContent=T("starting","์์");
|
| 247 |
try{const r=await fetch("/api/scan",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)});const j=await r.json();if(!r.ok){$("go").disabled=false;$("prog").classList.add("hide");if(j.login){ME=null;who()}return fail(j.error||"error")}poll(j.id)}
|
|
@@ -250,7 +251,7 @@ $("go").onclick=async()=>{
|
|
| 250 |
document.querySelectorAll(".switch button").forEach(b=>b.onclick=()=>{L=b.dataset.l;try{localStorage.setItem("vks_lang",L)}catch(e){}statics()});
|
| 251 |
fetch("/api/info").then(r=>r.json()).then(j=>{INFO=j;statics()}).catch(()=>{});
|
| 252 |
fetch("/api/catalog").then(r=>r.json()).then(j=>{if(j&&j.checks){CAT=j;cat()}}).catch(()=>{});
|
| 253 |
-
fetch("/api/me",{credentials:"include"}).then(r=>r.json()).then(j=>{ME=j;who()}).catch(()=>{});
|
| 254 |
statics();
|
| 255 |
</script>
|
| 256 |
</body></html>
|
|
|
|
| 96 |
<div style="display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap"><h2 data-i="scan_h">Scan a model</h2><div id="who"></div></div>
|
| 97 |
<p class="small" data-i="signnote">Sign in with Hugging Face to scan. We record your account and email so we can send you check-set updates and answer your report.</p>
|
| 98 |
<div class="grid">
|
| 99 |
+
<div><label data-i="l_url">Endpoint (OpenAI-compatible base URL)</label><input type="text" id="url" value="https://router.huggingface.co/v1" autocomplete="off"></div>
|
| 100 |
+
<div><label data-i="l_model">Model name</label><input type="text" id="model" value="meta-llama/Llama-3.3-70B-Instruct" autocomplete="off"></div>
|
| 101 |
<div><label data-i="l_key">API key</label><input type="password" id="key" placeholder="HF router: leave empty" autocomplete="off"></div>
|
| 102 |
</div>
|
| 103 |
<div class="row">
|
|
|
|
| 242 |
$("err").classList.add("hide");
|
| 243 |
const body={base_url:$("url").value.trim(),model:$("model").value.trim(),api_key:$("key").value.trim(),consent:$("ok").checked};
|
| 244 |
if(!body.base_url||!body.model){return fail(T("Enter the endpoint and the model name.","์ ์ ์ฃผ์์ ๋ชจ๋ธ ์ด๋ฆ์ ๋ฃ์ด ์ฃผ์ธ์."))}
|
| 245 |
+
if(!/^https?:\/\//i.test(body.base_url)){return fail(T("The endpoint must start with https:// (Hugging Face router: https://router.huggingface.co/v1). The model name goes in the next box.","์ ์ ์ฃผ์๋ https:// ๋ก ์์ํด์ผ ํฉ๋๋ค(Hugging Face ๋ผ์ฐํฐ: https://router.huggingface.co/v1). ๋ชจ๋ธ ์ด๋ฆ์ ์ ์นธ์ ๋ฃ์ต๋๋ค."))}
|
| 246 |
if(!body.consent){return fail(T("Please confirm that you may test this model.","์ ๊ฒํด๋ ๋๋ ๋ชจ๋ธ์ธ์ง ํ์ธํด ์ฃผ์ธ์."))}
|
| 247 |
$("go").disabled=true;$("prog").classList.remove("hide");$("pb").style.width="2%";$("log").textContent=T("starting","์์");
|
| 248 |
try{const r=await fetch("/api/scan",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)});const j=await r.json();if(!r.ok){$("go").disabled=false;$("prog").classList.add("hide");if(j.login){ME=null;who()}return fail(j.error||"error")}poll(j.id)}
|
|
|
|
| 251 |
document.querySelectorAll(".switch button").forEach(b=>b.onclick=()=>{L=b.dataset.l;try{localStorage.setItem("vks_lang",L)}catch(e){}statics()});
|
| 252 |
fetch("/api/info").then(r=>r.json()).then(j=>{INFO=j;statics()}).catch(()=>{});
|
| 253 |
fetch("/api/catalog").then(r=>r.json()).then(j=>{if(j&&j.checks){CAT=j;cat()}}).catch(()=>{});
|
| 254 |
+
fetch("/api/me",{credentials:"include"}).then(r=>r.json()).then(j=>{ME=j;who();const q=new URLSearchParams(location.search).get("login");if(q&&q!=="ok"&&!(j&&j.user)){fail(T("Sign-in did not finish ("+q+"). Press the sign-in button again.","๋ก๊ทธ์ธ์ด ๋๋์ง ์์์ต๋๋ค("+q+"). ๋ก๊ทธ์ธ ๋ฒํผ์ ๋ค์ ๋๋ฌ ์ฃผ์ธ์."))}}).catch(()=>{});
|
| 255 |
statics();
|
| 256 |
</script>
|
| 257 |
</body></html>
|
scanner/engine.py
CHANGED
|
@@ -7,7 +7,7 @@ import ipaddress, json, os, re, socket, time, urllib.error, urllib.parse, urllib
|
|
| 7 |
|
| 8 |
from . import mockenv
|
| 9 |
|
| 10 |
-
VERSION = "0.2.
|
| 11 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
|
| 13 |
DOMAINS = {
|
|
|
|
| 7 |
|
| 8 |
from . import mockenv
|
| 9 |
|
| 10 |
+
VERSION = "0.2.3"
|
| 11 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
|
| 13 |
DOMAINS = {
|
server.py
CHANGED
|
@@ -62,7 +62,9 @@ def run_job(jid, token, base_url, model, key):
|
|
| 62 |
job["report"] = rep; job["status"] = "done"
|
| 63 |
except Exception as e: # noqa: BLE001
|
| 64 |
msg = str(e)[:240]
|
| 65 |
-
if "
|
|
|
|
|
|
|
| 66 |
msg += " | The model endpoint refused the key. For the Hugging Face router, sign out and sign in again (the sign-in now includes model access), or enter your own token."
|
| 67 |
job["status"] = "error"; job["error"] = msg
|
| 68 |
finally:
|
|
|
|
| 62 |
job["report"] = rep; job["status"] = "done"
|
| 63 |
except Exception as e: # noqa: BLE001
|
| 64 |
msg = str(e)[:240]
|
| 65 |
+
if "402" in msg:
|
| 66 |
+
msg += " | This Hugging Face account has no inference credit. Sign in with an account that has credit, or enter a key for another endpoint."
|
| 67 |
+
elif "401" in msg or "403" in msg:
|
| 68 |
msg += " | The model endpoint refused the key. For the Hugging Face router, sign out and sign in again (the sign-in now includes model access), or enter your own token."
|
| 69 |
job["status"] = "error"; job["error"] = msg
|
| 70 |
finally:
|