]> git.ipfire.org Git - pbs.git/commitdiff
misc: Move generate_random_string() to here
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 14:58:02 +0000 (14:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 14:58:02 +0000 (14:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/builders.py
src/buildservice/misc.py
src/buildservice/sessions.py

index 7a34d8b1c6b7d49998ec764ad5f86ff3f7edbe0f..208fc4aeec0540d7dc070c156a2c9ec9c4312980 100644 (file)
@@ -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.
        # <SALT> + <PASSWORD>
index 4a0cf76efa575115bf58499217a4bb29a4ed9f77..856a464c09c85562dddb5a6efa39ad4f93c569fa 100644 (file)
@@ -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")
 
index c6214898ab299bab37ee2f95ec5b308d7ee0480c..46f17d1635149ae7a49297febe450de805eb3bc6 100644 (file)
@@ -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)