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