d882b40c64ea54e2bde03b9c5827510bc7b5af22
[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         self.is_enabled = False
60
61     def on_enable(self):
62         # this one does nothing
63         # derived classes can override with custom behavior
64         return
65
66     def get_tree(self, site):
67         raise NotImplementedError
68
69     def register(self, site):
70         if self.tree:
71             # already registered
72             return
73
74         # configure self
75         plugins = site[FACILITY]
76         if self.name in plugins['config']:
77             self.set_config(plugins['config'][self.name])
78
79         # init pages and admin interfaces
80         self.tree = self.get_tree(site)
81
82         self._debug('IdP Provider registered: %s' % self.name)
83
84         if self.get_config_value('enabled') == '1':
85             # and enable self
86             self._enable(site)
87
88     def _enable(self, site):
89         root = site[FACILITY]['root']
90         root.add_subtree(self.name, self.tree)
91         self._debug('IdP Provider enabled: %s' % self.name)
92         self.is_enabled = True
93         self.on_enable()
94
95     def enable(self, site):
96         if self.is_enabled:
97             return
98
99         self._enable(site)
100         self.set_config_value('enabled', '1')
101         self.save_plugin_config(FACILITY)
102
103     def disable(self, site):
104         if not self.is_enabled:
105             return
106
107         # remove self to the root
108         root = site[FACILITY]['root']
109         root.del_subtree(self.name)
110
111         self.is_enabled = False
112         self.set_config_value('enabled', '0')
113         self.save_plugin_config(FACILITY)
114         self._debug('IdP Provider disabled: %s' % self.name)
115
116
117 class ProviderPageBase(Page):
118
119     def __init__(self, site, config):
120         super(ProviderPageBase, self).__init__(site)
121         self.plugin_name = config.name
122         self.cfg = config
123
124     def GET(self, *args, **kwargs):
125         raise cherrypy.HTTPError(501)
126
127     def POST(self, *args, **kwargs):
128         raise cherrypy.HTTPError(501)
129
130     def root(self, *args, **kwargs):
131         method = cherrypy.request.method
132
133         preop = getattr(self, 'pre_%s' % method, None)
134         if preop and callable(preop):
135             preop(*args, **kwargs)
136
137         op = getattr(self, method, self.GET)
138         if callable(op):
139             return op(*args, **kwargs)
140         else:
141             raise cherrypy.HTTPError(405)
142
143     def _debug(self, fact):
144         superfact = '%s: %s' % (self.plugin_name, fact)
145         super(ProviderPageBase, self)._debug(superfact)
146
147     def _audit(self, fact):
148         cherrypy.log('%s: %s' % (self.plugin_name, fact))
149
150
151 FACILITY = 'provider_config'
152
153
154 class LoadProviders(Log):
155
156     def __init__(self, root, site):
157         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
158         site[FACILITY] = loader.get_plugin_data()
159         providers = site[FACILITY]
160
161         available = providers['available'].keys()
162         self._debug('Available providers: %s' % str(available))
163
164         providers['root'] = root
165         for item in providers['available']:
166             plugin = providers['available'][item]
167             plugin.register(site)
168
169
170 class ProvidersInstall(object):
171
172     def __init__(self):
173         pi = PluginInstaller(ProvidersInstall)
174         self.plugins = pi.get_plugins()