Vote presence: codes may span the enrollment slack as long as one is fresh (slow phones hit 'scan expiré' with 5 scans); phone drops codes too old to count
This commit is contained in:
+7
-3
@@ -736,10 +736,14 @@ async def voter_vote(request: Request):
|
|||||||
tokens = d.get("presence")
|
tokens = d.get("presence")
|
||||||
if not isinstance(tokens, list):
|
if not isinstance(tokens, list):
|
||||||
return err("presence_required")
|
return err("presence_required")
|
||||||
window = config.TOKEN_GRACE_MS + need * p["interval_ms"]
|
# a slow phone can take well over need * interval to see enough distinct codes, so the codes may span
|
||||||
slots = {resolve_token(p, str(t)[:64], window) for t in tokens[:20]}
|
# the same slack as an enrollment, as long as one of them is fresh: the phone sees the screen right now
|
||||||
|
tokens = [str(t)[:64] for t in tokens[:20]]
|
||||||
|
window = config.ATTEMPT_SLACK_MS + need * p["interval_ms"]
|
||||||
|
slots = {resolve_token(p, t, window) for t in tokens}
|
||||||
slots.discard(None)
|
slots.discard(None)
|
||||||
if len(slots) < need:
|
fresh = any(resolve_token(p, t, config.TOKEN_GRACE_MS) is not None for t in tokens)
|
||||||
|
if len(slots) < need or not fresh:
|
||||||
return err("presence_required")
|
return err("presence_required")
|
||||||
e = db.cast(pid, qid, v["id"], choices)
|
e = db.cast(pid, qid, v["id"], choices)
|
||||||
if e:
|
if e:
|
||||||
|
|||||||
@@ -453,9 +453,14 @@ function collectPresence(n) {
|
|||||||
paint();
|
paint();
|
||||||
$("#p-msg").textContent = "";
|
$("#p-msg").textContent = "";
|
||||||
dlg.showModal();
|
dlg.showModal();
|
||||||
|
const seenAt = new Map();
|
||||||
const sc = makeScanner($("#p-video"), (t) => {
|
const sc = makeScanner($("#p-video"), (t) => {
|
||||||
if (got.length >= n || got.includes(t)) return;
|
if (got.length >= n || got.includes(t)) return;
|
||||||
|
// the server takes codes spread over ~8 s + n intervals: on a slow phone the oldest ones lapse, drop them
|
||||||
|
const now = performance.now();
|
||||||
|
while (got.length && now - seenAt.get(got[0]) > 9000) got.shift();
|
||||||
got.push(t);
|
got.push(t);
|
||||||
|
seenAt.set(t, now);
|
||||||
paint();
|
paint();
|
||||||
navigator.vibrate?.(15);
|
navigator.vibrate?.(15);
|
||||||
if (got.length >= n) setTimeout(() => finish(got), FILL_MS);
|
if (got.length >= n) setTimeout(() => finish(got), FILL_MS);
|
||||||
|
|||||||
+4
-3
@@ -518,9 +518,10 @@ def test_vote_needs_fresh_presence_scans(server):
|
|||||||
await asyncio.sleep(3.5) # photo of the screen taken earlier
|
await asyncio.sleep(3.5) # photo of the screen taken earlier
|
||||||
r = await a.post(url, json={"question_id": qid, "choices": [0], "presence": old})
|
r = await a.post(url, json={"question_id": qid, "choices": [0], "presence": old})
|
||||||
assert r.json()["error"] == "presence_required"
|
assert r.json()["error"] == "presence_required"
|
||||||
fresh = [await next_token() for _ in range(3)]
|
# slow phone: two codes read a while ago and one just now, all distinct, counts
|
||||||
r = await a.post(url, json={"question_id": qid, "choices": [1], "presence": fresh})
|
mixed = old[1:] + [await next_token()]
|
||||||
assert r.status_code == 200
|
r = await a.post(url, json={"question_id": qid, "choices": [1], "presence": mixed})
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
await a.aclose()
|
await a.aclose()
|
||||||
|
|
||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|||||||
Reference in New Issue
Block a user