impl/python/consume_email_token.py
1,225 bytes · the Python implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import Sequence
from .auth_email_token_check_email_token import find_email_token ← checkEmailToken, another function of this group · built into the same file, even by a slim install
from .auth_email_token_types import EmailTokenRecord, EmailTokenResult
def _used(r: EmailTokenRecord, now: int) -> EmailTokenRecord:
return EmailTokenRecord(
token_hash=r.token_hash, purpose=r.purpose, subject=r.subject, expires_at=r.expires_at, used_at=now
)
def consume_email_token(records: Sequence[EmailTokenRecord], token: str, purpose: str, now: int) -> EmailTokenResult:
"""Uses a token up, as a pure state change. When ok, the token's record
and every other unused record of the same subject and purpose get
used_at = now; any other outcome returns the records unchanged."""
index, check = find_email_token(records, token, purpose, now)
if check.outcome != "ok" or check.record is None:
return EmailTokenResult(outcome=check.outcome, record=check.record, records=list(records))
used = check.record
nxt = [
_used(r, now)
if i == index or (r.used_at is None and r.subject == used.subject and r.purpose == used.purpose)
else r
for i, r in enumerate(records)
]
return EmailTokenResult(outcome="ok", record=nxt[index], records=nxt)