Camera app scan: the big qr rotates every 3 s and one scan with the phone's camera app enrolls or validates a vote (10 s), for browsers with the camera blocked; qr stays big beside the ring during a vote, step by step help on the phone
This commit is contained in:
+105
-28
@@ -35,7 +35,8 @@ def server(tmp_path_factory):
|
||||
port = free_port()
|
||||
tmp = tmp_path_factory.mktemp("db")
|
||||
db = tmp / "test.db"
|
||||
env = {**os.environ, "PORT": str(port), "HOST": "127.0.0.1", "DB_PATH": str(db), "PUBLIC_URL": "", "HEARTBEAT_S": "1"}
|
||||
env = {**os.environ, "PORT": str(port), "HOST": "127.0.0.1", "DB_PATH": str(db), "PUBLIC_URL": "", "HEARTBEAT_S": "1",
|
||||
"PASS_HOLD_MS": "1500"}
|
||||
scheme, wscheme, ws_kw = "http", "ws", {}
|
||||
if TLS:
|
||||
cert, key = tmp / "cert.pem", tmp / "key.pem"
|
||||
@@ -201,8 +202,8 @@ def test_full_flow_and_anonymity(server):
|
||||
assert con.execute("SELECT COUNT(*) FROM participation").fetchone()[0] >= 2
|
||||
|
||||
|
||||
def test_stale_duplicate_and_forged_tokens(server):
|
||||
p = create(server, scans_required=3, code_style="qr")
|
||||
def test_duplicate_token_counts_once(server):
|
||||
p = create(server, scans_required=3)
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": True})
|
||||
|
||||
async def run():
|
||||
@@ -211,25 +212,8 @@ def test_stale_duplicate_and_forged_tokens(server):
|
||||
tok = await next_token()
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tok})).json()
|
||||
assert r["count"] == 1 and r["accepted"]
|
||||
# same code twice does not count twice
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tok})).json()
|
||||
assert r["count"] == 1 and not r["accepted"]
|
||||
# forged mac
|
||||
slot = tok.split(".")[0]
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": f"{int(slot) + 1}.AAAAAAAAAAAA"})).json()
|
||||
assert not r["valid"] and r["count"] == 1
|
||||
# replaying an old photo of the screen
|
||||
old = await next_token()
|
||||
await asyncio.sleep(3.2)
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": old})).json()
|
||||
assert not r["valid"]
|
||||
# a token from another poll is useless here
|
||||
other = create(server)
|
||||
admin(server, other, "PATCH", "", json={"enrollment_open": True})
|
||||
async with projector(server, other) as other_next:
|
||||
ot = await other_next()
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": ot})).json()
|
||||
assert not r["valid"]
|
||||
await c.aclose()
|
||||
|
||||
asyncio.run(run())
|
||||
@@ -455,8 +439,7 @@ def test_behind_proxy(proxied):
|
||||
entry = m
|
||||
if m["type"] == "qr":
|
||||
break
|
||||
assert m["url"].startswith(f"https://vote.example.org/v/{p['id']}#")
|
||||
assert entry["url"] == f"https://vote.example.org/v/{p['id']}"
|
||||
assert m["url"] == entry["url"] == f"https://vote.example.org/v/{p['id']}"
|
||||
await ws.close()
|
||||
# browser origin is the public host, not the rewritten Host: must pass the origin check
|
||||
v = await websockets.connect(f"{proxied['ws']}/ws/{p['id']}/voter", origin="https://vote.example.org",
|
||||
@@ -628,7 +611,7 @@ def test_heartbeat_survives_dead_client(server):
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_ring_tokens_stale_forged_and_cross_format(server):
|
||||
def test_ring_tokens_stale_and_forged(server):
|
||||
from rotatyvote import tokens as tk
|
||||
p = create(server, scans_required=3, code_style="ring")
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": True})
|
||||
@@ -643,12 +626,10 @@ def test_ring_tokens_stale_forged_and_cross_format(server):
|
||||
assert tok.startswith("r")
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tok})).json()
|
||||
assert r["count"] == 1 and r["accepted"]
|
||||
# the same slot in the classic format is the same code: counted once
|
||||
s16 = int(tok[1:].split(".")[0])
|
||||
now = tk.current_slot(iv)
|
||||
full = next(s for s in range(now, now - 10, -1) if s & 0xFFFF == s16)
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tk.make_token(secret, p["id"], iv, full)})).json()
|
||||
assert r["valid"] and not r["accepted"] and r["count"] == 1
|
||||
# a camera app code is not a ring code
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tk.make_pass(secret, p["id"], 1)})).json()
|
||||
assert not r["valid"]
|
||||
# forged mac, malformed
|
||||
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": f"r{s16}.00000000"})).json()
|
||||
assert not r["valid"]
|
||||
@@ -751,3 +732,99 @@ def test_duplicated_screen_page(server):
|
||||
assert (await next_token()) is not None
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
async def camera_code(server, p, kind="entry"):
|
||||
"""the code the camera app would read off the projector right now"""
|
||||
ws = await websockets.connect(f"{server['ws']}/ws/{p['id']}/screen", **server["ws_kw"])
|
||||
await ws.send(json.dumps({"key": p["screen_key"]}))
|
||||
while True:
|
||||
m = json.loads(await asyncio.wait_for(ws.recv(), 5))
|
||||
if m["type"] == kind:
|
||||
await ws.close()
|
||||
return m
|
||||
|
||||
|
||||
def test_camera_app_scan_enrolls_and_covers_votes(server):
|
||||
from rotatyvote import tokens as tk
|
||||
p = create(server, scans_required=5, vote_scans=3)
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": True})
|
||||
|
||||
async def run():
|
||||
e = await camera_code(server, p)
|
||||
assert len(e["modules"]) == 33
|
||||
a = device(server)
|
||||
r = await a.get(f"/V/{p['id'].upper()}/{e['token'].upper()}", follow_redirects=False)
|
||||
assert r.status_code == 303 and "no-store" in r.headers["cache-control"]
|
||||
loc = r.headers["location"]
|
||||
assert loc.startswith(f"/v/{p['id']}#c=")
|
||||
claim = loc.split("#c=")[1]
|
||||
# one scan is enough to enroll, the claim is single use
|
||||
assert (await a.post(f"/api/v/{p['id']}/pass", json={"claim": claim})).json()["status"] == "enrolled"
|
||||
b = device(server)
|
||||
assert (await b.post(f"/api/v/{p['id']}/pass", json={"claim": claim})).json()["status"] == "expired"
|
||||
url = f"/api/v/{p['id']}/vote"
|
||||
q1 = admin(server, p, "POST", "/questions", json={"text": "Q1", "options": ["A", "B"], "open": True}).json()["id"]
|
||||
# the scan that enrolled also covers a vote right after
|
||||
assert (await a.post(url, json={"question_id": q1, "choices": [0]})).status_code == 200
|
||||
await asyncio.sleep(1.6)
|
||||
admin(server, p, "POST", f"/questions/{q1}/close")
|
||||
q2 = admin(server, p, "POST", "/questions", json={"text": "Q2", "options": ["A", "B"], "open": True}).json()["id"]
|
||||
r = await a.post(url, json={"question_id": q2, "choices": [1]})
|
||||
assert r.json()["error"] == "presence_required"
|
||||
# rescan with the camera app (or the page's camera on the classic qr): the code itself
|
||||
e = await camera_code(server, p)
|
||||
assert (await a.post(f"/api/v/{p['id']}/pass", json={"token": e["token"]})).json()["status"] == "pass"
|
||||
assert (await a.post(url, json={"question_id": q2, "choices": [1]})).status_code == 200
|
||||
# forged, malformed, other poll, stale
|
||||
bad = await a.get(f"/V/{p['id']}/{e['slot']}-AAAAAAAAAA", follow_redirects=False)
|
||||
assert bad.headers["location"] == f"/v/{p['id']}#cam=expired"
|
||||
bad = await a.get(f"/V/{p['id']}/%C3%A9-%C3%A9", follow_redirects=False)
|
||||
assert bad.headers["location"] == f"/v/{p['id']}#cam=expired"
|
||||
other = create(server)
|
||||
admin(server, other, "PATCH", "", json={"enrollment_open": True})
|
||||
oe = await camera_code(server, other)
|
||||
assert (await b.post(f"/api/v/{p['id']}/pass", json={"token": oe["token"]})).json()["status"] == "expired"
|
||||
con = sqlite3.connect(server["db"])
|
||||
secret = con.execute("SELECT secret FROM polls WHERE id=?", (p["id"],)).fetchone()[0]
|
||||
con.close()
|
||||
stale = tk.make_pass(secret, p["id"], e["slot"] - 6)
|
||||
assert (await b.post(f"/api/v/{p['id']}/pass", json={"token": stale})).json()["status"] == "expired"
|
||||
# late arrival after enrollment closed: nothing to do here
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": False})
|
||||
e = await camera_code(server, p)
|
||||
assert (await b.post(f"/api/v/{p['id']}/pass", json={"token": e["token"]})).json()["status"] == "closed"
|
||||
for x in (a, b):
|
||||
await x.aclose()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_camera_app_scan_then_paper_code(server):
|
||||
p = create(server, require_code=True, codes=2)
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": True})
|
||||
code = admin(server, p, "GET", "/codes").json()["codes"][0]["code"]
|
||||
|
||||
async def run():
|
||||
e = await camera_code(server, p)
|
||||
a = device(server)
|
||||
assert (await a.post(f"/api/v/{p['id']}/pass", json={"token": e["token"]})).json()["status"] == "need_code"
|
||||
assert (await a.get(f"/api/v/{p['id']}/me")).json()["need_code"]
|
||||
assert (await a.post(f"/api/v/{p['id']}/code", json={"code": code})).json()["status"] == "enrolled"
|
||||
await a.aclose()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_classic_qr_is_the_camera_code(server):
|
||||
p = create(server, code_style="qr", vote_scans=2)
|
||||
admin(server, p, "PATCH", "", json={"enrollment_open": True})
|
||||
|
||||
async def run():
|
||||
m = await camera_code(server, p, "qr")
|
||||
assert m["interval_ms"] == 3000 and len(m["modules"]) == 33
|
||||
a = device(server)
|
||||
assert (await a.post(f"/api/v/{p['id']}/pass", json={"token": m["token"]})).json()["status"] == "enrolled"
|
||||
await a.aclose()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
Reference in New Issue
Block a user