From: Michael Tremer Date: Sun, 29 May 2022 14:58:02 +0000 (+0000) Subject: misc: Move generate_random_string() to here X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=301f9ea2c291eef5e3f678d10fe8eafd77d668c1;p=pbs.git misc: Move generate_random_string() to here Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builders.py b/src/buildservice/builders.py index 7a34d8b1..208fc4ae 100644 --- a/src/buildservice/builders.py +++ b/src/buildservice/builders.py @@ -9,6 +9,7 @@ import time from . import base from . import logs +from . import misc from .decorators import * @@ -143,7 +144,7 @@ class Builder(base.DataObject): The new passphrase is returned to be sent to the user (once). """ # Generate a random string with 40 chars. - passphrase = generate_random_string(length=40) + passphrase = misc.generate_random_string(length=40) # Create salted hash. passphrase_hash = generate_password_hash(passphrase) @@ -398,22 +399,13 @@ class Builder(base.DataObject): return True -# A list of possible random characters. -random_chars = string.ascii_letters + string.digits - -def generate_random_string(length=16): - """ - Return a string with random chararcters A-Za-z0-9 with given length. - """ - return "".join([random.choice(random_chars) for i in range(length)]) - def generate_password_hash(password, salt=None, algo="sha512"): """ This function creates a salted digest of the given password. """ # Generate the salt (length = 16) of none was given. if salt is None: - salt = generate_random_string(length=16) + salt = misc.generate_random_string(length=16) # Compute the hash. # + diff --git a/src/buildservice/misc.py b/src/buildservice/misc.py index 4a0cf76e..856a464c 100644 --- a/src/buildservice/misc.py +++ b/src/buildservice/misc.py @@ -1,14 +1,23 @@ #!/usr/bin/python - - import hashlib import os +import random import re +import string import tarfile from .constants import * +# A list of possible random characters. +random_chars = string.ascii_letters + string.digits + +def generate_random_string(length=16): + """ + Return a string with random chararcters A-Za-z0-9 with given length. + """ + return "".join([random.choice(random_chars) for i in range(length)]) + def format_size(s): units = ("B", "k", "M", "G", "T") diff --git a/src/buildservice/sessions.py b/src/buildservice/sessions.py index c6214898..46f17d16 100644 --- a/src/buildservice/sessions.py +++ b/src/buildservice/sessions.py @@ -1,7 +1,7 @@ #!/usr/bin/python from . import base -from . import users +from . import misc from .decorators import * @@ -31,7 +31,7 @@ class Sessions(base.Object): The user is not checked and it is assumed that the user exists and has the right to log in. """ - session_id = users.generate_random_string(48) + session_id = misc.generate_random_string(48) return self._get_session("INSERT INTO sessions(session_id, user_id, address, user_agent) \ VALUES(%s, %s, %s, %s) RETURNING *", session_id, user.id, address, user_agent)