3 # Copyright (C) 2014 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.log import Log
21 from ipsilon.util.plugin import PluginLoader, PluginObject
22 from ipsilon.util.plugin import PluginInstaller
23 from ipsilon.util.page import Page
27 class ProviderException(Exception, Log):
29 def __init__(self, message):
30 super(ProviderException, self).__init__(message)
31 self.message = message
34 return repr(self.message)
37 class AuthenticationError(ProviderException):
39 def __init__(self, message, code):
40 super(AuthenticationError, self).__init__(message)
42 self._debug('%s [%s]' % (message, code))
45 class InvalidRequest(ProviderException):
47 def __init__(self, message):
48 super(InvalidRequest, self).__init__(message)
52 class ProviderBase(PluginObject):
54 def __init__(self, name, path):
55 super(ProviderBase, self).__init__()
61 # this one does nothing
62 # derived classes can override with custom behavior
65 def get_tree(self, site):
66 raise NotImplementedError
68 def register(self, site):
74 plugins = site[FACILITY]
75 if self.name in plugins['config']:
76 self.set_config(plugins['config'][self.name])
78 # init pages and admin interfaces
79 self.tree = self.get_tree(site)
81 self._debug('IdP Provider registered: %s' % self.name)
83 if self.get_config_value('enabled') == '1':
84 # and add self to the root
85 root = site[FACILITY]['root']
86 root.add_subtree(self.name, self.tree)
87 self._debug('IdP Provider enabled: %s' % self.name)
91 if self.get_config_value('enabled') == '1':
95 def enable(self, site):
99 # and add self to the root
100 root = site[FACILITY]['root']
101 root.add_subtree(self.name, self.tree)
103 self.set_config_value('enabled', '1')
104 self.save_plugin_config(FACILITY)
107 self._debug('IdP Provider enabled: %s' % self.name)
109 def disable(self, site):
110 if not self.is_enabled:
113 # remove self to the root
114 root = site[FACILITY]['root']
115 root.del_subtree(self.name)
117 self.set_config_value('enabled', '0')
118 self.save_plugin_config(FACILITY)
119 self._debug('IdP Provider disabled: %s' % self.name)
122 class ProviderPageBase(Page):
124 def __init__(self, site, config):
125 super(ProviderPageBase, self).__init__(site)
126 self.plugin_name = config.name
129 def GET(self, *args, **kwargs):
130 raise cherrypy.HTTPError(501)
132 def POST(self, *args, **kwargs):
133 raise cherrypy.HTTPError(501)
135 def root(self, *args, **kwargs):
136 method = cherrypy.request.method
138 preop = getattr(self, 'pre_%s' % method, None)
139 if preop and callable(preop):
140 preop(*args, **kwargs)
142 op = getattr(self, method, self.GET)
144 return op(*args, **kwargs)
146 raise cherrypy.HTTPError(405)
148 def _debug(self, fact):
149 superfact = '%s: %s' % (self.plugin_name, fact)
150 super(ProviderPageBase, self)._debug(superfact)
152 def _audit(self, fact):
153 cherrypy.log('%s: %s' % (self.plugin_name, fact))
156 FACILITY = 'provider_config'
159 class LoadProviders(Log):
161 def __init__(self, root, site):
162 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
163 site[FACILITY] = loader.get_plugin_data()
164 providers = site[FACILITY]
166 available = providers['available'].keys()
167 self._debug('Available providers: %s' % str(available))
169 providers['root'] = root
170 for item in providers['available']:
171 plugin = providers['available'][item]
172 plugin.register(site)
175 class ProvidersInstall(object):
178 pi = PluginInstaller(ProvidersInstall)
179 self.plugins = pi.get_plugins()