Add server-install plugin configuration support
[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.plugin import PluginInstaller
22 from ipsilon.util.page import Page
23 import cherrypy
24
25
26 class ProviderException(Exception):
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     def _debug(self, fact):
36         if cherrypy.config.get('debug', False):
37             cherrypy.log('%s: %s' % (self.__class__.__name__, fact))
38
39
40 class ProviderBase(PluginObject):
41
42     def __init__(self, name, path):
43         super(ProviderBase, self).__init__()
44         self.name = name
45         self.path = path
46
47
48 class ProviderPageBase(Page):
49
50     def __init__(self, site, config):
51         super(ProviderPageBase, self).__init__(site)
52         self.plugin_name = config.name
53         self.cfg = config
54
55     def GET(self, *args, **kwargs):
56         raise cherrypy.HTTPError(501)
57
58     def POST(self, *args, **kwargs):
59         raise cherrypy.HTTPError(501)
60
61     def root(self, *args, **kwargs):
62         op = getattr(self, cherrypy.request.method, self.GET)
63         if callable(op):
64             return op(*args, **kwargs)
65         else:
66             raise cherrypy.HTTPError(405)
67
68     def _debug(self, fact):
69         superfact = '%s: %s' % (self.plugin_name, fact)
70         super(ProviderPageBase, self)._debug(superfact)
71
72     def _audit(self, fact):
73         cherrypy.log('%s: %s' % (self.plugin_name, fact))
74
75
76 FACILITY = 'provider_config'
77
78
79 class LoadProviders(object):
80
81     def __init__(self, root, site):
82         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
83         site[FACILITY] = loader.get_plugin_data()
84         providers = site[FACILITY]
85
86         available = providers['available'].keys()
87         self._debug('Available providers: %s' % str(available))
88
89         for item in providers['whitelist']:
90             self._debug('IdP Provider in whitelist: %s' % item)
91             if item not in providers['available']:
92                 continue
93             self._debug('IdP Provider enabled: %s' % item)
94             providers['enabled'].append(item)
95             provider = providers['available'][item]
96             if item in providers['config']:
97                 provider.set_config(providers['config'][item])
98             root.__dict__[item] = provider.get_tree(site)
99
100     def _debug(self, fact):
101         if cherrypy.config.get('debug', False):
102             cherrypy.log(fact)
103
104
105 class ProvidersInstall(object):
106
107     def __init__(self):
108         pi = PluginInstaller(ProvidersInstall)
109         self.plugins = pi.get_plugins()