From 3f8dfc7b9b7507bda8c20382141d582eec098791 Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Thu, 8 Aug 2013 10:50:58 -0400 Subject: [PATCH] Move to a common random token generation function instead of several one-offs. (imported from commit 3217de5384088deff68fbffc6bd481c045a76817) --- confirmation/models.py | 5 ++--- zerver/lib/utils.py | 6 ++++++ zerver/views.py | 5 ++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/confirmation/models.py b/confirmation/models.py index e8f6603dfe..aabd799a61 100644 --- a/confirmation/models.py +++ b/confirmation/models.py @@ -4,9 +4,7 @@ __revision__ = '$Id: models.py 28 2009-10-22 15:03:02Z jarek.zgoda $' -import os import re -import base64 from django.db import models from django.core.urlresolvers import reverse @@ -20,6 +18,7 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.timezone import now from confirmation.util import get_status_field +from zerver.lib.utils import generate_random_token try: import mailer @@ -32,7 +31,7 @@ except ImportError: B16_RE = re.compile('^[a-f0-9]{40}$') def generate_key(): - return base64.b16encode(os.urandom(20)).lower() + return generate_random_token(40) def generate_activation_url(key): current_site = Site.objects.get_current() diff --git a/zerver/lib/utils.py b/zerver/lib/utils.py index a7cb86cd10..f8504a8d4d 100644 --- a/zerver/lib/utils.py +++ b/zerver/lib/utils.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +import base64 import hashlib +import os from time import sleep + from django.conf import settings def statsd_key(val, clean_periods=False): @@ -93,3 +96,6 @@ def log_statsd_event(name): """ event_name = "events.%s" % (name,) statsd.incr(event_name) + +def generate_random_token(length): + return base64.b16encode(os.urandom(length / 2)).lower() diff --git a/zerver/views.py b/zerver/views.py index 7b9715f93e..2052c99e7d 100644 --- a/zerver/views.py +++ b/zerver/views.py @@ -57,7 +57,7 @@ from zerver.lib.cache import cache_get_many, cache_set_many, \ generic_bulk_cached_fetch from zerver.lib.unminify import SourceMap from zerver.lib.queue import queue_json_publish -from zerver.lib.utils import statsd +from zerver.lib.utils import statsd, generate_random_token from zerver import tornado_callbacks from django.db import connection @@ -69,7 +69,6 @@ import ujson import simplejson import re import urllib -import os import base64 import time import logging @@ -1013,7 +1012,7 @@ def get_old_messages_backend(request, user_profile, return json_success(ret) def generate_client_id(): - return base64.b16encode(os.urandom(16)).lower() + return generate_random_token(32) @authenticated_json_post_view def json_get_profile(request, user_profile):