[PATCH 1/3] Add a password verification method to users.py

Jesse Keating jkeating at redhat.com
Fri Oct 12 02:21:04 UTC 2012


This moves it to a more central spot so that it can be used by both the
gui and the text spoke, or any other thing that wants to validate
passwords.
---
 pyanaconda/users.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index c10dd4a..f4d92d4 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -27,11 +27,15 @@ import tempfile
 import os
 import os.path
 import iutil
+import pwquality
 from pyanaconda.constants import ROOT_PATH
 
 import logging
 log = logging.getLogger("anaconda")
 
+import gettext
+_ = lambda x: gettext.ldgettext("anaconda", x)
+
 def createLuserConf(instPath, algoname='sha512'):
     """ Writes a libuser.conf for instPath.
 
@@ -111,6 +115,40 @@ def cryptPassword(password, algo=None):
 
     return crypt.crypt (password, saltstr)
 
+def validatePassword(pw, confirm, minlen=6):
+    # Do various steps to validate the password
+    # Return an error string, or None for no errors
+    # If inital checks pass, pwquality will be tested.  Raises
+    # from pwquality will pass up to the calling code
+
+    # if both pw and confirm are blank, password is disabled.
+    if (pw and not confirm) or (confirm and not pw):
+        error = _("You must enter your root password "
+                  "and confirm it by typing it a second "
+                  "time to continue.")
+        return error
+
+    if pw != confirm:
+        error = _("The passwords you entered were "
+                  "different.  Please try again.")
+        return error
+
+    legal = string.digits + string.ascii_letters + string.punctuation + " "
+    for letter in pw:
+        if letter not in legal:
+            error = _("Requested password contains "
+                      "non-ASCII characters, which are "
+                      "not allowed.")
+            return error
+
+    if pw:
+        settings = pwquality.PWQSettings()
+        settings.read_config()
+        settings.minlen = minlen
+        settings.check(pw, None, "root")
+
+    return None
+
 class Users:
     def __init__ (self):
         self.admin = libuser.admin()
-- 
1.7.11.4



More information about the anaconda-patches mailing list