8a245000a3526cd72deeb722790c0cfe1a1bae13
[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.util.plugin import PluginObject
22 from ipsilon.util import config as pconfig
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                 testdata = {'fullname': 'Test User %s' % username}
37                 return self.lm.auth_successful(self.trans,
38                                                username, 'password', testdata)
39             else:
40                 cherrypy.log("User %s failed authentication." % username)
41                 error = "Authentication failed"
42         else:
43             error = "Username or password is missing"
44             cherrypy.log.error("Error: " + error)
45
46         context = self.create_tmpl_context(
47             username=username,
48             error=error,
49             error_password=not password,
50             error_username=not username
51         )
52         # pylint: disable=star-args
53         return self._template('login/form.html', **context)
54
55
56 class LoginManager(LoginManagerBase):
57
58     def __init__(self, *args, **kwargs):
59         super(LoginManager, self).__init__(*args, **kwargs)
60         self.name = 'testauth'
61         self.service_name = 'testauth'
62         self.path = 'testauth'
63         self.page = None
64         self.description = """
65 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
66         self.new_config(
67             self.name,
68             pconfig.String(
69                 'username text',
70                 'Text used to ask for the username at login time.',
71                 'Username'),
72             pconfig.String(
73                 'password text',
74                 'Text used to ask for the password at login time.',
75                 'Password'),
76             pconfig.String(
77                 'help text',
78                 'Text used to guide the user at login time.',
79                 'DISABLE IN PRODUCTION, USE ONLY FOR TEST ' +
80                 'Use any username they are all valid, "admin" gives ' +
81                 'administrative powers. ' +
82                 'Use the fixed password "ipsilon" for any user')
83         )
84
85     @property
86     def help_text(self):
87         return self.get_config_value('help text')
88
89     @property
90     def username_text(self):
91         return self.get_config_value('username text')
92
93     @property
94     def password_text(self):
95         return self.get_config_value('password text')
96
97     def get_tree(self, site):
98         self.page = TestAuth(site, self, 'login/testauth')
99         return self.page
100
101
102 class Installer(object):
103
104     def __init__(self, *pargs):
105         self.name = 'testauth'
106         self.ptype = 'login'
107         self.pargs = pargs
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         print self.pargs
118         # Add configuration data to database
119         po = PluginObject(*self.pargs)
120         po.name = 'testauth'
121         po.wipe_data()
122
123         # Update global config to add login plugin
124         po.is_enabled = True
125         po.save_enabled_state()