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.plugin import PluginLoader, PluginObject
21 from ipsilon.util.plugin import PluginInstaller
22 from ipsilon.util.page import Page
26 class ProviderException(Exception):
28 def __init__(self, message):
29 super(ProviderException, self).__init__(message)
30 self.message = message
33 return repr(self.message)
35 def _debug(self, fact):
36 if cherrypy.config.get('debug', False):
37 cherrypy.log('%s: %s' % (self.__class__.__name__, fact))
40 class ProviderBase(PluginObject):
42 def __init__(self, name, path):
43 super(ProviderBase, self).__init__()
47 def _debug(self, fact):
48 if cherrypy.config.get('debug', False):
51 def get_tree(self, site):
52 raise NotImplementedError
54 def enable(self, site):
55 plugins = site[FACILITY]
56 if self in plugins['enabled']:
60 if self.name in plugins['config']:
61 self.set_config(plugins['config'][self.name])
63 # and add self to the root
64 root = plugins['root']
65 root.add_subtree(self.name, self.get_tree(site))
67 plugins['enabled'].append(self)
68 self._debug('IdP Provider enabled: %s' % self.name)
70 def disable(self, site):
71 plugins = site[FACILITY]
72 if self not in plugins['enabled']:
75 # remove self to the root
76 root = plugins['root']
77 root.del_subtree(self.name)
79 plugins['enabled'].remove(self)
80 self._debug('IdP Provider disabled: %s' % self.name)
83 class ProviderPageBase(Page):
85 def __init__(self, site, config):
86 super(ProviderPageBase, self).__init__(site)
87 self.plugin_name = config.name
90 def GET(self, *args, **kwargs):
91 raise cherrypy.HTTPError(501)
93 def POST(self, *args, **kwargs):
94 raise cherrypy.HTTPError(501)
96 def root(self, *args, **kwargs):
97 op = getattr(self, cherrypy.request.method, self.GET)
99 return op(*args, **kwargs)
101 raise cherrypy.HTTPError(405)
103 def _debug(self, fact):
104 superfact = '%s: %s' % (self.plugin_name, fact)
105 super(ProviderPageBase, self)._debug(superfact)
107 def _audit(self, fact):
108 cherrypy.log('%s: %s' % (self.plugin_name, fact))
111 FACILITY = 'provider_config'
114 class LoadProviders(object):
116 def __init__(self, root, site):
117 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
118 site[FACILITY] = loader.get_plugin_data()
119 providers = site[FACILITY]
121 available = providers['available'].keys()
122 self._debug('Available providers: %s' % str(available))
124 providers['root'] = root
125 for item in providers['whitelist']:
126 self._debug('IdP Provider in whitelist: %s' % item)
127 if item not in providers['available']:
129 providers['available'][item].enable(site)
131 def _debug(self, fact):
132 if cherrypy.config.get('debug', False):
136 class ProvidersInstall(object):
139 pi = PluginInstaller(ProvidersInstall)
140 self.plugins = pi.get_plugins()