289a493277192a6013ef228948dec5f1c490d6d4
[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 LoginPageBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
23 import cherrypy
24
25
26 class TestAuth(LoginPageBase):
27
28     def GET(self, *args, **kwargs):
29         context = self.create_tmpl_context()
30         # pylint: disable=star-args
31         return self._template('login/pam.html', **context)
32
33     def POST(self, *args, **kwargs):
34         username = kwargs.get("login_name")
35         password = kwargs.get("login_password")
36         error = None
37
38         if username and password:
39             if password == 'ipsilon':
40                 cherrypy.log("User %s successfully authenticated." % username)
41                 return self.lm.auth_successful(username)
42             else:
43                 cherrypy.log("User %s failed authentication." % username)
44                 error = "Authentication failed"
45         else:
46             error = "Username or password is missing"
47             cherrypy.log.error("Error: " + error)
48
49         context = self.create_tmpl_context(
50             username=username,
51             error=error,
52             error_password=not password,
53             error_username=not username
54         )
55         # pylint: disable=star-args
56         return self._template('login/pam.html', **context)
57
58     def root(self, *args, **kwargs):
59         op = getattr(self, cherrypy.request.method, self.GET)
60         if callable(op):
61             return op(*args, **kwargs)
62
63     def create_tmpl_context(self, **kwargs):
64         next_url = None
65         if self.lm.next_login is not None:
66             next_url = self.lm.next_login.path
67
68         context = {
69             "title": 'TEST Login',
70             "action": '%s/login/testauth' % self.basepath,
71             "username_text": self.lm.username_text,
72             "password_text": self.lm.password_text,
73             "description": self.lm.help_text,
74             "next_url": next_url,
75         }
76         context.update(kwargs)
77         return context
78
79
80 class LoginManager(LoginManagerBase):
81
82     def __init__(self, *args, **kwargs):
83         super(LoginManager, self).__init__(*args, **kwargs)
84         self.name = 'testauth'
85         self.path = 'testauth'
86         self.page = None
87         self.description = """
88 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
89         self._options = {
90             'help text': [
91                 """ The text shown to guide the user at login time. """,
92                 'string',
93                 'Insert your Username and Password and then submit.'
94             ],
95             'username text': [
96                 """ The text shown to ask for the username in the form. """,
97                 'string',
98                 'Username'
99             ],
100             'password text': [
101                 """ The text shown to ask for the password in the form. """,
102                 'string',
103                 'Password'
104             ],
105         }
106
107     @property
108     def help_text(self):
109         return self.get_config_value('help text')
110
111     @property
112     def username_text(self):
113         return self.get_config_value('username text')
114
115     @property
116     def password_text(self):
117         return self.get_config_value('password text')
118
119     def get_tree(self, site):
120         self.page = TestAuth(site, self)
121         return self.page
122
123
124 class Installer(object):
125
126     def __init__(self):
127         self.name = 'testauth'
128         self.ptype = 'login'
129
130     def install_args(self, group):
131         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
132                            help='Configure PAM authentication')
133
134     def configure(self, opts):
135         if opts['testauth'] != 'yes':
136             return
137
138         # Add configuration data to database
139         po = PluginObject()
140         po.name = 'testauth'
141         po.wipe_data()
142
143         # Update global config to add login plugin
144         po = PluginObject()
145         po.name = 'global'
146         globalconf = po.get_plugin_config(FACILITY)
147         if 'order' in globalconf:
148             order = globalconf['order'].split(',')
149         else:
150             order = []
151         order.append('testauth')
152         globalconf['order'] = ','.join(order)
153         po.set_config(globalconf)
154         po.save_plugin_config(FACILITY)