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 PluginInstaller, PluginLoader
22 from ipsilon.util.plugin import PluginObject, PluginConfig
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(PluginConfig, PluginObject):
54 def __init__(self, name, path):
55 PluginConfig.__init__(self)
56 PluginObject.__init__(self)
60 self.is_enabled = False
63 # this one does nothing
64 # derived classes can override with custom behavior
67 def get_tree(self, site):
68 raise NotImplementedError
70 def register(self, site):
76 plugins = site[FACILITY]
77 if self.name in plugins['config']:
78 self.import_config(plugins['config'][self.name])
80 # init pages and admin interfaces
81 self.tree = self.get_tree(site)
83 self._debug('IdP Provider registered: %s' % self.name)
85 if self.get_config_value('enabled') is True:
89 def _enable(self, site):
90 root = site[FACILITY]['root']
91 root.add_subtree(self.name, self.tree)
92 self._debug('IdP Provider enabled: %s' % self.name)
93 self.is_enabled = True
96 def enable(self, site):
101 self.set_config_value('enabled', True)
102 self.save_plugin_config(FACILITY)
104 def disable(self, site):
105 if not self.is_enabled:
108 # remove self to the root
109 root = site[FACILITY]['root']
110 root.del_subtree(self.name)
112 self.is_enabled = False
113 self.set_config_value('enabled', False)
114 self.save_plugin_config(FACILITY)
115 self._debug('IdP Provider disabled: %s' % self.name)
118 class ProviderPageBase(Page):
120 def __init__(self, site, config):
121 super(ProviderPageBase, self).__init__(site)
122 self.plugin_name = config.name
125 def GET(self, *args, **kwargs):
126 raise cherrypy.HTTPError(501)
128 def POST(self, *args, **kwargs):
129 raise cherrypy.HTTPError(501)
131 def root(self, *args, **kwargs):
132 method = cherrypy.request.method
134 preop = getattr(self, 'pre_%s' % method, None)
135 if preop and callable(preop):
136 preop(*args, **kwargs)
138 op = getattr(self, method, self.GET)
140 return op(*args, **kwargs)
142 raise cherrypy.HTTPError(405)
144 def _debug(self, fact):
145 superfact = '%s: %s' % (self.plugin_name, fact)
146 super(ProviderPageBase, self)._debug(superfact)
148 def _audit(self, fact):
149 cherrypy.log('%s: %s' % (self.plugin_name, fact))
152 FACILITY = 'provider_config'
155 class LoadProviders(Log):
157 def __init__(self, root, site):
158 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
159 site[FACILITY] = loader.get_plugin_data()
160 providers = site[FACILITY]
162 available = providers['available'].keys()
163 self._debug('Available providers: %s' % str(available))
165 providers['root'] = root
166 for item in providers['available']:
167 plugin = providers['available'][item]
168 plugin.register(site)
171 class ProvidersInstall(object):
174 pi = PluginInstaller(ProvidersInstall)
175 self.plugins = pi.get_plugins()