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)
65 class ProviderPageBase(Page):
67 def __init__(self, site, config):
68 super(ProviderPageBase, self).__init__(site)
69 self.plugin_name = config.name
72 def GET(self, *args, **kwargs):
73 raise cherrypy.HTTPError(501)
75 def POST(self, *args, **kwargs):
76 raise cherrypy.HTTPError(501)
78 def root(self, *args, **kwargs):
79 method = cherrypy.request.method
81 preop = getattr(self, 'pre_%s' % method, None)
82 if preop and callable(preop):
83 preop(*args, **kwargs)
85 op = getattr(self, method, self.GET)
87 return op(*args, **kwargs)
89 raise cherrypy.HTTPError(405)
91 def debug(self, fact):
92 superfact = '%s: %s' % (self.plugin_name, fact)
93 super(ProviderPageBase, self).debug(superfact)
95 def _audit(self, fact):
96 cherrypy.log('%s: %s' % (self.plugin_name, fact))
99 FACILITY = 'provider_config'
102 class ProviderInstaller(object):
104 self.facility = FACILITY
105 self.ptype = 'provider'
108 def unconfigure(self, opts):
111 def install_args(self, group):
112 raise NotImplementedError
114 def validate_args(self, args):
117 def configure(self, opts):
118 raise NotImplementedError
121 class LoadProviders(Log):
123 def __init__(self, root, site):
124 plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
125 plugins.get_plugin_data()
126 site[FACILITY] = plugins
128 available = plugins.available.keys()
129 self.debug('Available providers: %s' % str(available))
131 for item in plugins.available:
132 plugin = plugins.available[item]
133 plugin.register(root, site)
135 for item in plugins.enabled:
136 self.debug('Provider plugin in enabled list: %s' % item)
137 if item not in plugins.available:
139 plugins.available[item].enable()
142 class ProvidersInstall(object):
145 pi = PluginInstaller(ProvidersInstall, FACILITY)
146 self.plugins = pi.get_plugins()
149 class RestProviderBase(RestPage):
151 def __init__(self, site, config):
152 super(RestProviderBase, self).__init__(site)
153 self.plugin_name = config.name
157 def GET(self, *args, **kwargs):
158 raise cherrypy.HTTPError(501)
161 def POST(self, *args, **kwargs):
162 raise cherrypy.HTTPError(501)
165 def DELETE(self, *args, **kwargs):
166 raise cherrypy.HTTPError(501)
169 def PUT(self, *args, **kwargs):
170 raise cherrypy.HTTPError(501)
172 def root(self, *args, **kwargs):
173 method = cherrypy.request.method
175 preop = getattr(self, 'pre_%s' % method, None)
176 if preop and callable(preop):
177 preop(*args, **kwargs)
179 op = getattr(self, method, self.GET)
181 return op(*args, **kwargs)
183 raise cherrypy.HTTPError(405)
185 def debug(self, fact):
186 superfact = '%s: %s' % (self.plugin_name, fact)
187 super(RestProviderBase, self).debug(superfact)
189 def _audit(self, fact):
190 cherrypy.log('%s: %s' % (self.plugin_name, fact))