Move some exceptions into provider.common
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from ipsilon.util.log import Log
21 from ipsilon.util.plugin import PluginLoader, PluginObject
22 from ipsilon.util.plugin import PluginInstaller
23 from ipsilon.util.page import Page
24 import cherrypy
25
26
27 class ProviderException(Exception, Log):
28
29     def __init__(self, message):
30         super(ProviderException, self).__init__(message)
31         self.message = message
32
33     def __str__(self):
34         return repr(self.message)
35
36
37 class AuthenticationError(ProviderException):
38
39     def __init__(self, message, code):
40         super(AuthenticationError, self).__init__(message)
41         self.code = code
42         self._debug('%s [%s]' % (message, code))
43
44
45 class InvalidRequest(ProviderException):
46
47     def __init__(self, message):
48         super(InvalidRequest, self).__init__(message)
49         self._debug(message)
50
51
52 class ProviderBase(PluginObject):
53
54     def __init__(self, name, path):
55         super(ProviderBase, self).__init__()
56         self.name = name
57         self.path = path
58         self.tree = None
59
60     def on_enable(self):
61         # this one does nothing
62         # derived classes can override with custom behavior
63         return
64
65     def get_tree(self, site):
66         raise NotImplementedError
67
68     def register(self, site):
69         if self.tree:
70             # already registered
71             return
72
73         # configure self
74         plugins = site[FACILITY]
75         if self.name in plugins['config']:
76             self.set_config(plugins['config'][self.name])
77
78         # init pages and admin interfaces
79         self.tree = self.get_tree(site)
80
81         self._debug('IdP Provider registered: %s' % self.name)
82
83         if self.get_config_value('enabled') == '1':
84             # and add self to the root
85             root = site[FACILITY]['root']
86             root.add_subtree(self.name, self.tree)
87             self._debug('IdP Provider enabled: %s' % self.name)
88
89     @property
90     def is_enabled(self):
91         if self.get_config_value('enabled') == '1':
92             return True
93         return False
94
95     def enable(self, site):
96         if self.is_enabled:
97             return
98
99         # and add self to the root
100         root = site[FACILITY]['root']
101         root.add_subtree(self.name, self.tree)
102
103         self.set_config_value('enabled', '1')
104         self.save_plugin_config(FACILITY)
105
106         self.on_enable()
107         self._debug('IdP Provider enabled: %s' % self.name)
108
109     def disable(self, site):
110         if not self.is_enabled:
111             return
112
113         # remove self to the root
114         root = site[FACILITY]['root']
115         root.del_subtree(self.name)
116
117         self.set_config_value('enabled', '0')
118         self.save_plugin_config(FACILITY)
119         self._debug('IdP Provider disabled: %s' % self.name)
120
121
122 class ProviderPageBase(Page):
123
124     def __init__(self, site, config):
125         super(ProviderPageBase, self).__init__(site)
126         self.plugin_name = config.name
127         self.cfg = config
128
129     def GET(self, *args, **kwargs):
130         raise cherrypy.HTTPError(501)
131
132     def POST(self, *args, **kwargs):
133         raise cherrypy.HTTPError(501)
134
135     def root(self, *args, **kwargs):
136         method = cherrypy.request.method
137
138         preop = getattr(self, 'pre_%s' % method, None)
139         if preop and callable(preop):
140             preop(*args, **kwargs)
141
142         op = getattr(self, method, self.GET)
143         if callable(op):
144             return op(*args, **kwargs)
145         else:
146             raise cherrypy.HTTPError(405)
147
148     def _debug(self, fact):
149         superfact = '%s: %s' % (self.plugin_name, fact)
150         super(ProviderPageBase, self)._debug(superfact)
151
152     def _audit(self, fact):
153         cherrypy.log('%s: %s' % (self.plugin_name, fact))
154
155
156 FACILITY = 'provider_config'
157
158
159 class LoadProviders(Log):
160
161     def __init__(self, root, site):
162         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
163         site[FACILITY] = loader.get_plugin_data()
164         providers = site[FACILITY]
165
166         available = providers['available'].keys()
167         self._debug('Available providers: %s' % str(available))
168
169         providers['root'] = root
170         for item in providers['available']:
171             plugin = providers['available'][item]
172             plugin.register(site)
173
174
175 class ProvidersInstall(object):
176
177     def __init__(self):
178         pi = PluginInstaller(ProvidersInstall)
179         self.plugins = pi.get_plugins()