Move to a common random token generation function instead of several one-offs.

(imported from commit 3217de5384088deff68fbffc6bd481c045a76817)
This commit is contained in:
Jessica McKellar 2013-08-08 10:50:58 -04:00
parent 28a4c1e971
commit 3f8dfc7b9b
3 changed files with 10 additions and 6 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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):