forked from CGM_Public/pretix_original
[SECURITY] Tokens for downloading answer attachments
This commit is contained in:
18
src/pretix/helpers/safedownload.py
Normal file
18
src/pretix/helpers/safedownload.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import hashlib
|
||||
|
||||
from django.core.signing import BadSignature, TimestampSigner
|
||||
|
||||
|
||||
def get_token(request, answer):
|
||||
payload = '{}:{}'.format(request.session.session_key, answer.pk)
|
||||
signer = TimestampSigner()
|
||||
return signer.sign(hashlib.sha1(payload.encode()).hexdigest())
|
||||
|
||||
|
||||
def check_token(request, answer, token):
|
||||
payload = hashlib.sha1('{}:{}'.format(request.session.session_key, answer.pk).encode()).hexdigest()
|
||||
signer = TimestampSigner()
|
||||
try:
|
||||
return payload == signer.unsign(token, max_age=3600 * 24)
|
||||
except BadSignature:
|
||||
return False
|
||||
Reference in New Issue
Block a user