[PATCH jk-tui 1/2] Add some validation to the text rootpw spoke

Jesse Keating jkeating at redhat.com
Tue Aug 21 23:44:04 UTC 2012


This copies some code over from textw/userauth_text and massages it a
bit to work in the new text UI.  See comments for some code that could
move to a higher level and other notes.
---
 pyanaconda/ui/tui/spokes/password.py | 52 ++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/pyanaconda/ui/tui/spokes/password.py b/pyanaconda/ui/tui/spokes/password.py
index ccf3f33..eb9ff1e 100644
--- a/pyanaconda/ui/tui/spokes/password.py
+++ b/pyanaconda/ui/tui/spokes/password.py
@@ -22,6 +22,8 @@
 from pyanaconda.ui.tui.spokes import NormalTUISpoke
 from pyanaconda.ui.tui.simpleline import TextWidget
 import getpass
+import pwquality
+import string
 
 import gettext
 _ = lambda x: gettext.ldgettext("anaconda", x)
@@ -58,8 +60,8 @@ class PasswordSpoke(NormalTUISpoke):
         p1 = getpass.getpass(_("Password: "))
         p2 = getpass.getpass(_("Password (confirm): "))
 
-        if p1 != p2:
-            print _("Passwords do not match!")
+        if not self.validate(p1, p2):
+            return None
         else:
             self._password = p1
             self.apply()
@@ -70,3 +72,49 @@ class PasswordSpoke(NormalTUISpoke):
     def apply(self):
         self.data.rootpw.password = self._password
         self.data.rootpw.isCrypted = False
+
+    def validate(self, p1, p2):
+        # Check length of password
+        if len(p1) < 6:
+            print(_("The root password must be at least 6 characters long."))
+            return False
+        # Check equality
+        elif p1 != p2:
+            print(_("The passwords you entered were different. Please "
+                  "try again."))
+            return False
+        # See if there are bad characters
+        elif self.hasBadChars(p1):
+            print(_("Requested password contains non-ASCII characters, "
+                    "which are not allowed."))
+            return False
+        # Check the quality
+        else:
+            try:
+                settings = pwquality.PWQSettings()
+                settings.read_config()
+                settings.check(p1, None, "root")
+                return True
+            except pwquality.PWQError as (e, msg):
+                # This translation may not work.  Taken from textw/userauth_text
+                # where it actually did the substitution /inside/ the _() call.
+                print(_("You have provided a weak password: %s\n\n"
+                        "Would you like to continue with this password?") % msg)
+                # This yesno thing feels like it should be a library provided
+                # utility, instead of written out here.  Move it somewhere
+                # better someday.  jlk
+                yesno = raw_input(_("Please enter y for yes or n for no: "))
+                if yesno in [_("y"), _("Y"), _("yes"), _("YES"), _("Yes")]:
+                    return True
+                else:
+                    return False
+
+    # this also seems like something useful to other spokes and could move
+    # somewhere more common
+    def hasBadChars(self, pw):
+        allowed = string.digits + string.ascii_letters + \
+                  string.punctuation + " "
+        for letter in pw:
+            if letter not in allowed:
+                return True
+        return False
-- 
1.7.11.2



More information about the anaconda-patches mailing list