Create common form handler page
authorSimo Sorce <simo@redhat.com>
Fri, 1 Aug 2014 12:14:58 +0000 (08:14 -0400)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Wed, 24 Sep 2014 18:29:19 +0000 (20:29 +0200)
Reduce duplication

Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
ipsilon/login/authfas.py
ipsilon/login/authform.py
ipsilon/login/authpam.py
ipsilon/login/authtest.py
ipsilon/login/common.py

index 30e09f5..1592cac 100755 (executable)
@@ -3,7 +3,7 @@
 # Copyright (C) 2014 Ipsilon contributors, see COPYING file for license
 
 
-from ipsilon.login.common import LoginPageBase, LoginManagerBase
+from ipsilon.login.common import LoginFormBase, LoginManagerBase
 from ipsilon.login.common import FACILITY
 from ipsilon.util.plugin import PluginObject
 import cherrypy
@@ -12,12 +12,7 @@ from fedora.client.fasproxy import FasProxyClient
 from fedora.client import AuthError
 
 
-class FAS(LoginPageBase):
-
-    def GET(self, *args, **kwargs):
-        context = self.create_tmpl_context()
-        # pylint: disable=star-args
-        return self._template('login/fas.html', **context)
+class FAS(LoginFormBase):
 
     def POST(self, *args, **kwargs):
         username = kwargs.get("login_name")
@@ -49,28 +44,7 @@ class FAS(LoginPageBase):
             error_username=not username
         )
         # pylint: disable=star-args
-        return self._template('login/fas.html', **context)
-
-    def root(self, *args, **kwargs):
-        op = getattr(self, cherrypy.request.method, self.GET)
-        if callable(op):
-            return op(*args, **kwargs)
-
-    def create_tmpl_context(self, **kwargs):
-        next_url = None
-        if self.lm.next_login is not None:
-            next_url = self.lm.next_login.path
-
-        context = {
-            "title": 'Login',
-            "action": '%s/login/fas' % self.basepath,
-            "username_text": self.lm.username_text,
-            "password_text": self.lm.password_text,
-            "description": self.lm.help_text,
-            "next_url": next_url,
-        }
-        context.update(kwargs)
-        return context
+        return self._template(self.formtemplate, **context)
 
 
 class LoginManager(LoginManagerBase):
@@ -79,6 +53,7 @@ class LoginManager(LoginManagerBase):
         super(LoginManager, self).__init__(*args, **kwargs)
         self.name = 'fas'
         self.path = 'fas'
+        self.service_name = 'fas'
         self.page = None
         self.fpc = None
         self.description = """
@@ -145,7 +120,7 @@ Form based login Manager that uses the Fedora Authentication Server
         self.fpc = FasProxyClient(base_url=self.fas_url,
                                   useragent=self.user_agent,
                                   insecure=(self.insecure == 'YES'))
-        self.page = FAS(site, self)
+        self.page = FAS(site, self, 'login/fas', 'login/fas.html')
         return self.page
 
 
index 9e8e56c..c59e722 100755 (executable)
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from ipsilon.login.common import LoginPageBase, LoginManagerBase
+from ipsilon.login.common import LoginFormBase, LoginManagerBase
 from ipsilon.login.common import FACILITY
 from ipsilon.util.plugin import PluginObject
 from ipsilon.util.user import UserSession
@@ -26,12 +26,7 @@ import cherrypy
 import subprocess
 
 
-class Form(LoginPageBase):
-
-    def GET(self, *args, **kwargs):
-        context = self.create_tmpl_context()
-        # pylint: disable=star-args
-        return self._template('login/form.html', **context)
+class Form(LoginFormBase):
 
     def POST(self, *args, **kwargs):
         us = UserSession()
@@ -47,28 +42,6 @@ class Form(LoginPageBase):
                 cherrypy.log.error("Error: %s" % error)
             return self.lm.auth_failed()
 
-    def root(self, *args, **kwargs):
-        op = getattr(self, cherrypy.request.method, self.GET)
-        if callable(op):
-            return op(*args, **kwargs)
-
-    def create_tmpl_context(self, **kwargs):
-        next_url = None
-        if self.lm.next_login is not None:
-            next_url = self.lm.next_login.path
-
-        context = {
-            "title": 'Login',
-            "action": '%s/login/form' % self.basepath,
-            "service_name": self.lm.service_name,
-            "username_text": self.lm.username_text,
-            "password_text": self.lm.password_text,
-            "description": self.lm.help_text,
-            "next_url": next_url,
-        }
-        context.update(kwargs)
-        return context
-
 
 class LoginManager(LoginManagerBase):
 
@@ -120,7 +93,7 @@ Form based login Manager. Relies on mod_intercept_form_submit plugin for
         return self.get_config_value('password text')
 
     def get_tree(self, site):
-        self.page = Form(site, self)
+        self.page = Form(site, self, 'login/form')
         return self.page
 
 
index 663ed19..c88f0a0 100755 (executable)
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from ipsilon.login.common import LoginPageBase, LoginManagerBase
+from ipsilon.login.common import LoginFormBase, LoginManagerBase
 from ipsilon.login.common import FACILITY
 from ipsilon.util.plugin import PluginObject
 import cherrypy
@@ -25,7 +25,7 @@ import pam
 import subprocess
 
 
-class Pam(LoginPageBase):
+class Pam(LoginFormBase):
 
     def _authenticate(self, username, password):
         if self.lm.service_name:
@@ -40,11 +40,6 @@ class Pam(LoginPageBase):
         cherrypy.log("User %s failed authentication." % username)
         return None
 
-    def GET(self, *args, **kwargs):
-        context = self.create_tmpl_context()
-        # pylint: disable=star-args
-        return self._template('login/form.html', **context)
-
     def POST(self, *args, **kwargs):
         username = kwargs.get("login_name")
         password = kwargs.get("login_password")
@@ -71,28 +66,6 @@ class Pam(LoginPageBase):
         # pylint: disable=star-args
         return self._template('login/form.html', **context)
 
-    def root(self, *args, **kwargs):
-        op = getattr(self, cherrypy.request.method, self.GET)
-        if callable(op):
-            return op(*args, **kwargs)
-
-    def create_tmpl_context(self, **kwargs):
-        next_url = None
-        if self.lm.next_login is not None:
-            next_url = self.lm.next_login.path
-
-        context = {
-            "title": 'Login',
-            "action": '%s/login/pam' % self.basepath,
-            "service_name": self.lm.service_name,
-            "username_text": self.lm.username_text,
-            "password_text": self.lm.password_text,
-            "description": self.lm.help_text,
-            "next_url": next_url,
-        }
-        context.update(kwargs)
-        return context
-
 
 class LoginManager(LoginManagerBase):
 
@@ -144,7 +117,7 @@ for authentication. """
         return self.get_config_value('password text')
 
     def get_tree(self, site):
