7fc41607727514824390ed35207fde046c65d6b2
[cascardo/ipsilon.git] / ipsilon / login / authtest.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from ipsilon.login.common import LoginFormBase, LoginManagerBase, \
19     LoginManagerInstaller
20 from ipsilon.util.plugin import PluginObject
21 from ipsilon.util import config as pconfig
22 import cherrypy
23
24
25 class TestAuth(LoginFormBase):
26
27     def POST(self, *args, **kwargs):
28         username = kwargs.get("login_name")
29         password = kwargs.get("login_password")
30         error = None
31
32         if username and password:
33             if password == 'ipsilon':
34                 cherrypy.log("User %s successfully authenticated." % username)
35                 testdata = {
36                     'givenname': 'Test User',
37                     'surname': username,
38                     'fullname': 'Test User %s' % username,
39                     'email': '%s@example.com' % username
40                 }
41                 return self.lm.auth_successful(self.trans,
42                                                username, 'password', testdata)
43             else:
44                 cherrypy.log("User %s failed authentication." % username)
45                 error = "Authentication failed"
46         else:
47             error = "Username or password is missing"
48             cherrypy.log.error("Error: " + error)
49
50         context = self.create_tmpl_context(
51             username=username,
52             error=error,
53             error_password=not password,
54             error_username=not username
55         )
56         self.lm.set_auth_error()
57         # pylint: disable=star-args
58         return self._template('login/form.html', **context)
59
60
61 class LoginManager(LoginManagerBase):
62
63     def __init__(self, *args, **kwargs):
64         super(LoginManager, self).__init__(*args, **kwargs)
65         self.name = 'testauth'
66         self.service_name = 'testauth'
67         self.path = 'testauth'
68         self.page = None
69         self.description = """
70 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
71         self.new_config(
72             self.name,
73             pconfig.String(
74                 'username text',
75                 'Text used to ask for the username at login time.',
76                 'Username'),
77             pconfig.String(
78                 'password text',
79                 'Text used to ask for the password at login time.',
80                 'Password'),
81             pconfig.String(
82                 'help text',
83                 'Text used to guide the user at login time.',
84                 'DISABLE IN PRODUCTION, USE ONLY FOR TEST ' +
85                 'Use any username they are all valid, "admin" gives ' +
86                 'administrative powers. ' +
87                 'Use the fixed password "ipsilon" for any user')
88         )
89
90     @property
91     def help_text(self):
92         return self.get_config_value('help text')
93
94     @property
95     def username_text(self):
96         return self.get_config_value('username text')
97
98     @property
99     def password_text(self):
100         return self.get_config_value('password text')
101
102     def get_tree(self, site):
103         self.page = TestAuth(site, self, 'login/testauth')
104         return self.page
105
106
107 class Installer(LoginManagerInstaller):
108
109     def __init__(self, *pargs):
110         super(Installer, self).__init__()
111         self.name = 'testauth'
112         self.pargs = pargs
113
114     def install_args(self, group):
115         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
116                            help='Configure PAM authentication')
117
118     def configure(self, opts):
119         if opts['testauth'] != 'yes':
120             return
121
122         print self.pargs
123         # Add configuration data to database
124         po = PluginObject(*self.pargs)
125         po.name = 'testauth'
126         po.wipe_data()
127
128         # Update global config to add login plugin
129         po.is_enabled = True
130         po.save_enabled_state()