Implement change registration
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
2
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
10 import cherrypy
11
12
13 class ProviderException(Exception, Log):
14
15     def __init__(self, message):
16         super(ProviderException, self).__init__(message)
17         self.message = message
18
19     def __str__(self):
20         return repr(self.message)
21
22
23 class AuthenticationError(ProviderException):
24
25     def __init__(self, message, code):
26         super(AuthenticationError, self).__init__(message)
27         self.code = code
28         self.debug('%s [%s]' % (message, code))
29
30
31 class InvalidRequest(ProviderException):
32
33     def __init__(self, message):
34         super(InvalidRequest, self).__init__(message)
35         self.debug(message)
36
37
38 class ProviderBase(ConfigHelper, PluginObject):
39
40     def __init__(self, name, path, *pargs):
41         ConfigHelper.__init__(self)
42         PluginObject.__init__(self, *pargs)
43         self.name = name
44         self._root = None
45         self.path = path
46         self.tree = None
47
48     def get_tree(self, site):
49         raise NotImplementedError
50
51     def register(self, root, site):
52
53         self._root = root
54         # init pages and admin interfaces
55         self.tree = self.get_tree(site)
56         self.debug('IdP Provider registered: %s' % self.name)
57
58     def on_enable(self):
59         self._root.add_subtree(self.name, self.tree)
60
61     def on_disable(self):
62         self._root.del_subtree(self.name)
63
64
65 class ProviderPageBase(Page):
66
67     def __init__(self, site, config):
68         super(ProviderPageBase, self).__init__(site)
69         self.plugin_name = config.name
70         self.cfg = config
71
72     def GET(self, *args, **kwargs):
73         raise cherrypy.HTTPError(501)
74
75     def POST(self, *args, **kwargs):
76         raise cherrypy.HTTPError(501)
77
78     def root(self, *args, **kwargs):
79         method = cherrypy.request.method
80
81         preop = getattr(self, 'pre_%s' % method, None)
82         if preop and callable(preop):
83             preop(*args, **kwargs)
84
85         op = getattr(self, method, self.GET)
86         if callable(op):
87             return op(*args, **kwargs)
88         else:
89             raise cherrypy.HTTPError(405)
90
91     def debug(self, fact):
92         superfact = '%s: %s' % (self.plugin_name, fact)
93         super(ProviderPageBase, self).debug(superfact)
94
95     def _audit(self, fact):
96         cherrypy.log('%s: %s' % (self.plugin_name, fact))
97
98
99 FACILITY = 'provider_config'
100
101
102 class ProviderInstaller(object):
103     def __init__(self):
104         self.facility = FACILITY
105         self.ptype = 'provider'
106         self.name = None
107
108     def unconfigure(self, opts, changes):
109         return
110
111     def install_args(self, group):
112         raise NotImplementedError
113
114     def validate_args(self, args):
115         return
116
117     def configure(self, opts, changes):
118         raise NotImplementedError
119
120
121 class LoadProviders(Log):
122
123     def __init__(self, root, site):
124         plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
125         plugins.get_plugin_data()
126         site[FACILITY] = plugins
127
128         available = plugins.available.keys()
129         self.debug('Available providers: %s' % str(available))
130
131         for item in plugins.available:
132             plugin = plugins.available[item]
133             plugin.register(root, site)
134
135         for item in plugins.enabled:
136             self.debug('Provider plugin in enabled list: %s' % item)
137             if item not in plugins.available:
138                 continue
139             plugins.available[item].enable()
140
141
142 class ProvidersInstall(object):
143
144     def __init__(self):
145         pi = PluginInstaller(ProvidersInstall, FACILITY)
146         self.plugins = pi.get_plugins()
147
148
149 class RestProviderBase(RestPage):
150
151     def __init__(self, site, config):
152         super(RestProviderBase, self).__init__(site)
153         self.plugin_name = config.name
154         self.cfg = config
155
156     @admin_protect
157     def GET(self, *args, **kwargs):
158         raise cherrypy.HTTPError(501)
159
160     @admin_protect
161     def POST(self, *args, **kwargs):
162         raise cherrypy.HTTPError(501)
163
164     @admin_protect
165     def DELETE(self, *args, **kwargs):
166         raise cherrypy.HTTPError(501)
167
168     @admin_protect
169     def PUT(self, *args, **kwargs):
170         raise cherrypy.HTTPError(501)
171
172     def root(self, *args, **kwargs):
173         method = cherrypy.request.method
174
175         preop = getattr(self, 'pre_%s' % method, None)
176         if preop and callable(preop):
177             preop(*args, **kwargs)
178
179         op = getattr(self, method, self.GET)
180         if callable(op):
181             return op(*args, **kwargs)
182         else:
183             raise cherrypy.HTTPError(405)
184
185     def debug(self, fact):
186         superfact = '%s: %s' % (self.plugin_name, fact)
187         super(RestProviderBase, self).debug(superfact)
188
189     def _audit(self, fact):
190         cherrypy.log('%s: %s' % (self.plugin_name, fact))