Fix file permissions and remove shebang's
[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 from ipsilon.util.plugin import PluginObject
20 from ipsilon.util import config as pconfig
21 import cherrypy
22
23
24 class TestAuth(LoginFormBase):
25
26     def POST(self, *args, **kwargs):
27         username = kwargs.get("login_name")
28         password = kwargs.get("login_password")
29         error = None
30
31         if username and password:
32             if password == 'ipsilon':
33                 cherrypy.log("User %s successfully authenticated." % username)
34                 testdata = {'fullname': 'Test User %s' % username}
35                 return self.lm.auth_successful(self.trans,
36                                                username, 'password', testdata)
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.new_config(
65             self.name,
66             pconfig.String(
67                 'username text',
68                 'Text used to ask for the username at login time.',
69                 'Username'),
70             pconfig.String(
71                 'password text',
72                 'Text used to ask for the password at login time.',
73                 'Password'),
74             pconfig.String(
75                 'help text',
76                 'Text used to guide the user at login time.',
77                 'DISABLE IN PRODUCTION, USE ONLY FOR TEST ' +
78                 'Use any username they are all valid, "admin" gives ' +
79                 'administrative powers. ' +
80                 'Use the fixed password "ipsilon" for any user')
81         )
82
83     @property
84     def help_text(self):
85         return self.get_config_value('help text')
86
87     @property
88     def username_text(self):
89         return self.get_config_value('username text')
90
91     @property
92     def password_text(self):
93         return self.get_config_value('password text')
94
95     def get_tree(self, site):
96         self.page = TestAuth(site, self, 'login/testauth')
97         return self.page
98
99
100 class Installer(object):
101
102     def __init__(self, *pargs):
103         self.name = 'testauth'
104         self.ptype = 'login'
105         self.pargs = pargs
106
107     def install_args(self, group):
108         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
109                            help='Configure PAM authentication')
110
111     def configure(self, opts):
112         if opts['testauth'] != 'yes':
113             return
114
115         print self.pargs
116         # Add configuration data to database
117         po = PluginObject(*self.pargs)
118         po.name = 'testauth'
119         po.wipe_data()
120
121         # Update global config to add login plugin
122         po.is_enabled = True
123         po.save_enabled_state()