From a14c10a8fc8d1b4e16d7a8961d8a524b18c22675 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Tue, 1 Mar 2016 16:56:24 +0800 Subject: [PATCH] Update init user reset password, and clean code --- seahub/settings.py | 6 +-- seahub/share/settings.py | 4 -- seahub/share/tokens.py | 87 ---------------------------------------- seahub/share/views.py | 2 - 4 files changed, 2 insertions(+), 97 deletions(-) delete mode 100644 seahub/share/settings.py delete mode 100644 seahub/share/tokens.py diff --git a/seahub/settings.py b/seahub/settings.py index 77b07d19d2..a58b859186 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -4,8 +4,6 @@ import sys import os import re -import random -import string from seaserv import FILE_SERVER_ROOT, FILE_SERVER_PORT, SERVICE_URL @@ -353,8 +351,8 @@ REQUIRE_DETAIL_ON_REGISTRATION = False # Account initial password, for password resetting. # INIT_PASSWD can either be a string, or a function (function has to be set without the brackets) def genpassword(): - return ''.join([random.choice(string.digits + string.letters) for i in range(0, 10)]) - + from django.utils.crypto import get_random_string + return get_random_string(10) INIT_PASSWD = genpassword # browser tab title diff --git a/seahub/share/settings.py b/seahub/share/settings.py deleted file mode 100644 index 38e8f66a1f..0000000000 --- a/seahub/share/settings.py +++ /dev/null @@ -1,4 +0,0 @@ -from django.conf import settings - -ANONYMOUS_SHARE_COOKIE_TIMEOUT = getattr(settings, 'ANONYMOUS_SHARE_COOKIE_TIMEOUT', 24*60*60) -ANONYMOUS_SHARE_LINK_TIMEOUT = getattr(settings, 'ANONYMOUS_SHARE_LINK_TIMEOUT', 2) diff --git a/seahub/share/tokens.py b/seahub/share/tokens.py deleted file mode 100644 index dd943b5b0a..0000000000 --- a/seahub/share/tokens.py +++ /dev/null @@ -1,87 +0,0 @@ -import random -from datetime import date -from datetime import datetime as dt -from django.conf import settings -from django.utils.http import int_to_base36, base36_to_int - -from settings import ANONYMOUS_SHARE_LINK_TIMEOUT - -class AnonymousShareTokenGenerator(object): - """ - Strategy object used to generate and check tokens for the repo anonymous - share mechanism. - """ - def make_token(self): - """ - Returns a token that can be used once to do a anonymous share for repo. - """ - return self._make_token_with_timestamp(self._num_days(self._today())) - - def check_token(self, token): - """ - Check that a anonymous share token is valid. - """ - # Parse the token - try: - ts_b36, hash = token.split("-") - except ValueError: - return False - - try: - ts = base36_to_int(ts_b36) - except ValueError: - return False - - # Check the timestamp is within limit - if (self._num_days(self._today()) - ts) > ANONYMOUS_SHARE_LINK_TIMEOUT: - return False - - return True - - def get_remain_time(self, token): - """ - Get token remain time. - """ - try: - ts_b36, hash = token.split("-") - except ValueError: - return None - - try: - ts = base36_to_int(ts_b36) - except ValueError: - return None - - days = ANONYMOUS_SHARE_LINK_TIMEOUT - (self._num_days(self._today()) - ts) - if days < 0: - return None - - now = dt.now() - tomorrow = dt(now.year, now.month, now.day+1) - - return (tomorrow - now).seconds + days * 24 * 60 * 60 - - def _make_token_with_timestamp(self, timestamp): - # timestamp is number of days since 2001-1-1. Converted to - # base 36, this gives us a 3 digit string until about 2121 - ts_b36 = int_to_base36(timestamp) - - # We limit the hash to 20 chars to keep URL short - import datetime - import hashlib - now = datetime.datetime.now() - hash = hashlib.sha1(settings.SECRET_KEY + - unicode(random.randint(0, 999999)) + - now.strftime('%Y-%m-%d %H:%M:%S') + - unicode(timestamp)).hexdigest()[::2] - - return "%s-%s" % (ts_b36, hash) - - def _num_days(self, dt): - return (dt - date(2001,1,1)).days - - def _today(self): - # Used for mocking in tests - return date.today() - -anon_share_token_generator = AnonymousShareTokenGenerator() diff --git a/seahub/share/views.py b/seahub/share/views.py index 389eb3c0e0..4cb67eea55 100644 --- a/seahub/share/views.py +++ b/seahub/share/views.py @@ -28,8 +28,6 @@ from seahub.share.forms import RepoShareForm, FileLinkShareForm, \ from seahub.share.models import FileShare, PrivateFileDirShare, \ UploadLinkShare, OrgFileShare from seahub.share.signals import share_repo_to_user_successful -# from settings import ANONYMOUS_SHARE_COOKIE_TIMEOUT -# from tokens import anon_share_token_generator from seahub.auth.decorators import login_required, login_required_ajax from seahub.base.accounts import User from seahub.base.decorators import user_mods_check, require_POST