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
23 from ipsilon.util.plugin import PluginInstaller
27 class LoginManagerBase(PluginObject):
30 super(LoginManagerBase, self).__init__()
32 self.next_login = None
34 def redirect_to_path(self, path):
35 base = cherrypy.config.get('base.mount', "")
36 raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path))
38 def auth_successful(self, username, userdata=None):
39 # save ref before calling UserSession login() as it
40 # may regenerate the session
41 session = UserSession()
42 ref = session.get_data('login', 'Return')
44 ref = cherrypy.config.get('base.mount', "") + '/'
46 session.login(username)
48 # Save additional data provided by the login manager
51 session.save_data('user', key, userdata[key])
53 raise cherrypy.HTTPRedirect(ref)
55 def auth_failed(self):
56 # try with next module
58 return self.redirect_to_path(self.next_login.path)
60 # return to the caller if any
61 session = UserSession()
62 ref = session.get_data('login', 'Return')
64 # otherwise destroy session and return error
67 raise cherrypy.HTTPError(401)
69 raise cherrypy.HTTPRedirect(ref)
72 class LoginPageBase(Page):
74 def __init__(self, site, mgr):
75 super(LoginPageBase, self).__init__(site)
78 def root(self, *args, **kwargs):
79 raise cherrypy.HTTPError(500)
82 FACILITY = 'login_config'
87 def __init__(self, *args, **kwargs):
88 super(Login, self).__init__(*args, **kwargs)
89 self.first_login = None
91 loader = PluginLoader(Login, FACILITY, 'LoginManager')
92 self._site[FACILITY] = loader.get_plugin_data()
93 plugins = self._site[FACILITY]
95 available = plugins['available'].keys()
96 self._debug('Available login managers: %s' % str(available))
99 for item in plugins['whitelist']:
100 self._debug('Login plugin in whitelist: %s' % item)
101 if item not in plugins['available']:
103 self._debug('Login plugin enabled: %s' % item)
104 plugins['enabled'].append(item)
105 obj = plugins['available'][item]
107 prev_obj.next_login = obj
109 self.first_login = obj
111 if item in plugins['config']:
112 obj.set_config(plugins['config'][item])
113 self.__dict__[item] = obj.get_tree(self._site)
115 def root(self, *args, **kwargs):
117 raise cherrypy.HTTPRedirect('%s/login/%s' %
119 self.first_login.path))
120 return self._template('login/index.html', title='Login')
125 def root(self, *args, **kwargs):
126 UserSession().logout(self.user)
127 return self._template('logout.html', title='Logout')
130 class LoginMgrsInstall(object):
133 pi = PluginInstaller(LoginMgrsInstall)
134 self.plugins = pi.get_plugins()