3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from ipsilon.login.common import LoginPageBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
26 class TestAuth(LoginPageBase):
28 def GET(self, *args, **kwargs):
29 context = self.create_tmpl_context()
30 # pylint: disable=star-args
31 return self._template('login/pam.html', **context)
33 def POST(self, *args, **kwargs):
34 username = kwargs.get("login_name")
35 password = kwargs.get("login_password")
38 if username and password:
39 if password == 'ipsilon':
40 cherrypy.log("User %s successfully authenticated." % username)
41 return self.lm.auth_successful(username)
43 cherrypy.log("User %s failed authentication." % username)
44 error = "Authentication failed"
46 error = "Username or password is missing"
47 cherrypy.log.error("Error: " + error)
49 context = self.create_tmpl_context(
52 error_password=not password,
53 error_username=not username
55 # pylint: disable=star-args
56 return self._template('login/pam.html', **context)
58 def root(self, *args, **kwargs):
59 op = getattr(self, cherrypy.request.method, self.GET)
61 return op(*args, **kwargs)
63 def create_tmpl_context(self, **kwargs):
65 if self.lm.next_login is not None:
66 next_url = self.lm.next_login.path
69 "title": 'TEST Login',
70 "action": '%s/login/testauth' % self.basepath,
71 "username_text": self.lm.username_text,
72 "password_text": self.lm.password_text,
73 "description": self.lm.help_text,
76 context.update(kwargs)
80 class LoginManager(LoginManagerBase):
82 def __init__(self, *args, **kwargs):
83 super(LoginManager, self).__init__(*args, **kwargs)
84 self.name = 'testauth'
85 self.path = 'testauth'
87 self.description = """
88 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
91 """ The text shown to guide the user at login time. """,
93 'Insert your Username and Password and then submit.'
96 """ The text shown to ask for the username in the form. """,
101 """ The text shown to ask for the password in the form. """,
109 return self.get_config_value('help text')
112 def username_text(self):
113 return self.get_config_value('username text')
116 def password_text(self):
117 return self.get_config_value('password text')
119 def get_tree(self, site):
120 self.page = TestAuth(site, self)
124 class Installer(object):
127 self.name = 'testauth'
130 def install_args(self, group):
131 group.add_argument('--testauth', choices=['yes', 'no'], default='no',
132 help='Configure PAM authentication')
134 def configure(self, opts):
135 if opts['testauth'] != 'yes':
138 # Add configuration data to database
143 # Update global config to add login plugin
146 globalconf = po.get_plugin_config(FACILITY)
147 if 'order' in globalconf:
148 order = globalconf['order'].split(',')
151 order.append('testauth')
152 globalconf['order'] = ','.join(order)
153 po.set_config(globalconf)
154 po.save_plugin_config(FACILITY)