Add attribute mapping for user information
[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 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._options = {
67             'help text': [
68                 """ The text shown to guide the user at login time. """,
69                 'string',
70                 'Insert your Username and Password and then submit.'
71             ],
72             'username text': [
73                 """ The text shown to ask for the username in the form. """,
74                 'string',
75                 'Username'
76             ],
77             'password text': [
78                 """ The text shown to ask for the password in the form. """,
79                 'string',
80                 'Password'
81             ],
82         }
83
84     @property
85     def help_text(self):
86         return self.get_config_value('help text')
87
88     @property
89     def username_text(self):
90         return self.get_config_value('username text')
91
92     @property
93     def password_text(self):
94         return self.get_config_value('password text')
95
96     def get_tree(self, site):
97         self.page = TestAuth(site, self, 'login/testauth')
98         return self.page
99
100
101 class Installer(object):
102
103     def __init__(self):
104         self.name = 'testauth'
105         self.ptype = 'login'
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         # Add configuration data to database
116         po = PluginObject()
117         po.name = 'testauth'
118         po.wipe_data()
119
120         # Update global config to add login plugin
121         po = PluginObject()
122         po.name = 'global'
123         globalconf = po.get_plugin_config(FACILITY)
124         if 'order' in globalconf:
125             order = globalconf['order'].split(',')
126         else:
127             order = []
128         order.append('testauth')
129         globalconf['order'] = ','.join(order)
130         po.set_config(globalconf)
131         po.save_plugin_config(FACILITY)