55b30a41d9b3f89d63f862971902993165dbd974
[cascardo/ipsilon.git] / ipsilon / login / authtest.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 from ipsilon.login.common import LoginFormBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
23 import cherrypy
24
25
26 class TestAuth(LoginFormBase):
27
28     def POST(self, *args, **kwargs):
29         username = kwargs.get("login_name")
30         password = kwargs.get("login_password")
31         error = None
32
33         if username and password:
34             if password == 'ipsilon':
35                 cherrypy.log("User %s successfully authenticated." % username)
36                 return self.lm.auth_successful(self.trans,
37                                                username, 'password')
38             else:
39                 cherrypy.log("User %s failed authentication." % username)
40                 error = "Authentication failed"
41         else:
42             error = "Username or password is missing"
43             cherrypy.log.error("Error: " + error)
44
45         context = self.create_tmpl_context(
46             username=username,
47             error=error,
48             error_password=not password,
49             error_username=not username
50         )
51         # pylint: disable=star-args
52         return self._template('login/form.html', **context)
53
54
55 class LoginManager(LoginManagerBase):
56
57     def __init__(self, *args, **kwargs):
58         super(LoginManager, self).__init__(*args, **kwargs)
59         self.name = 'testauth'
60         self.service_name = 'testauth'
61         self.path = 'testauth'
62         self.page = None
63         self.description = """
64 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
65         self._options = {
66             'help text': [
67                 """ The text shown to guide the user at login time. """,
68                 'string',
69                 'Insert your Username and Password and then submit.'
70             ],
71             'username text': [
72                 """ The text shown to ask for the username in the form. """,
73                 'string',
74                 'Username'
75             ],
76             'password text': [
77                 """ The text shown to ask for the password in the form. """,
78                 'string',
79                 'Password'
80             ],
81         }
82
83     @property
84     def help_text(self):
85         return self.get_config_value('help text')
86
87     @property
88     def username_text(self):
89         return self.get_config_value('username text')
90
91     @property
92     def password_text(self):
93         return self.get_config_value('password text')
94
95     def get_tree(self, site):
96         self.page = TestAuth(site, self, 'login/testauth')
97         return self.page
98
99
100 class Installer(object):
101
102     def __init__(self):
103         self.name = 'testauth'
104         self.ptype = 'login'
105
106     def install_args(self, group):
107         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
108                            help='Configure PAM authentication')
109
110     def configure(self, opts):
111         if opts['testauth'] != 'yes':
112             return
113
114         # Add configuration data to database
115         po = PluginObject()
116         po.name = 'testauth'
117         po.wipe_data()
118
119         # Update global config to add login plugin
120         po = PluginObject()
121         po.name = 'global'
122         globalconf = po.get_plugin_config(FACILITY)
123         if 'order' in globalconf:
124             order = globalconf['order'].split(',')
125         else:
126             order = []
127         order.append('testauth')
128         globalconf['order'] = ','.join(order)
129         po.set_config(globalconf)
130         po.save_plugin_config(FACILITY)