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