Skip to content

Commit 4a90f88

Browse files
committed
Added date verification on signed file to download
1 parent a883019 commit 4a90f88

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/imio/esign/browser/views.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from datetime import datetime
3+
from datetime import timedelta
24
from imio.esign import _
35
from imio.esign import ESIGN_CREDENTIALS
46
from imio.esign import ESIGN_ROOT_URL
@@ -232,6 +234,7 @@ class DownloadFileView(BrowserView):
232234

233235
shortuid_separator = "-"
234236
named_blob_file_attribute = "file"
237+
download_time_delta = timedelta(days=120)
235238

236239
def __init__(self, context, request):
237240
super(DownloadFileView, self).__init__(context, request)
@@ -250,7 +253,7 @@ def publishTraverse(self, request, name):
250253
return self
251254

252255
def __call__(self):
253-
"""Handle the file download request and return an html response."""
256+
"""Handle the file download request and return a html response."""
254257
if self.file_id is None:
255258
message = translate(_("A file identifier must be passed in the url !"), context=self.request)
256259
return self.html_message(message)
@@ -264,7 +267,20 @@ def __call__(self):
264267
mapping={"uid": safe_encode(self.file_id)}),
265268
context=self.request)
266269
return self.html_message(message)
267-
# TODO Added a date verification
270+
# Verify date - check if file is not too old
271+
if self.download_time_delta is not None:
272+
modification_date = file_obj.modified()
273+
if hasattr(modification_date, 'asdatetime'):
274+
modification_date = modification_date.asdatetime()
275+
modification_date = modification_date.date()
276+
if datetime.now().date() - modification_date > self.download_time_delta:
277+
message = translate(
278+
_("The download period for this file has expired (was ${valid_date}) !",
279+
mapping={"valid_date": datetime.strftime(modification_date + self.download_time_delta,
280+
"%Y-%m-%d")}),
281+
context=self.request)
282+
return self.html_message(message)
283+
# Get file content
268284
nbf = getattr(file_obj, self.named_blob_file_attribute, None)
269285
if nbf is None:
270286
message = translate(_("The corresponding file content cannot be retrieved (${uid}) !",

src/imio/esign/tests/test_browser_views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""Browser views tests for this package."""
3+
from datetime import datetime
4+
from datetime import timedelta
35
from imio.esign.browser.views import DownloadFileView
46
from imio.esign.testing import IMIO_ESIGN_FUNCTIONAL_TESTING
57
from imio.pyutils.utils import shortuid_encode_id
@@ -13,6 +15,7 @@
1315

1416
import collective.iconifiedcategory
1517
import os
18+
import time
1619
import transaction
1720
import unittest
1821

@@ -119,8 +122,20 @@ def test_download_file_view(self):
119122
result = view()
120123
self.assertIn("The corresponding file content cannot be retrieved", result)
121124

125+
# valid id but file too old
126+
view.file_id = self.encoded_uid
127+
view.download_time_delta = timedelta(days=1)
128+
self.test_annex.setModificationDate(datetime.now() - timedelta(days=3))
129+
result = view()
130+
self.assertIn("The download period for this file has expired", result)
131+
view.download_time_delta = None # Disable date verification
132+
result = view()
133+
self.assertNotIn("The download period for this file has expired", result)
134+
self.assertIsInstance(result, str)
135+
122136
# Download file with valid UID
123137
view.file_id = self.encoded_uid
138+
view.download_time_delta = timedelta(days=7)
124139
result = view()
125140
# Check that we got binary data (the file content)
126141
self.assertIsInstance(result, str) # In Python 2, binary data is str

0 commit comments

Comments
 (0)