1 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
3 from ipsilon.util.log import Log
4 from ipsilon.util.plugin import PluginInstaller, PluginLoader
5 from ipsilon.util.plugin import PluginObject
6 from ipsilon.util.config import ConfigHelper
7 from ipsilon.util.page import Page
8 from ipsilon.util.page import admin_protect
9 from ipsilon.rest.common import RestPage
13 class ProviderException(Exception, Log):
15 def __init__(self, message):
16 super(ProviderException, self).__init__(message)
17 self.message = message
20 return repr(self.message)
23 class AuthenticationError(ProviderException):
25 def __init__(self, message, code):
26 super(AuthenticationError, self).__init__(message)
28 self.debug('%s [%s]' % (message, code))
31 class InvalidRequest(ProviderException):
33 def __init__(self, message):
34 super(InvalidRequest, self).__init__(message)
38 class ProviderBase(ConfigHelper, PluginObject):
40 def __init__(self, name, path, *pargs):
41 ConfigHelper.__init__(self)
42 PluginObject.__init__(self, *pargs)
48 def get_tree(self, site):
49 raise NotImplementedError
51 def register(self, root, site):
54 # init pages and admin interfaces
55 self.tree = self.get_tree(site)
56 self.debug('IdP Provider registered: %s' % self.name)
59 self._root.add_subtree(self.name, self.tree)
62 self._root.del_subtree(self.name)
64 def get_providers(self):
68 class ProviderPageBase(Page):
70 def __init__(self, site, config):
71 super(ProviderPageBase, self).__init__(site)
72 self.plugin_name = config.name
75 def GET(self, *args, **kwargs):
76 raise cherrypy.HTTPError(501)
78 def POST(self, *args, **kwargs):
79 raise cherrypy.HTTPError(501)
81 def root(self, *args, **kwargs):
82 method = cherrypy.request.method
84 preop = getattr(self, 'pre_%s' % method, None)
85 if preop and callable(preop):
86 preop(*args, **kwargs)
88 op = getattr(self, method, self.GET)
90 return op(*args, **kwargs)
92 raise cherrypy.HTTPError(405)
94 def debug(self, fact):
95 superfact = '%s: %s' % (self.plugin_name, fact)
96 super(ProviderPageBase, self).debug(superfact)
98 def _audit(self, fact):
99 cherrypy.log('%s: %s' % (self.plugin_name, fact))
102 FACILITY = 'provider_config'
105 class ProviderInstaller(object):
107 self.facility = FACILITY
108 self.ptype = 'provider'
111 def unconfigure(self, opts, changes):
114 def install_args(self, group):
115 raise NotImplementedError
117 def validate_args(self, args):
120 def configure(self, opts, changes):
121 raise NotImplementedError
124 class LoadProviders(Log):
126 def __init__(self, root, site):
127 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
128 plugins.get_plugin_data()
129 site[FACILITY] = plugins
131 available = plugins.available.keys()
132 self.debug('Available providers: %s' % str(available))
134 for item in plugins.available:
135 plugin = plugins.available[item]
136 plugin.register(root, site)
138 for item in plugins.enabled:
139 self.debug('Provider plugin in enabled list: %s' % item)
140 if item not in plugins.available:
142 plugins.available[item].enable()
145 class ProvidersInstall(object):
148 pi = PluginInstaller(ProvidersInstall, FACILITY)
149 self.plugins = pi.get_plugins()
152 class RestProviderBase(RestPage):
154 def __init__(self, site, config):
155 super(RestProviderBase, self).__init__(site)
156 self.plugin_name = config.name
160 def GET(self, *args, **kwargs):
161 raise cherrypy.HTTPError(501)
164 def POST(self, *args, **kwargs):
165 raise cherrypy.HTTPError(501)
168 def DELETE(self, *args, **kwargs):
169 raise cherrypy.HTTPError(501)
172 def PUT(self, *args, **kwargs):
173 raise cherrypy.HTTPError(501)
175 def root(self, *args, **kwargs):
176 method = cherrypy.request.method
178 preop = getattr(self, 'pre_%s' % method, None)
179 if preop and callable(preop):
180 preop(*args, **kwargs)
182 op = getattr(self, method, self.GET)
184 return op(*args, **kwargs)
186 raise cherrypy.HTTPError(405)
188 def debug(self, fact):
189 superfact = '%s: %s' % (self.plugin_name, fact)
190 super(RestProviderBase, self).debug(superfact)
192 def _audit(self, fact):
193 cherrypy.log('%s: %s' % (self.plugin_name, fact))