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