File size: 642 Bytes
345855e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from __future__ import annotations

import hmac
import secrets


def create_download_token(secret: str, job_id: str) -> str:
    nonce = secrets.token_urlsafe(18)
    signature = hmac.digest(secret.encode("utf-8"), f"{job_id}:{nonce}".encode("utf-8"), "sha256").hex()
    return f"{nonce}.{signature}"


def verify_download_token(secret: str, job_id: str, token: str | None) -> bool:
    if not token or "." not in token:
        return False
    nonce, signature = token.split(".", 1)
    expected = hmac.digest(secret.encode("utf-8"), f"{job_id}:{nonce}".encode("utf-8"), "sha256").hex()
    return hmac.compare_digest(signature, expected)