Add provider plugins loader
[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.plugin import PluginLoader, PluginObject
21 from ipsilon.util.page import Page
22 import cherrypy
23
24
25 class ProviderBase(PluginObject):
26
27     def __init__(self, name, path):
28         super(ProviderBase, self).__init__()
29         self.name = name
30         self.path = path
31
32
33 class ProviderPageBase(Page):
34
35     def __init__(self, site, config):
36         super(ProviderPageBase, self).__init__(site)
37         self.plugin_name = config.name
38         self.cfg = config
39
40     def GET(self, *args, **kwargs):
41         raise cherrypy.HTTPError(501)
42
43     def POST(self, *args, **kwargs):
44         raise cherrypy.HTTPError(501)
45
46     def root(self, *args, **kwargs):
47         op = getattr(self, cherrypy.request.method, self.GET)
48         if callable(op):
49             return op(*args, **kwargs)
50         else:
51             raise cherrypy.HTTPError(405)
52
53     def _debug(self, fact):
54         superfact = '%s: %s' % (self.plugin_name, fact)
55         super(ProviderPageBase, self)._debug(superfact)
56
57     def _audit(self, fact):
58         cherrypy.log('%s: %s' % (self.plugin_name, fact))
59
60
61 FACILITY = 'provider_config'
62
63
64 class LoadProviders(object):
65
66     def __init__(self, root, site):
67         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
68         site[FACILITY] = loader.get_plugin_data()
69         providers = site[FACILITY]
70
71         available = providers['available'].keys()
72         self._debug('Available providers: %s' % str(available))
73
74         for item in providers['whitelist']:
75             self._debug('IdP Provider in whitelist: %s' % item)
76             if item not in providers['available']:
77                 continue
78             self._debug('IdP Provider enabled: %s' % item)
79             providers['enabled'].append(item)
80             provider = providers['available'][item]
81             if item in providers['config']:
82                 provider.set_config(providers['config'][item])
83             root.__dict__[item] = provider.get_tree(site)
84
85     def _debug(self, fact):
86         if cherrypy.config.get('debug', False):
87             cherrypy.log(fact)