b7000b27b51d99b48a062195f823582ea34b5a59
[cascardo/ipsilon.git] / ipsilon / login / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  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.util.page import Page
21 from ipsilon.util.user import UserSession
22 from ipsilon.util.plugin import PluginLoader, PluginObject
23 import cherrypy
24
25
26 class LoginManagerBase(PluginObject):
27
28     def __init__(self):
29         super(LoginManagerBase, self).__init__()
30         self.path = '/'
31         self.next_login = None
32
33     def redirect_to_path(self, path):
34         base = cherrypy.config.get('base.mount', "")
35         raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path))
36
37     def auth_successful(self, username, userdata=None):
38         # save ref before calling UserSession login() as it
39         # may regenerate the session
40         session = UserSession()
41         ref = session.get_data('login', 'Return')
42         if not ref:
43             ref = cherrypy.config.get('base.mount', "") + '/'
44
45         session.login(username)
46
47         # Save additional data provided by the login manager
48         if userdata:
49             for key in userdata:
50                 session.save_data('user', key, userdata[key])
51
52         raise cherrypy.HTTPRedirect(ref)
53
54     def auth_failed(self):
55         # try with next module
56         if self.next_login:
57             return self.redirect_to_path(self.next_login.path)
58
59         # return to the caller if any
60         session = UserSession()
61         ref = session.get_data('login', 'Return')
62
63         # otherwise destroy session and return error
64         if not ref:
65             session.logout(None)
66             raise cherrypy.HTTPError(401)
67
68         raise cherrypy.HTTPRedirect(ref)
69
70
71 class LoginPageBase(Page):
72
73     def __init__(self, site, mgr):
74         super(LoginPageBase, self).__init__(site)
75         self.lm = mgr
76
77     def root(self, *args, **kwargs):
78         raise cherrypy.HTTPError(500)
79
80
81 FACILITY = 'login_config'
82
83
84 class Login(Page):
85
86     def __init__(self, *args, **kwargs):
87         super(Login, self).__init__(*args, **kwargs)
88         self.first_login = None
89
90         loader = PluginLoader(Login, FACILITY, 'LoginManager')
91         self._site[FACILITY] = loader.get_plugin_data()
92         plugins = self._site[FACILITY]
93
94         available = plugins['available'].keys()
95         self._debug('Available login managers: %s' % str(available))
96
97         prev_obj = None
98         for item in plugins['whitelist']:
99             self._debug('Login plugin in whitelist: %s' % item)
100             if item not in plugins['available']:
101                 continue
102             self._debug('Login plugin enabled: %s' % item)
103             plugins['enabled'].append(item)
104             obj = plugins['available'][item]
105             if prev_obj:
106                 prev_obj.next_login = obj
107             else:
108                 self.first_login = obj
109             prev_obj = obj
110             if item in plugins['config']:
111                 obj.set_config(plugins['config'][item])
112             self.__dict__[item] = obj.get_tree(self._site)
113
114     def root(self, *args, **kwargs):
115         if self.first_login:
116             raise cherrypy.HTTPRedirect('%s/login/%s' %
117                                         (self.basepath,
118                                          self.first_login.path))
119         return self._template('login/index.html', title='Login')
120
121
122 class Logout(Page):
123
124     def root(self, *args, **kwargs):
125         UserSession().logout(self.user)
126         return self._template('logout.html', title='Logout')