3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
20 from ipsilon.util.page import Page
21 from ipsilon.util.user import UserSession
22 from ipsilon.util.plugin import PluginLoader, PluginObject
26 class LoginManagerBase(PluginObject):
29 super(LoginManagerBase, self).__init__()
31 self.next_login = None
33 def redirect_to_path(self, path):
34 base = cherrypy.config.get('base.mount', "")
35 raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path))
37 def auth_successful(self, username):
38 # save ref before calling UserSession login() as it
39 # may regenerate the session
40 ref = cherrypy.config.get('base.mount', "") + '/'
41 if 'referral' in cherrypy.session:
42 ref = cherrypy.session['referral']
44 UserSession().login(username)
45 raise cherrypy.HTTPRedirect(ref)
47 def auth_failed(self):
48 # Just make sure we destroy the session
49 UserSession().logout(None)
52 return self.redirect_to_path(self.next_login.path)
54 ref = cherrypy.config.get('base.mount', "") + '/unauthorized'
55 raise cherrypy.HTTPRedirect(ref)
58 class LoginPageBase(Page):
60 def __init__(self, site, mgr):
61 super(LoginPageBase, self).__init__(site)
64 def root(self, *args, **kwargs):
65 raise cherrypy.HTTPError(500)
68 FACILITY = 'login_config'
73 def __init__(self, *args, **kwargs):
74 super(Login, self).__init__(*args, **kwargs)
75 self.first_login = None
77 loader = PluginLoader(Login, FACILITY, 'LoginManager')
78 self._site[FACILITY] = loader.get_plugin_data()
79 plugins = self._site[FACILITY]
81 available = plugins['available'].keys()
82 self._log('Available login managers: %s' % str(available))
85 for item in plugins['whitelist']:
86 self._log('Login plugin in whitelist: %s' % item)
87 if item not in plugins['available']:
89 self._log('Login plugin enabled: %s' % item)
90 plugins['enabled'].append(item)
91 obj = plugins['available'][item]
93 prev_obj.next_login = obj
95 self.first_login = obj
97 if item in plugins['config']:
98 obj.set_config(plugins['config'][item])
99 self.__dict__[item] = obj.get_tree(self._site)
101 def _log(self, fact):
102 if cherrypy.config.get('debug', False):
105 def root(self, *args, **kwargs):
107 raise cherrypy.HTTPRedirect('%s/login/%s' %
109 self.first_login.path))
110 return self._template('login/index.html', title='Login')
115 def root(self, *args, **kwargs):
116 UserSession().logout(self.user)
117 return self._template('logout.html', title='Logout')