-        self.page = Pam(site, self)
+        self.page = Pam(site, self, 'login/pam')
         return self.page
 
 
index 7b66066..df826c8 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from ipsilon.login.common import LoginPageBase, LoginManagerBase
+from ipsilon.login.common import LoginFormBase, LoginManagerBase
 from ipsilon.login.common import FACILITY
 from ipsilon.util.plugin import PluginObject
 import cherrypy
 
 
-class TestAuth(LoginPageBase):
-
-    def GET(self, *args, **kwargs):
-        context = self.create_tmpl_context()
-        # pylint: disable=star-args
-        return self._template('login/form.html', **context)
+class TestAuth(LoginFormBase):
 
     def POST(self, *args, **kwargs):
         username = kwargs.get("login_name")
@@ -55,33 +50,13 @@ class TestAuth(LoginPageBase):
         # pylint: disable=star-args
         return self._template('login/form.html', **context)
 
-    def root(self, *args, **kwargs):
-        op = getattr(self, cherrypy.request.method, self.GET)
-        if callable(op):
-            return op(*args, **kwargs)
-
-    def create_tmpl_context(self, **kwargs):
-        next_url = None
-        if self.lm.next_login is not None:
-            next_url = self.lm.next_login.path
-
-        context = {
-            "title": 'TEST Login',
-            "action": '%s/login/testauth' % self.basepath,
-            "username_text": self.lm.username_text,
-            "password_text": self.lm.password_text,
-            "description": self.lm.help_text,
-            "next_url": next_url,
-        }
-        context.update(kwargs)
-        return context
-
 
 class LoginManager(LoginManagerBase):
 
     def __init__(self, *args, **kwargs):
         super(LoginManager, self).__init__(*args, **kwargs)
         self.name = 'testauth'
+        self.service_name = 'testauth'
         self.path = 'testauth'
         self.page = None
         self.description = """
@@ -117,7 +92,7 @@ Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
         return self.get_config_value('password text')
 
     def get_tree(self, site):
-        self.page = TestAuth(site, self)
+        self.page = TestAuth(site, self, 'login/testauth')
         return self.page
 
 
index 5470626..b451550 100755 (executable)
@@ -126,6 +126,41 @@ class LoginPageBase(Page):
         raise cherrypy.HTTPError(500)
 
 
+class LoginFormBase(LoginPageBase):
+
+    def __init__(self, site, mgr, page, template=None):
+        super(LoginFormBase, self).__init__(site, mgr)
+        self.formpage = page
+        self.formtemplate = template or 'login/form.html'
+
+    def GET(self, *args, **kwargs):
+        context = self.create_tmpl_context()
+        # pylint: disable=star-args
+        return self._template(self.formtemplate, **context)
+
+    def root(self, *args, **kwargs):
+        op = getattr(self, cherrypy.request.method, self.GET)
+        if callable(op):
+            return op(*args, **kwargs)
+
+    def create_tmpl_context(self, **kwargs):
+        next_url = None
+        if self.lm.next_login is not None:
+            next_url = self.lm.next_login.path
+
+        context = {
+            "title": 'Login',
+            "action": '%s/%s' % (self.basepath, self.formpage),
+            "service_name": self.lm.service_name,
+            "username_text": self.lm.username_text,
+            "password_text": self.lm.password_text,
+            "description": self.lm.help_text,
+            "next_url": next_url,
+        }
+        context.update(kwargs)
+        return context
+
+
 FACILITY = 'login_config'