新增网站值班确认和按钮测试邮件入口,修复活动邮件跨站确认
This commit is contained in:
@@ -18,6 +18,7 @@ def connect(path):
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_confirmation_forms (digest TEXT PRIMARY KEY, nonce_hash TEXT NOT NULL)")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_absences (day TEXT, slot TEXT, name TEXT, end_at REAL NOT NULL, recorded_at REAL NOT NULL, PRIMARY KEY(day,slot,name))")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_audit_state (key TEXT PRIMARY KEY, value TEXT NOT NULL)")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS mail_button_tests (digest TEXT PRIMARY KEY, kind TEXT NOT NULL, expires REAL NOT NULL, confirmed REAL NOT NULL DEFAULT 0)")
|
||||
return con
|
||||
|
||||
|
||||
@@ -69,6 +70,18 @@ def mark_sent(path, day, names, now):
|
||||
con.commit()
|
||||
|
||||
|
||||
def confirm_on_site(path, day, slot, name, email, now, end_at):
|
||||
if now >= end_at:
|
||||
return None
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("BEGIN IMMEDIATE")
|
||||
ensure(con, day, slot, name, email)
|
||||
con.execute("UPDATE duty_receipts SET confirmed=CASE WHEN confirmed=0 THEN ? ELSE confirmed END WHERE day=? AND slot=? AND name=?", (now, day, slot, name))
|
||||
confirmed = con.execute("SELECT confirmed FROM duty_receipts WHERE day=? AND slot=? AND name=?", (day, slot, name)).fetchone()[0]
|
||||
con.commit()
|
||||
return int(confirmed * 1000)
|
||||
|
||||
|
||||
def issue_confirmation_form(path, token, now):
|
||||
with closing(connect(path)) as con:
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
@@ -81,6 +94,24 @@ def issue_confirmation_form(path, token, now):
|
||||
return nonce
|
||||
|
||||
|
||||
def issue_activity_form(path, token, namespace='activity'):
|
||||
digest = hashlib.sha256((namespace + ':' + token).encode()).hexdigest()
|
||||
nonce = secrets.token_urlsafe(32)
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("INSERT OR REPLACE INTO duty_confirmation_forms VALUES (?,?)", (digest, hashlib.sha256(nonce.encode()).hexdigest()))
|
||||
con.commit()
|
||||
return nonce
|
||||
|
||||
|
||||
def validate_activity_form(path, token, nonce, namespace='activity'):
|
||||
if not nonce:
|
||||
return False
|
||||
digest = hashlib.sha256((namespace + ':' + token).encode()).hexdigest()
|
||||
with closing(connect(path)) as con:
|
||||
row = con.execute("SELECT nonce_hash FROM duty_confirmation_forms WHERE digest=?", (digest,)).fetchone()
|
||||
return bool(row and secrets.compare_digest(row[0], hashlib.sha256(nonce.encode()).hexdigest()))
|
||||
|
||||
|
||||
def confirmation(path, token, now, consume=False, form_token=None, require_form=False):
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("BEGIN IMMEDIATE")
|
||||
@@ -111,6 +142,26 @@ def reserve_test(path, email, now):
|
||||
return True
|
||||
|
||||
|
||||
def create_button_test(path, kind, now):
|
||||
token = secrets.token_urlsafe(32)
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("INSERT INTO mail_button_tests(digest,kind,expires) VALUES (?,?,?)", (hashlib.sha256(token.encode()).hexdigest(), kind, now + 86400))
|
||||
con.commit()
|
||||
return token
|
||||
|
||||
|
||||
def button_test(path, token, now, consume=False):
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
with closing(connect(path)) as con:
|
||||
row = con.execute("SELECT * FROM mail_button_tests WHERE digest=? AND expires>?", (digest, now)).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
if consume:
|
||||
con.execute("UPDATE mail_button_tests SET confirmed=CASE WHEN confirmed=0 THEN ? ELSE confirmed END WHERE digest=?", (now, digest))
|
||||
con.commit()
|
||||
return row['kind']
|
||||
|
||||
|
||||
def sync_absences(path, schedule, now):
|
||||
from .duty import BEIJING, duties_on
|
||||
now = now.astimezone(BEIJING)
|
||||
|
||||
Reference in New Issue
Block a user