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
21 from ipsilon.util.config import ConfigHelper
22 from ipsilon.util.page import Page
23 from ipsilon.util.page import admin_protect
24 from ipsilon.rest.common import RestPage
28 class ProviderException(Exception, Log):
30 def __init__(self, message):
31 super(ProviderException, self).__init__(message)
32 self.message = message
35 return repr(self.message)
38 class AuthenticationError(ProviderException):
40 def __init__(self, message, code):
41 super(AuthenticationError, self).__init__(message)
43 self.debug('%s [%s]' % (message, code))
46 class InvalidRequest(ProviderException):
48 def __init__(self, message):
49 super(InvalidRequest, self).__init__(message)
53 class ProviderBase(ConfigHelper, PluginObject):
55 def __init__(self, name, path, *pargs):
56 ConfigHelper.__init__(self)
57 PluginObject.__init__(self, *pargs)
63 def get_tree(self, site):
64 raise NotImplementedError
66 def register(self, root, site):
69 # init pages and admin interfaces
70 self.tree = self.get_tree(site)
71 self.debug('IdP Provider registered: %s' % self.name)
74 self._root.add_subtree(self.name, self.tree)
77 self._root.del_subtree(self.name)
80 class ProviderPageBase(Page):
82 def __init__(self, site, config):
83 super(ProviderPageBase, self).__init__(site)
84 self.plugin_name = config.name
87 def GET(self, *args, **kwargs):
88 raise cherrypy.HTTPError(501)
90 def POST(self, *args, **kwargs):
91 raise cherrypy.HTTPError(501)
93 def root(self, *args, **kwargs):
94 method = cherrypy.request.method
96 preop = getattr(self, 'pre_%s' % method, None)
97 if preop and callable(preop):
98 preop(*args, **kwargs)
100 op = getattr(self, method, self.GET)
102 return op(*args, **kwargs)
104 raise cherrypy.HTTPError(405)
106 def debug(self, fact):
107 superfact = '%s: %s' % (self.plugin_name, fact)
108 super(ProviderPageBase, self).debug(superfact)
110 def _audit(self, fact):
111 cherrypy.log('%s: %s' % (self.plugin_name, fact))
114 FACILITY = 'provider_config'
117 class ProviderInstaller(object):
119 self.facility = FACILITY
120 self.ptype = 'provider'
123 def unconfigure(self, opts):
126 def install_args(self, group):
127 raise NotImplementedError
129 def validate_args(self, args):
132 def configure(self, opts):
133 raise NotImplementedError
136 class LoadProviders(Log):
138 def __init__(self, root, site):
139 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
140 plugins.get_plugin_data()
141 site[FACILITY] = plugins
143 available = plugins.available.keys()
144 self.debug('Available providers: %s' % str(available))
146 for item in plugins.available:
147 plugin = plugins.available[item]
148 plugin.register(root, site)
150 for item in plugins.enabled:
151 self.debug('Provider plugin in enabled list: %s' % item)
152 if item not in plugins.available:
154 plugins.available[item].enable()
157 class ProvidersInstall(object):
160 pi = PluginInstaller(ProvidersInstall, FACILITY)
161 self.plugins = pi.get_plugins()
164 class RestProviderBase(RestPage):
166 def __init__(self, site, config):
167 super(RestProviderBase, self).__init__(site)
168 self.plugin_name = config.name
172 def GET(self, *args, **kwargs):
173 raise cherrypy.HTTPError(501)
176 def POST(self, *args, **kwargs):
177 raise cherrypy.HTTPError(501)
180 def DELETE(self, *args, **kwargs):
181 raise cherrypy.HTTPError(501)
184 def PUT(self, *args, **kwargs):
185 raise cherrypy.HTTPError(501)
187 def root(self, *args, **kwargs):
188 method = cherrypy.request.method
190 preop = getattr(self, 'pre_%s' % method, None)
191 if preop and callable(preop):
192 preop(*args, **kwargs)
194 op = getattr(self, method, self.GET)
196 return op(*args, **kwargs)
198 raise cherrypy.HTTPError(405)
200 def debug(self, fact):
201 superfact = '%s: %s' % (self.plugin_name, fact)
202 super(RestProviderBase, self).debug(superfact)
204 def _audit(self, fact):
205 cherrypy.log('%s: %s' % (self.plugin_name, fact))