1 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
3 # see file 'COPYING' for use and warranty information
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from ipsilon.util.log import Log
19 from ipsilon.util.plugin import PluginInstaller, PluginLoader
20 from ipsilon.util.plugin import PluginObject, PluginConfig
21 from ipsilon.util.page import Page
25 class ProviderException(Exception, Log):
27 def __init__(self, message):
28 super(ProviderException, self).__init__(message)
29 self.message = message
32 return repr(self.message)
35 class AuthenticationError(ProviderException):
37 def __init__(self, message, code):
38 super(AuthenticationError, self).__init__(message)
40 self._debug('%s [%s]' % (message, code))
43 class InvalidRequest(ProviderException):
45 def __init__(self, message):
46 super(InvalidRequest, self).__init__(message)
50 class ProviderBase(PluginConfig, PluginObject):
52 def __init__(self, name, path, *pargs):
53 PluginConfig.__init__(self)
54 PluginObject.__init__(self, *pargs)
60 def get_tree(self, site):
61 raise NotImplementedError
63 def register(self, root, site):
66 # init pages and admin interfaces
67 self.tree = self.get_tree(site)
68 self._debug('IdP Provider registered: %s' % self.name)
71 self._root.add_subtree(self.name, self.tree)
74 self._root.del_subtree(self.name)
77 class ProviderPageBase(Page):
79 def __init__(self, site, config):
80 super(ProviderPageBase, self).__init__(site)
81 self.plugin_name = config.name
84 def GET(self, *args, **kwargs):
85 raise cherrypy.HTTPError(501)
87 def POST(self, *args, **kwargs):
88 raise cherrypy.HTTPError(501)
90 def root(self, *args, **kwargs):
91 method = cherrypy.request.method
93 preop = getattr(self, 'pre_%s' % method, None)
94 if preop and callable(preop):
95 preop(*args, **kwargs)
97 op = getattr(self, 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 ProviderInstaller(object):
116 self.facility = FACILITY
117 self.ptype = 'provider'
120 def unconfigure(self, opts):
123 def install_args(self, group):
124 raise NotImplementedError
126 def configure(self, opts):
127 raise NotImplementedError
130 class LoadProviders(Log):
132 def __init__(self, root, site):
133 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
134 plugins.get_plugin_data()
135 site[FACILITY] = plugins
137 available = plugins.available.keys()
138 self._debug('Available providers: %s' % str(available))
140 for item in plugins.available:
141 plugin = plugins.available[item]
142 plugin.register(root, site)
144 for item in plugins.enabled:
145 self._debug('Provider plugin in enabled list: %s' % item)
146 if item not in plugins.available:
148 plugins.available[item].enable()
151 class ProvidersInstall(object):
154 pi = PluginInstaller(ProvidersInstall, FACILITY)
155 self.plugins = pi.get_plugins()