Revision ef48be59 snf-cyclades-app/synnefo/api/util.py

b/snf-cyclades-app/synnefo/api/util.py
81 81
log = getLogger('synnefo.api')
82 82

  
83 83

  
84
def random_password():
84
def random_password(length=10):
85 85
    """Generates a random password
86 86

  
87 87
    We generate a windows compliant password: it must contain at least
88
    one charachter from each of the groups: upper case, lower case, digits.
88
    one character from each of the groups: upper case, lower case, digits.
89 89
    """
90 90

  
91
    pool = lowercase + uppercase + digits
91
    pool = (lowercase * 5) + (uppercase * 5) + (digits * 13)
92 92
    lowerset = set(lowercase)
93 93
    upperset = set(uppercase)
94 94
    digitset = set(digits)
95
    length = 10
95
    valid_pass = False
96 96

  
97
    password = ''.join(choice(pool) for i in range(length - 2))
97
    while True:
98
        password = ''.join(choice(pool) for i in range(length))
98 99

  
99
    # Make sure the password is compliant
100
    chars = set(password)
101
    if not chars & lowerset:
102
        password += choice(lowercase)
103
    if not chars & upperset:
104
        password += choice(uppercase)
105
    if not chars & digitset:
106
        password += choice(digits)
100
        # Make sure the password is compliant
101
        chars = set(password)
102
        if (chars & lowerset) and (chars & upperset) and (chars & digitset):
103
            return password
107 104

  
108
    # Pad if necessary to reach required length
109
    password += ''.join(choice(pool) for i in range(length - len(password)))
110

  
111
    return password
105
        password = ""
112 106

  
113 107

  
114 108
def zeropad(s):

Also available in: Unified diff