e3f8eff74486c0bdd072224712895c2f63ebeebc
[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 from ipsilon.util import config as pconfig
24 import cherrypy
25
26
27 class TestAuth(LoginFormBase):
28
29     def POST(self, *args, **kwargs):
30         username = kwargs.get("login_name")
31         password = kwargs.get("login_password")
32         error = None
33
34         if username and password:
35             if password == 'ipsilon':
36                 cherrypy.log("User %s successfully authenticated." % username)
37                 testdata = {'fullname': 'Test User %s' % username}
38                 return self.lm.auth_successful(self.trans,
39                                                username, 'password', testdata)
40             else:
41                 cherrypy.log("User %s failed authentication." % username)
42                 error = "Authentication failed"
43         else:
44             error = "Username or password is missing"
45             cherrypy.log.error("Error: " + error)
46
47         context = self.create_tmpl_context(
48             username=username,
49             error=error,
50             error_password=not password,
51             error_username=not username
52         )
53         # pylint: disable=star-args
54         return self._template('login/form.html', **context)
55
56
57 class LoginManager(LoginManagerBase):
58
59     def __init__(self, *args, **kwargs):
60         super(LoginManager, self).__init__(*args, **kwargs)
61         self.name = 'testauth'
62         self.service_name = 'testauth'
63         self.path = 'testauth'
64         self.page = None
65         self.description = """
66 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
67         self.new_config(
68             self.name,
69             pconfig.String(
70                 'username text',
71                 'Text used to ask for the username at login time.',
72                 'Username'),
73             pconfig.String(
74                 'password text',
75                 'Text used to ask for the password at login time.',
76                 'Password'),
77             pconfig.String(
78                 'help text',
79                 'Text used to guide the user at login time.',
80                 'DISABLE IN PRODUCTION, USE ONLY FOR TEST ' +
81                 'Use any username they are all valid, "admin" gives ' +
82                 'administrative powers. ' +
83                 'Use the fixed password "ipsilon" for any user')
84         )
85
86     @property
87     def help_text(self):
88         return self.get_config_value('help text')
89
90     @property
91     def username_text(self):
92         return self.get_config_value('username text')
93
94     @property
95     def password_text(self):
96         return self.get_config_value('password text')
97
98     def get_tree(self, site):
99         self.page = TestAuth(site, self, 'login/testauth')
100         return self.page
101
102
103 class Installer(object):
104
105     def __init__(self):
106         self.name = 'testauth'
107         self.ptype = 'login'
108
109     def install_args(self, group):
110         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
111                            help='Configure PAM authentication')
112
113     def configure(self, opts):
114         if opts['testauth'] != 'yes':
115             return
116
117         # Add configuration data to database
118         po = PluginObject()
119         po.name = 'testauth'
120         po.wipe_data()
121
122         # Update global config to add login plugin
123         po = PluginObject()
124         po.name = 'global'
125         globalconf = po.get_plugin_config(FACILITY)
126         if 'order' in globalconf:
127             order = globalconf['order'].split(',')
128         else:
129             order = []
130         order.append('testauth')
131         globalconf['order'] = ','.join(order)
132         po.save_plugin_config(FACILITY, globalconf)