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 PluginLoader, PluginObject
22 from ipsilon.util.plugin import PluginInstaller
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(PluginObject):
54 def __init__(self, name, path):
55 super(ProviderBase, self).__init__()
59 self.is_enabled = False
62 # this one does nothing
63 # derived classes can override with custom behavior
66 def get_tree(self, site):
67 raise NotImplementedError
69 def register(self, site):
75 plugins = site[FACILITY]
76 if self.name in plugins['config']:
77 self.set_config(plugins['config'][self.name])
79 # init pages and admin interfaces
80 self.tree = self.get_tree(site)
82 self._debug('IdP Provider registered: %s' % self.name)
84 if self.get_config_value('enabled') == '1':
88 def _enable(self, site):
89 root = site[FACILITY]['root']
90 root.add_subtree(self.name, self.tree)
91 self._debug('IdP Provider enabled: %s' % self.name)
92 self.is_enabled = True
95 def enable(self, site):
100 self.set_config_value('enabled', '1')
101 self.save_plugin_config(FACILITY)
103 def disable(self, site):
104 if not self.is_enabled:
107 # remove self to the root
108 root = site[FACILITY]['root']
109 root.del_subtree(self.name)
111 self.is_enabled = False
112 self.set_config_value('enabled', '0')
113 self.save_plugin_config(FACILITY)
114 self._debug('IdP Provider disabled: %s' % self.name)
117 class ProviderPageBase(Page):
119 def __init__(self, site, config):
120 super(ProviderPageBase, self).__init__(site)
121 self.plugin_name = config.name
124 def GET(self, *args, **kwargs):
125 raise cherrypy.HTTPError(501)
127 def POST(self, *args, **kwargs):
128 raise cherrypy.HTTPError(501)
130 def root(self, *args, **kwargs):
131 method = cherrypy.request.method
133 preop = getattr(self, 'pre_%s' % method, None)
134 if preop and callable(preop):
135 preop(*args, **kwargs)
137 op = getattr(self, method, self.GET)
139 return op(*args, **kwargs)
141 raise cherrypy.HTTPError(405)
143 def _debug(self, fact):
144 superfact = '%s: %s' % (self.plugin_name, fact)
145 super(ProviderPageBase, self)._debug(superfact)
147 def _audit(self, fact):
148 cherrypy.log('%s: %s' % (self.plugin_name, fact))
151 FACILITY = 'provider_config'
154 class LoadProviders(Log):
156 def __init__(self, root, site):
157 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
158 site[FACILITY] = loader.get_plugin_data()
159 providers = site[FACILITY]
161 available = providers['available'].keys()
162 self._debug('Available providers: %s' % str(available))
164 providers['root'] = root
165 for item in providers['available']:
166 plugin = providers['available'][item]
167 plugin.register(site)
170 class ProvidersInstall(object):
173 pi = PluginInstaller(ProvidersInstall)
174 self.plugins = pi.get_plugins()