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.util.page import admin_protect
23 from ipsilon.rest.common import RestPage
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, *pargs):
55 PluginConfig.__init__(self)
56 PluginObject.__init__(self, *pargs)
62 def get_tree(self, site):
63 raise NotImplementedError
65 def register(self, root, site):
68 # init pages and admin interfaces
69 self.tree = self.get_tree(site)
70 self._debug('IdP Provider registered: %s' % self.name)
73 self._root.add_subtree(self.name, self.tree)
76 self._root.del_subtree(self.name)
79 class ProviderPageBase(Page):
81 def __init__(self, site, config):
82 super(ProviderPageBase, self).__init__(site)
83 self.plugin_name = config.name
86 def GET(self, *args, **kwargs):
87 raise cherrypy.HTTPError(501)
89 def POST(self, *args, **kwargs):
90 raise cherrypy.HTTPError(501)
92 def root(self, *args, **kwargs):
93 method = cherrypy.request.method
95 preop = getattr(self, 'pre_%s' % method, None)
96 if preop and callable(preop):
97 preop(*args, **kwargs)
99 op = getattr(self, method, self.GET)
101 return op(*args, **kwargs)
103 raise cherrypy.HTTPError(405)
105 def _debug(self, fact):
106 superfact = '%s: %s' % (self.plugin_name, fact)
107 super(ProviderPageBase, self)._debug(superfact)
109 def _audit(self, fact):
110 cherrypy.log('%s: %s' % (self.plugin_name, fact))
113 FACILITY = 'provider_config'
116 class ProviderInstaller(object):
118 self.facility = FACILITY
119 self.ptype = 'provider'
122 def unconfigure(self, opts):
125 def install_args(self, group):
126 raise NotImplementedError
128 def configure(self, opts):
129 raise NotImplementedError
132 class LoadProviders(Log):
134 def __init__(self, root, site):
135 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
136 plugins.get_plugin_data()
137 site[FACILITY] = plugins
139 available = plugins.available.keys()
140 self._debug('Available providers: %s' % str(available))
142 for item in plugins.available:
143 plugin = plugins.available[item]
144 plugin.register(root, site)
146 for item in plugins.enabled:
147 self._debug('Provider plugin in enabled list: %s' % item)
148 if item not in plugins.available:
150 plugins.available[item].enable()
153 class ProvidersInstall(object):
156 pi = PluginInstaller(ProvidersInstall, FACILITY)
157 self.plugins = pi.get_plugins()
160 class RestProviderBase(RestPage):
162 def __init__(self, site, config):
163 super(RestProviderBase, self).__init__(site)
164 self.plugin_name = config.name
168 def GET(self, *args, **kwargs):
169 raise cherrypy.HTTPError(501)
172 def POST(self, *args, **kwargs):
173 raise cherrypy.HTTPError(501)
176 def DELETE(self, *args, **kwargs):
177 raise cherrypy.HTTPError(501)
180 def PUT(self, *args, **kwargs):
181 raise cherrypy.HTTPError(501)
183 def root(self, *args, **kwargs):
184 method = cherrypy.request.method
186 preop = getattr(self, 'pre_%s' % method, None)
187 if preop and callable(preop):
188 preop(*args, **kwargs)
190 op = getattr(self, method, self.GET)
192 return op(*args, **kwargs)
194 raise cherrypy.HTTPError(405)
196 def _debug(self, fact):
197 superfact = '%s: %s' % (self.plugin_name, fact)
198 super(RestProviderBase, self)._debug(superfact)
200 def _audit(self, fact):
201 cherrypy.log('%s: %s' % (self.plugin_name, fact))