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