Add transactions support
[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(username, 'password')
37             else:
38                 cherrypy.log("User %s failed authentication." % username)
39                 error = "Authentication failed"
40         else:
41             error = "Username or password is missing"
42             cherrypy.log.error("Error: " + error)
43
44         context = self.create_tmpl_context(
45             username=username,
46             error=error,
47             error_password=not password,
48             error_username=not username
49         )
50         # pylint: disable=star-args
51         return self._template('login/form.html', **context)
52
53
54 class LoginManager(LoginManagerBase):
55
56     def __init__(self, *args, **kwargs):
57         super(LoginManager, self).__init__(*args, **kwargs)
58         self.name = 'testauth'
59         self.service_name = 'testauth'
60         self.path = 'testauth'
61         self.page = None
62         self.description = """
63 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
64         self._options = {
65             'help text': [
66                 """ The text shown to guide the user at login time. """,
67                 'string',
68                 'Insert your Username and Password and then submit.'
69             ],
70             'username text': [
71                 """ The text shown to ask for the username in the form. """,
72                 'string',
73                 'Username'
74             ],
75             'password text': [
76                 """ The text shown to ask for the password in the form. """,
77                 'string',
78                 'Password'
79             ],
80         }
81
82     @property
83     def help_text(self):
84         return self.get_config_value('help text')
85
86     @property
87     def username_text(self):
88         return self.get_config_value('username text')
89
90     @property
91     def password_text(self):
92         return self.get_config_value('password text')
93
94     def get_tree(self, site):
95         self.page = TestAuth(site, self, 'login/testauth')
96         return self.page
97
98
99 class Installer(object):
100
101     def __init__(self):
102         self.name = 'testauth'
103         self.ptype = 'login'
104
105     def install_args(self, group):
106         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
107                            help='Configure PAM authentication')
108
109     def configure(self, opts):
110         if opts['testauth'] != 'yes':
111             return
112
113         # Add configuration data to database
114         po = PluginObject()
115         po.name = 'testauth'
116         po.wipe_data()
117
118         # Update global config to add login plugin
119         po = PluginObject()
120         po.name = 'global'
121         globalconf = po.get_plugin_config(FACILITY)
122         if 'order' in globalconf:
123             order = globalconf['order'].split(',')
124         else:
125             order = []
126         order.append('testauth')
127         globalconf['order'] = ','.join(order)
128         po.set_config(globalconf)
129         po.save_plugin_config(FACILITY)