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
22 from ipsilon.rest.common import RestPage
26 class ProviderException(Exception, Log):
28 def __init__(self, message):
29 super(ProviderException, self).__init__(message)
30 self.message = message
33 return repr(self.message)
36 class AuthenticationError(ProviderException):
38 def __init__(self, message, code):
39 super(AuthenticationError, self).__init__(message)
41 self._debug('%s [%s]' % (message, code))
44 class InvalidRequest(ProviderException):
46 def __init__(self, message):
47 super(InvalidRequest, self).__init__(message)
51 class ProviderBase(PluginConfig, PluginObject):
53 def __init__(self, name, path, *pargs):
54 PluginConfig.__init__(self)
55 PluginObject.__init__(self, *pargs)
61 def get_tree(self, site):
62 raise NotImplementedError
64 def register(self, root, site):
67 # init pages and admin interfaces
68 self.tree = self.get_tree(site)
69 self._debug('IdP Provider registered: %s' % self.name)
72 self._root.add_subtree(self.name, self.tree)
75 self._root.del_subtree(self.name)
78 class ProviderPageBase(Page):
80 def __init__(self, site, config):
81 super(ProviderPageBase, self).__init__(site)
82 self.plugin_name = config.name
85 def GET(self, *args, **kwargs):
86 raise cherrypy.HTTPError(501)
88 def POST(self, *args, **kwargs):
89 raise cherrypy.HTTPError(501)
91 def root(self, *args, **kwargs):
92 method = cherrypy.request.method
94 preop = getattr(self, 'pre_%s' % method, None)
95 if preop and callable(preop):
96 preop(*args, **kwargs)
98 op = getattr(self, method, self.GET)
100 return op(*args, **kwargs)
102 raise cherrypy.HTTPError(405)
104 def _debug(self, fact):
105 superfact = '%s: %s' % (self.plugin_name, fact)
106 super(ProviderPageBase, self)._debug(superfact)
108 def _audit(self, fact):
109 cherrypy.log('%s: %s' % (self.plugin_name, fact))
112 FACILITY = 'provider_config'
115 class ProviderInstaller(object):
117 self.facility = FACILITY
118 self.ptype = 'provider'
121 def unconfigure(self, opts):
124 def install_args(self, group):
125 raise NotImplementedError
127 def configure(self, opts):
128 raise NotImplementedError
131 class LoadProviders(Log):
133 def __init__(self, root, site):
134 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
135 plugins.get_plugin_data()
136 site[FACILITY] = plugins
138 available = plugins.available.keys()
139 self._debug('Available providers: %s' % str(available))
141 for item in plugins.available:
142 plugin = plugins.available[item]
143 plugin.register(root, site)
145 for item in plugins.enabled:
146 self._debug('Provider plugin in enabled list: %s' % item)
147 if item not in plugins.available:
149 plugins.available[item].enable()
152 class ProvidersInstall(object):
155 pi = PluginInstaller(ProvidersInstall, FACILITY)
156 self.plugins = pi.get_plugins()
159 class RestProviderBase(RestPage):
161 def __init__(self, site, config):
162 super(RestProviderBase, self).__init__(site)
163 self.plugin_name = config.name
166 def GET(self, *args, **kwargs):
167 raise cherrypy.HTTPError(501)
169 def POST(self, *args, **kwargs):
170 raise cherrypy.HTTPError(501)
172 def DELETE(self, *args, **kwargs):
173 raise cherrypy.HTTPError(501)
175 def PUT(self, *args, **kwargs):
176 raise cherrypy.HTTPError(501)
178 def root(self, *args, **kwargs):
179 method = cherrypy.request.method
181 preop = getattr(self, 'pre_%s' % method, None)
182 if preop and callable(preop):
183 preop(*args, **kwargs)
185 op = getattr(self, method, self.GET)
187 return op(*args, **kwargs)
189 raise cherrypy.HTTPError(405)
191 def _debug(self, fact):
192 superfact = '%s: %s' % (self.plugin_name, fact)
193 super(RestProviderBase, self)._debug(superfact)
195 def _audit(self, fact):
196 cherrypy.log('%s: %s' % (self.plugin_name, fact))